Quellcode durchsuchen

Removedold utilities.

Eugene Lazutkin vor 3 Jahren
Ursprung
Commit
57a9d67eae
12 geänderte Dateien mit 0 neuen und 588 gelöschten Zeilen
  1. 0 94
      utils/FromIterable.js
  2. 0 40
      utils/Reduce.js
  3. 0 85
      utils/asFun.js
  4. 0 77
      utils/asGen.js
  5. 0 20
      utils/comp.js
  6. 0 43
      utils/fold.js
  7. 0 24
      utils/gen.js
  8. 0 41
      utils/scan.js
  9. 0 32
      utils/skip.js
  10. 0 46
      utils/skipWhile.js
  11. 0 39
      utils/take.js
  12. 0 47
      utils/takeWhile.js

+ 0 - 94
utils/FromIterable.js

@@ -1,94 +0,0 @@
-'use strict';
-
-const {Readable} = require('stream');
-
-class FromIterable extends Readable {
-  constructor(options) {
-    super(Object.assign({}, options, {objectMode: true}));
-    this._iterable = null;
-    this._next = null;
-    if (options) {
-      'iterable' in options && (this._iterable = options.iterable);
-    }
-    !this._iterable && (this._read = this._readStop);
-  }
-
-  _read() {
-    if (Symbol.asyncIterator && typeof this._iterable[Symbol.asyncIterator] == 'function') {
-      this._next = this._iterable[Symbol.asyncIterator]();
-      this._iterable = null;
-      this._read = this._readNext;
-      this._readNext();
-      return;
-    }
-    if (Symbol.iterator && typeof this._iterable[Symbol.iterator] == 'function') {
-      this._next = this._iterable[Symbol.iterator]();
-      this._iterable = null;
-      this._read = this._readNext;
-      this._readNext();
-      return;
-    }
-    if (typeof this._iterable.next == 'function') {
-      this._next = this._iterable;
-      this._iterable = null;
-      this._read = this._readNext;
-      this._readNext();
-      return;
-    }
-    const result = this._iterable();
-    this._iterable = null;
-    if (result && typeof result.then == 'function') {
-      result.then(value => this.push(value), error => this.emit('error', error));
-      this._read = this._readStop;
-      return;
-    }
-    if (result && typeof result.next == 'function') {
-      this._next = result;
-      this._read = this._readNext;
-      this._readNext();
-      return;
-    }
-    this.push(result);
-    this._read = this._readStop;
-  }
-
-  _readNext() {
-    for (;;) {
-      const result = this._next.next();
-      if (result && typeof result.then == 'function') {
-        result.then(
-          value => {
-            if (value.done || value.value === null) {
-              this.push(null);
-              this._next = null;
-              this._read = this._readStop;
-            } else {
-              this.push(value.value);
-            }
-          },
-          error => this.emit('error', error)
-        );
-        break;
-      }
-      if (result.done || result.value === null) {
-        this.push(null);
-        this._next = null;
-        this._read = this._readStop;
-        break;
-      }
-      if (!this.push(result.value)) break;
-    }
-  }
-
-  _readStop() {
-    this.push(null);
-  }
-
-  static make(iterable) {
-    return new FromIterable(typeof iterable == 'object' && iterable.iterable ? iterable : {iterable});
-  }
-}
-FromIterable.fromIterable = FromIterable.make;
-FromIterable.make.Constructor = FromIterable;
-
-module.exports = FromIterable;

+ 0 - 40
utils/Reduce.js

@@ -1,40 +0,0 @@
-'use strict';
-
-const {Writable} = require('stream');
-
-const defaultInitial = 0;
-const defaultReducer = (acc, value) => value;
-
-class Reduce extends Writable {
-  constructor(options) {
-    super(Object.assign({}, options, {objectMode: true}));
-    this.accumulator = defaultInitial;
-    this._reducer = defaultReducer;
-    if (options) {
-      'initial' in options && (this.accumulator = options.initial);
-      'reducer' in options && (this._reducer = options.reducer);
-    }
-  }
-  _write(chunk, encoding, callback) {
-    const result = this._reducer.call(this, this.accumulator, chunk);
-    if (result && typeof result.then == 'function') {
-      result.then(
-        value => {
-          this.accumulator = value;
-          callback(null);
-        },
-        error => callback(error)
-      );
-    } else {
-      this.accumulator = result;
-      callback(null);
-    }
-  }
-  static make(reducer, initial) {
-    return new Reduce(typeof reducer == 'object' ? reducer : {reducer, initial});
-  }
-}
-Reduce.reduce = Reduce.make;
-Reduce.make.Constructor = Reduce;
-
-module.exports = Reduce;

+ 0 - 85
utils/asFun.js

@@ -1,85 +0,0 @@
-'use strict';
-
-const {none, final, isFinalValue, getFinalValue, many, isMany, getManyValues} = require('../defs');
-
-const next = async (value, fns, index, push) => {
-  for (let i = index; i <= fns.length; ++i) {
-    if (value && typeof value.then == 'function') {
-      // thenable
-      value = await value;
-    }
-    if (value === none) break;
-    if (isFinalValue(value)) {
-      const val = getFinalValue(value);
-      val !== none && push(val);
-      break;
-    }
-    if (isMany(value)) {
-      const values = getManyValues(value);
-      if (i == fns.length) {
-        values.forEach(val => push(val));
-      } else {
-        for (let j = 0; j < values.length; ++j) {
-          await next(values[j], fns, i, push);
-        }
-      }
-      break;
-    }
-    if (value && typeof value.next == 'function') {
-      // generator
-      for (;;) {
-        let data = value.next();
-        if (data && typeof data.then == 'function') {
-          data = await data;
-        }
-        if (data.done) break;
-        if (i == fns.length) {
-          push(data.value);
-        } else {
-          await next(data.value, fns, i, push);
-        }
-      }
-      break;
-    }
-    if (i == fns.length) {
-      push(value);
-      break;
-    }
-    value = fns[i](value);
-  }
-};
-
-const nop = () => {};
-
-const asFun = (...fns) => {
-  fns = fns.filter(fn => fn);
-  if (!fns.length) return nop;
-  if (Symbol.asyncIterator && fns[0][Symbol.asyncIterator]) {
-    fns[0] = fns[0][Symbol.asyncIterator];
-  } else if (Symbol.iterator && fns[0][Symbol.iterator]) {
-    fns[0] = fns[0][Symbol.iterator];
-  }
-  return async value => {
-    const results = [];
-    await next(value, fns, 0, value => results.push(value));
-    switch (results.length) {
-      case 0:
-        return none;
-      case 1:
-        return results[0];
-    }
-    return many(results);
-  };
-};
-
-asFun.next = next;
-
-asFun.none = none;
-asFun.final = final;
-asFun.isFinalValue = isFinalValue;
-asFun.getFinalValue = getFinalValue;
-asFun.many = many;
-asFun.isMany = isMany;
-asFun.getManyValues = getManyValues;
-
-module.exports = asFun;

+ 0 - 77
utils/asGen.js

@@ -1,77 +0,0 @@
-'use strict';
-
-const {none, final, isFinalValue, getFinalValue, many, isMany, getManyValues} = require('../defs');
-
-const next = async function*(value, fns, index) {
-  for (let i = index; i <= fns.length; ++i) {
-    if (value && typeof value.then == 'function') {
-      // thenable
-      value = await value;
-    }
-    if (value === none) break;
-    if (isFinalValue(value)) {
-      const val = getFinalValue(value);
-      if (val !== none) yield val;
-      break;
-    }
-    if (isMany(value)) {
-      const values = getManyValues(value);
-      if (i == fns.length) {
-        yield* values;
-      } else {
-        for (let j = 0; j < values.length; ++j) {
-          yield* next(values[j], fns, i);
-        }
-      }
-      break;
-    }
-    if (value && typeof value.next == 'function') {
-      // generator
-      for (;;) {
-        let data = value.next();
-        if (data && typeof data.then == 'function') {
-          data = await data;
-        }
-        if (data.done) break;
-        if (i == fns.length) {
-          yield data.value;
-        } else {
-          yield* next(data.value, fns, i);
-        }
-      }
-      break;
-    }
-    if (i == fns.length) {
-      yield value;
-      break;
-    }
-    value = fns[i](value);
-  }
-};
-
-const nop = async function*() {};
-
-const asGen = (...fns) => {
-  fns = fns.filter(fn => fn);
-  if (!fns.length) return nop;
-  if (Symbol.asyncIterator && fns[0][Symbol.asyncIterator]) {
-    fns[0] = fns[0][Symbol.asyncIterator];
-  } else if (Symbol.iterator && fns[0][Symbol.iterator]) {
-    fns[0] = fns[0][Symbol.iterator];
-  }
-  return async function*(value) {
-    yield* next(value, fns, 0);
-  };
-};
-
-asGen.next = next;
-
-asGen.none = none;
-asGen.final = final;
-asGen.isFinalValue = isFinalValue;
-asGen.getFinalValue = getFinalValue;
-asGen.many = many;
-asGen.isMany = isMany;
-asGen.getManyValues = getManyValues;
-
-module.exports = asGen;

+ 0 - 20
utils/comp.js

@@ -1,20 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-const {next} = require('./asFun');
-const {sanitize} = require('../index');
-
-const comp = (...fns) => {
-  fns = fns.filter(fn => fn);
-  return fns.length
-    ? new Transform({
-        writableObjectMode: true,
-        readableObjectMode: true,
-        transform(chunk, encoding, callback) {
-          next(chunk, fns, 0, value => sanitize(value, this)).then(() => callback(null), error => callback(error));
-        }
-      })
-    : null;
-};
-
-module.exports = comp;

+ 0 - 43
utils/fold.js

@@ -1,43 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-
-const defaultInitial = 0;
-const defaultReducer = (acc, value) => value;
-
-class Fold extends Transform {
-  constructor(options) {
-    super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
-    this._accumulator = defaultInitial;
-    this._reducer = defaultReducer;
-    if (options) {
-      'initial' in options && (this._accumulator = options.initial);
-      'reducer' in options && (this._reducer = options.reducer);
-    }
-  }
-  _transform(chunk, encoding, callback) {
-    const result = this._reducer.call(this, this._accumulator, chunk);
-    if (result && typeof result.then == 'function') {
-      result.then(
-        value => {
-          this._accumulator = value;
-          callback(null);
-        },
-        error => callback(error)
-      );
-    } else {
-      this._accumulator = result;
-      callback(null);
-    }
-  }
-  _final(callback) {
-    this.push(this._accumulator);
-    callback(null);
-  }
-  static make(reducer, initial) {
-    return new Fold(typeof reducer == 'object' ? reducer : {reducer, initial});
-  }
-}
-Fold.make.Constructor = Fold;
-
-module.exports = Fold.make;

+ 0 - 24
utils/gen.js

@@ -1,24 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-const {next} = require('./asGen');
-const {sanitize} = require('../index');
-
-const gen = (...fns) => {
-  fns = fns.filter(fn => fn);
-  return fns.length
-    ? new Transform({
-        writableObjectMode: true,
-        readableObjectMode: true,
-        transform(chunk, encoding, callback) {
-          (async () => {
-            for await (let value of next(chunk, fns, 0)) {
-              sanitize(value, this);
-            }
-          })().then(() => callback(null), error => callback(error));
-        }
-      })
-    : null;
-};
-
-module.exports = gen;

+ 0 - 41
utils/scan.js

@@ -1,41 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-
-const defaultInitial = 0;
-const defaultReducer = (acc, value) => value;
-
-class Scan extends Transform {
-  constructor(options) {
-    super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
-    this._accumulator = defaultInitial;
-    this._reducer = defaultReducer;
-    if (options) {
-      'initial' in options && (this._accumulator = options.initial);
-      'reducer' in options && (this._reducer = options.reducer);
-    }
-  }
-  _transform(chunk, encoding, callback) {
-    const result = this._reducer.call(this, this._accumulator, chunk);
-    if (result && typeof result.then == 'function') {
-      result.then(
-        value => {
-          this._accumulator = value;
-          this.push(this._accumulator);
-          callback(null);
-        },
-        error => callback(error)
-      );
-    } else {
-      this._accumulator = result;
-      this.push(this._accumulator);
-      callback(null);
-    }
-  }
-  static make(reducer, initial) {
-    return new Scan(typeof reducer == 'object' ? reducer : {reducer, initial});
-  }
-}
-Scan.make.Constructor = Scan;
-
-module.exports = Scan.make;

+ 0 - 32
utils/skip.js

@@ -1,32 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-
-class Skip extends Transform {
-  constructor(options) {
-    super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
-    this._n = 0;
-    if (options) {
-      'n' in options && (this._n = options.n);
-    }
-    if (this._n <= 0) {
-      this._transform = this._passThrough;
-    }
-  }
-  _transform(chunk, encoding, callback) {
-    if (--this._n <= 0) {
-      this._transform = this._passThrough;
-    }
-    callback(null);
-  }
-  _passThrough(chunk, encoding, callback) {
-    this.push(chunk);
-    callback(null);
-  }
-  static make(n) {
-    return new Skip(typeof n == 'object' ? n : {n});
-  }
-}
-Skip.make.Constructor = Skip;
-
-module.exports = Skip.make;

+ 0 - 46
utils/skipWhile.js

@@ -1,46 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-
-const alwaysFalse = () => false;
-
-class SkipWhile extends Transform {
-  constructor(options) {
-    super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
-    this._condition = alwaysFalse;
-    if (options) {
-      'condition' in options && (this._condition = options.condition);
-    }
-  }
-  _transform(chunk, encoding, callback) {
-    const result = this._condition.call(this, chunk);
-    if (result && typeof result.then == 'function') {
-      result.then(
-        flag => {
-          if (!flag) {
-            this._transform = this._passThrough;
-            this.push(chunk);
-          }
-          callback(null);
-        },
-        error => callback(error)
-      );
-    } else {
-      if (!result) {
-        this._transform = this._passThrough;
-        this.push(chunk);
-      }
-      callback(null);
-    }
-  }
-  _passThrough(chunk, encoding, callback) {
-    this.push(chunk);
-    callback(null);
-  }
-  static make(condition) {
-    return new SkipWhile(typeof condition == 'object' ? condition : {condition});
-  }
-}
-SkipWhile.make.Constructor = SkipWhile;
-
-module.exports = SkipWhile.make;

+ 0 - 39
utils/take.js

@@ -1,39 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-
-class Take extends Transform {
-  constructor(options) {
-    super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
-    this._n = this._skip = 0;
-    if (options) {
-      'n' in options && (this._n = options.n);
-      'skip' in options && (this._skip = options.skip);
-    }
-    if (this._skip <= 0) {
-      this._transform = this._n > 0 ? this._countValues : this._doNothing;
-    }
-  }
-  _transform(chunk, encoding, callback) {
-    if (--this._skip <= 0) {
-      this._transform = this._n > 0 ? this._countValues : this._doNothing;
-    }
-    callback(null);
-  }
-  _countValues(chunk, encoding, callback) {
-    if (--this._n <= 0) {
-      this._transform = this._doNothing;
-    }
-    this.push(chunk);
-    callback(null);
-  }
-  _doNothing(chunk, encoding, callback) {
-    callback(null);
-  }
-  static make(n) {
-    return new Take(typeof n == 'object' ? n : {n});
-  }
-}
-Take.make.Constructor = Take;
-
-module.exports = Take.make;

+ 0 - 47
utils/takeWhile.js

@@ -1,47 +0,0 @@
-'use strict';
-
-const {Transform} = require('stream');
-
-const alwaysTrue = () => true;
-
-class TakeWhile extends Transform {
-  constructor(options) {
-    super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
-    this._condition = alwaysTrue;
-    if (options) {
-      'condition' in options && (this._condition = options.condition);
-    }
-  }
-  _transform(chunk, encoding, callback) {
-    const result = this._condition.call(this, chunk);
-    if (result && typeof result.then == 'function') {
-      result.then(
-        flag => {
-          if (flag) {
-            this.push(chunk);
-          } else {
-            this._transform = this._doNothing;
-          }
-          callback(null);
-        },
-        error => callback(error)
-      );
-    } else {
-      if (result) {
-        this.push(chunk);
-      } else {
-        this._transform = this._doNothing;
-      }
-      callback(null);
-    }
-  }
-  _doNothing(chunk, encoding, callback) {
-    callback(null);
-  }
-  static make(condition) {
-    return new TakeWhile(typeof condition == 'object' ? condition : {condition});
-  }
-}
-TakeWhile.make.Constructor = TakeWhile;
-
-module.exports = TakeWhile.make;