Quellcode durchsuchen

Replaced unique objects with unique symbols.

Eugene Lazutkin vor 7 Jahren
Ursprung
Commit
c298ba041b
4 geänderte Dateien mit 61 neuen und 35 gelöschten Zeilen
  1. 14 11
      defs.js
  2. 24 9
      index.js
  3. 12 8
      utils/asFun.js
  4. 11 7
      utils/asGen.js

+ 14 - 11
defs.js

@@ -1,19 +1,22 @@
 'use strict';
 
-const none = {};
+const none = Symbol.for('stream-chain.none');
+const finalSymbol = Symbol.for('stream-chain.final');
+const manySymbol = Symbol.for('stream-chain.many');
 
-function Final(value) {
-  this.value = value;
-}
-const final = value => new Final(value);
+const final = value => ({[finalSymbol]: value});
+const many = values => ({[manySymbol]: values});
 
-function Many(values) {
-  this.values = values;
-}
-const many = values => new Many(values);
+const isFinal = o => o && typeof o == 'object' && finalSymbol in o;
+const isMany = o => o && typeof o == 'object' && manySymbol in o;
+
+const getFinalValue = o => o[finalSymbol];
+const getManyValues = o => o[manySymbol];
 
 module.exports.none = none;
-module.exports.Final = Final;
 module.exports.final = final;
-module.exports.Many = Many;
+module.exports.isFinal = isFinal;
+module.exports.getFinalValue = getFinalValue;
 module.exports.many = many;
+module.exports.isMany = isMany;
+module.exports.getManyValues = getManyValues;

+ 24 - 9
index.js

@@ -1,7 +1,20 @@
 'use strict';
 
 const {Readable, Writable, Duplex, Transform} = require('stream');
-const {none, Final, Many, final, many} = require('./defs');
+
+// const {none, Final, Many, final, many} = require('./defs');
+const none = Symbol.for('stream-chain.none');
+const finalSymbol = Symbol.for('stream-chain.final');
+const manySymbol = Symbol.for('stream-chain.many');
+
+const final = value => ({[finalSymbol]: value});
+const many = values => ({[manySymbol]: values});
+
+const isFinal = o => o && typeof o == 'object' && finalSymbol in o;
+const isMany = o => o && typeof o == 'object' && manySymbol in o;
+
+const getFinalValue = o => o[finalSymbol];
+const getManyValues = o => o[manySymbol];
 
 const runAsyncGenerator = async (gen, stream) => {
   for (;;) {
@@ -56,8 +69,8 @@ const wrapArray = fns =>
             callback(null);
             return;
           }
-          if (result instanceof Chain.Final) {
-            value = result.value;
+          if (Chain.isFinal(result)) {
+            value = Chain.getFinalValue(result);
             break;
           }
           value = result;
@@ -139,10 +152,10 @@ class Chain extends Duplex {
     return new Chain(fns, options);
   }
   static sanitize(result, stream) {
-    if (result instanceof Chain.Final) {
-      result = result.value;
-    } else if (result instanceof Chain.Many) {
-      result = result.values;
+    if (Chain.isFinal(result)) {
+      result = Chain.getFinalValue(result);
+    } else if (Chain.isMany(result)) {
+      result = Chain.getManyValues(result);
     }
     if (result !== undefined && result !== null && result !== Chain.none) {
       if (result instanceof Array) {
@@ -160,10 +173,12 @@ class Chain extends Duplex {
 }
 
 Chain.none = none;
-Chain.Final = Final;
 Chain.final = final;
-Chain.Many = Many;
+Chain.isFinal = isFinal;
+Chain.getFinalValue = getFinalValue;
 Chain.many = many;
+Chain.isMany = isMany;
+Chain.getManyValues = getManyValues;
 
 Chain.chain = Chain.make;
 Chain.make.Constructor = Chain;

+ 12 - 8
utils/asFun.js

@@ -1,6 +1,6 @@
 'use strict';
 
-const {none, Final, Many} = require('../defs');
+const {none, final, isFinal, getFinalValue, many, isMany, getManyValues} = require('../defs');
 
 const next = async (value, fns, index, push) => {
   for (let i = index; i <= fns.length; ++i) {
@@ -9,12 +9,12 @@ const next = async (value, fns, index, push) => {
       value = await value;
     }
     if (value === none) break;
-    if (value instanceof Final) {
-      value !== none && push(value.value);
+    if (isFinal(value)) {
+      value !== none && push(getFinalValue(value));
       break;
     }
-    if (value instanceof Many) {
-      const values = value.values;
+    if (isMany(value)) {
+      const values = getManyValues(value);
       if (i == fns.length) {
         values.forEach(val => push(val));
       } else {
@@ -62,14 +62,18 @@ const asFun = (...fns) => {
       case 1:
         return results[0];
     }
-    return new Many(results);
+    return many(results);
   };
 };
 
 asFun.next = next;
 
 asFun.none = none;
-asFun.Final = Final;
-asFun.Many = Many;
+asFun.final = final;
+asFun.isFinal = isFinal;
+asFun.getFinalValue = getFinalValue;
+asFun.many = many;
+asFun.isMany = isMany;
+asFun.getManyValues = getManyValues;
 
 module.exports = asFun;

+ 11 - 7
utils/asGen.js

@@ -1,6 +1,6 @@
 'use strict';
 
-const {none, Final, Many} = require('../defs');
+const {none, final, isFinal, getFinalValue, many, isMany, getManyValues} = require('../defs');
 
 const next = async function*(value, fns, index) {
   for (let i = index; i <= fns.length; ++i) {
@@ -9,12 +9,12 @@ const next = async function*(value, fns, index) {
       value = await value;
     }
     if (value === none) break;
-    if (value instanceof Final) {
-      if (value !== none) yield value;
+    if (isFinal(value)) {
+      if (value !== none) yield getFinalValue(value);
       break;
     }
-    if (value instanceof Many) {
-      const values = value.values;
+    if (isMany(value)) {
+      const values = getManyValues(value);
       if (i == fns.length) {
         for (let j = 0; j < values.length; ++j) {
           yield values[j];
@@ -63,7 +63,11 @@ const asGen = (...fns) => {
 asGen.next = next;
 
 asGen.none = none;
-asGen.Final = Final;
-asGen.Many = Many;
+asGen.final = final;
+asGen.isFinal = isFinal;
+asGen.getFinalValue = getFinalValue;
+asGen.many = many;
+asGen.isMany = isMany;
+asGen.getManyValues = getManyValues;
 
 module.exports = asGen;