{"name":"gensequence","version":"8.0.1","keywords":["generators","generator","iterator","iterators","iterable","functional","map","reduce"],"author":{"name":"Jason Dent"},"license":"MIT","_id":"gensequence@8.0.1","maintainers":[{"name":"jason-dent","email":"jason@streetsidesoftware.nl"}],"homepage":"https://github.com/streetsidesoftware/GenSequence#readme","bugs":{"url":"https://github.com/streetsidesoftware/GenSequence/issues"},"dist":{"shasum":"bf782c44dc90656c9f71cff7075a81cb65d29691","tarball":"http://registry-npm.ywwl.com/gensequence/download/gensequence-8.0.1.tgz","fileCount":27,"integrity":"sha512-1vI1qr5aEtOXpALKHF63rmFymCYaA6ivttk5Atyzxdbk3zH5ABjgFCaFcf8kQMykI1QEMUezW8+hzytBNb+TEg==","signatures":[{"sig":"MEUCIQD88hHe/cgi5Zg0jEGqeQ3NQNJyXO7QdQLBLJcj84D5mAIgCSw12Q96f9llBaMld3LKoTiyAstppo5574BbOKJRQ9A=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/gensequence@8.0.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":47527,"size":9612,"noattachment":false,"key":"/gensequence/-/gensequence-8.0.1.tgz"},"type":"module","module":"dist/index.js","engines":{"node":">=20"},"exports":{".":{"import":"./dist/index.js"},"./operators":{"import":"./dist/operators/index.js"}},"gitHead":"f25240a7a5bc34860c77e4681410badc7656fb28","scripts":{"lint":"npm run lint:prettier","test":"vitest run","build":"tsc -p .","clean":"shx rm -rf dist coverage","watch":"tsc -w -p .","coverage":"npm test -- --coverage","lint:fix":"npm run lint:prettier","clean-build":"npm run clean && npm run build","build:readme":"inject-markdown README.md","lint:prettier":"prettier . -w","prepublishOnly":"npm run clean-build","update-packages":"npx npm-check-updates -t semver -u && shx rm -rf node_modules package-lock.json && npm i"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:f27673af-e5c1-4724-adca-6c5deaee6951"}},"repository":{"url":"git+https://github.com/streetsidesoftware/GenSequence.git","type":"git"},"_npmVersion":"11.6.1","description":"Small library to simplify working with Generators and Iterators in Javascript / Typescript","directories":{},"sideEffects":false,"_nodeVersion":"24.11.0","_hasShrinkwrap":false,"devDependencies":{"shx":"^0.4.0","ts2mjs":"^4.0.0","vitest":"^3.2.4","prettier":"^3.6.2","typescript":"^5.9.3","@types/node":"^24.10.0","inject-markdown":"^4.0.0","@tsconfig/node20":"^20.1.6","@vitest/coverage-istanbul":"^3.2.4"},"_npmOperationalInternal":{"tmp":"tmp/gensequence_8.0.1_1762412305113_0.9204448103529093","host":"s3://npm-registry-packages-npm-production"},"readme":"# GenSequence\n\n[![Coverage Status](https://coveralls.io/repos/github/streetsidesoftware/GenSequence/badge.svg?branch=main)](https://coveralls.io/github/streetsidesoftware/GenSequence?branch=main)\n\nSmall library to simplify working with Generators and Iterators in Javascript / Typescript\n\nJavascript [Iterators and Generators](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Iterators_and_Generators)\nare very exciting and provide some powerful new ways to solve programming problems.\n\nThe purpose of this library is to make using the results of a generator function easier.\nIt is not intended as a replacement for arrays and the convenient `[...genFn()]` notation.\nGenSequence is useful for cases where you might not want an array of all possible values.\nGenSequence deals efficiently with large sequences because only one element at a time is evaluated.\nIntermediate arrays are not created, saving memory and cpu cycles.\n\n## Installation\n\n```sh\nnpm install -S gensequence\n```\n\n## Usage\n\n```ts\nimport { genSequence } from 'gensequence';\n```\n\n## Examples\n\n### Fibonacci\n\nThe Fibonacci sequence can be very simply expressed using a generator. Yet using the result of a generator can be a bit convoluted.\nGenSequence provides a wrapper to add familiar functionality similar to arrays.\n\n<!--- @@inject: ./src/samples/fibonacci.ts --->\n\n```ts\nimport { genSequence, Sequence } from '../index.js';\n\nexport function fibonacci(): Sequence<number> {\n  function* fib() {\n    let [a, b] = [0, 1];\n    while (true) {\n      yield b;\n      [a, b] = [b, a + b];\n    }\n  }\n  return genSequence(fib());\n}\n\nexport function fib(n: number) {\n  return fibonacci()\n    .take(n) // Take n from the fibonacci sequence\n    .toArray(); // Convert it into an array\n}\n\nexport const fib5 = fib(5); // [1, 1, 2, 3, 5]\n```\n\n<!--- @@inject-end: ./src/samples/fibonacci.ts --->\n\n### RegEx Match\n\nRegular expressions are wonderfully powerful. Yet, working with the results can sometimes be a bit of a pain.\n\n<!--- @@inject: ./src/samples/match.ts --->\n\n```ts\nimport { genSequence } from '../GenSequence.js';\n\nfunction matchWords(text: string) {\n  return genSequence(text.matchAll(/\\w+/g)).map((a) => a[0]);\n}\n\nexport function toSetOfWords(text: string) {\n  return new Set(matchWords(text));\n}\n\nexport const text = 'Some long bit of text with many words, duplicate words...';\nexport const setOfWords = toSetOfWords(text);\n// Walk through the set of words and pull out the 4 letter one.\nexport const setOf4LetterWords = new Set(genSequence(setOfWords).filter((a) => a.length === 4));\n```\n\n<!--- @@inject-end: ./src/samples/match.ts --->\n\n## Reference\n\n- `genSequence(Iterable|Array|()=>Iterable)` -- generate a new Iterable from an Iterable, Array or function with the following functions.\n\n### Filters\n\n- `.filter(fn)` -- just like array.filter, filters the sequence\n- `.skip(n)` -- skip _n_ entries in the sequence\n- `.take(n)` -- take the next _n_ entries in the sequence.\n\n### Extenders\n\n- `.concat(iterable)` -- this will extend the current sequence with the values from _iterable_\n- `.concatMap(fnMap)` -- this is used to flatten the result of a map function.\n\n### Mappers\n\n- `.combine(fnCombiner, iterable)` -- is used to combine values from two different lists.\n- `.map(fn)` -- just like array.map, allows you to convert the values in a sequence.\n- `.pipe(...operatorFns)` -- pipe any amount of operators in sequence.\n- `.scan(fn, init?)` -- similar to reduce, but returns a sequence of all the results of fn.\n\n### Reducers\n\n- `.all(fn)` -- true if all values in the sequence return true for _fn(value)_ or the sequence is empty.\n- `.any(fn)` -- true if any value in the sequence exists where _fn(value)_ returns true.\n- `.count()` -- return the number of values in the sequence.\n- `.first()` -- return the next value in the sequence.\n- `.first(fn)` -- return the next value in the sequence where _fn(value)_ return true.\n- `.forEach(fn)` -- apply _fn(value, index)_ to all values.\n- `.max()` -- return the largest value in the sequence.\n- `.max(fn)` -- return the largest value of _fn(value)_ in the sequence.\n- `.min()` -- return the smallest value in the sequence.\n- `.min(fn)` -- return the smallest value of _fn(value)_ in the sequence.\n- `.reduce(fn, init?)` -- just like array.reduce, reduces the sequence into a single result.\n- `.reduceAsync(fn, init?)` -- just like array.reduce, reduces promises into the sequence into a single result chaining the promises, fn/init can be async or not, it will work, the previousValue, and currentValue will never be a promise.\n- `.reduceToSequence(fn, init)` -- return a sequence of values that _fn_ creates from looking at all the values and the initial sequence.\n\n### Cast\n\n- `.toArray()` -- convert the sequence into an array. This is the same as `[...iterable]`.\n- `.toIterable()` -- Casts a Sequence into an IterableIterator - used in cases where type checking is too strict.\n","publish_time":1762412305359,"_cnpm_publish_time":1762412305359,"dist-tags":{"latest":"8.0.8"}}