Browse Source

Initial commit

master
Sven Slootweg 5 months ago
commit
4359a24328
  1. 3
      .eslintrc.js
  2. 1
      .gitignore
  3. 31
      example.js
  4. 25
      index.js
  5. 22
      package.json
  6. 1404
      yarn.lock

3
.eslintrc.js

@ -0,0 +1,3 @@
module.exports = {
extends: "@joepie91/eslint-config"
};

1
.gitignore

@ -0,0 +1 @@
node_modules

31
example.js

@ -0,0 +1,31 @@
"use strict";
const Promise = require("bluebird");
const fromLazyValue = require("./");
const pipe = require("@promistream/pipe");
const collect = require("@promistream/collect");
return Promise.try(() => {
console.log("Creating stream");
let lazyValueSource = fromLazyValue(() => {
console.log("Creating lazy value");
return 42;
});
console.log("Piping");
return pipe([
lazyValueSource,
collect()
]).read();
}).then((result) => {
console.log(result);
/*
Creating stream
Piping
Creating lazy value
[ 42 ]
*/
});

25
index.js

@ -0,0 +1,25 @@
"use strict";
const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const isFunction = require("@validatem/is-function");
const EndOfStream = require("@promistream/end-of-stream");
const simpleSource = require("@promistream/simple-source");
module.exports = function createStreamFromLazyValue(_valueGenerator) {
let [ valueGenerator ] = validateArguments(arguments, {
valueGenerator: [ required, isFunction ]
});
let wasValueRead = false;
return simpleSource(() => {
if (wasValueRead === false) {
wasValueRead = true;
return valueGenerator();
} else {
throw new EndOfStream("Value was read");
}
});
};

22
package.json

@ -0,0 +1,22 @@
{
"name": "@promistream/from-lazy-value",
"version": "0.1.0",
"main": "index.js",
"repository": "http://git.cryto.net/promistream/from-lazy-value.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"devDependencies": {
"@joepie91/eslint-config": "^1.1.0",
"@promistream/collect": "^0.1.1",
"@promistream/pipe": "^0.1.5",
"bluebird": "^3.7.2",
"eslint": "^6.3.0"
},
"dependencies": {
"@promistream/end-of-stream": "^0.1.1",
"@promistream/simple-source": "^0.1.2",
"@validatem/core": "^0.3.13",
"@validatem/is-function": "^0.1.0",
"@validatem/required": "^0.1.1"
}
}

1404
yarn.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save