{"id":21853339,"url":"https://github.com/emnudge/lazy-iter","last_synced_at":"2025-03-21T18:41:49.857Z","repository":{"id":44005035,"uuid":"237542111","full_name":"EmNudge/Lazy-Iter","owner":"EmNudge","description":"Lazy iterbales in JS with a small class","archived":false,"fork":false,"pushed_at":"2023-01-05T06:12:35.000Z","size":608,"stargazers_count":1,"open_issues_count":10,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T02:09:29.287Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EmNudge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-01T00:57:45.000Z","updated_at":"2020-05-02T04:39:56.000Z","dependencies_parsed_at":"2023-02-03T15:00:46.198Z","dependency_job_id":null,"html_url":"https://github.com/EmNudge/Lazy-Iter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmNudge%2FLazy-Iter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmNudge%2FLazy-Iter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmNudge%2FLazy-Iter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmNudge%2FLazy-Iter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EmNudge","download_url":"https://codeload.github.com/EmNudge/Lazy-Iter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244852073,"owners_count":20521151,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T01:21:51.049Z","updated_at":"2025-03-21T18:41:49.832Z","avatar_url":"https://github.com/EmNudge.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lazy-Iter\n\nThis is a utility class to allow lazy iterables in javascript. \nYou can read [here](https://dev.to/emnudge/lazy-iterators-from-scratch-2903) to learn about the problems with the current available iteration methods, but essentially, while normal array methods which consume HOFs are much more readable than a simple `for` loop, they can be much less performant.\n\nLazy iterables allow you to gain a significant performance boost in many instances.\n\n## Usage\n```js\nconst arr = [1, 2, 3, 4, 5];\nconst arrIter = new LazyIter(arr);\n\n// cube values, remove even results, and then only return elements while the current element is under 30 \n// takeWhile will skip iterations, unlike filter, making it more performant\narrIter\n  .map(n =\u003e n ** 3)\n  .filter(n =\u003e n % 2 == 1)\n  .takeWhile(n =\u003e n \u003c 30);\n  \n// collect is a consumer, which means it no longer returns the iterator\n// collect runs through the array and applies all the functions in order\nconst newArr = arrIter.collect();\n```\n\nA lazy iterable first encloses an array (which is never mutated, so no need to clone) and methods are called which are added to an internal array. Nothing happens until a consumer is called, at which point the iterable will internally iterate over the array and return values as a result of calling the functions in order.\n\nA number can alternatively be provided instead of an array, which creates an array of said size, filled with `null`.\n```js\nconst arrIter = new LazyIter(10);\nconst reduceFunc = (accum, val) =\u003e `${accum} ${val}`;\n\n// reduce is another consumer which works the same as `Array.prototype.reduce`, but it requires a starting value\nconst str = arrIter.map((_, i) =\u003e i).reduce(reduceFunc, '');\n\nconsole.log(str) // logs: \" 0 1 2 3 4 5 6 7 8 9\"\n```\n- The LazyIter instance can alternatively be thrown into the header of a `for` loop as it is an iterator.\n\nConsult the tests folder to see methods in use.\n\n## Documentation\n\ndocs and explanations can be seen on the [wiki](https://github.com/EmNudge/Lazy-Iter/wiki) as it provided a better format for longer and better-written explanations.\n\u003cbr /\u003e\n\n## ToDo\nA `.entries()` method may be added which returns an iterator with the value and index combined in an array, similarly to how `Array.prototype.entries` works. This is because using the `for` loop over `forEach` may be a style preference for many, but the index would have to be retrieved manually, by incrementing a local `index` variable on each iteration.\n\nThis would clean things up, but it is not of vital importance.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femnudge%2Flazy-iter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femnudge%2Flazy-iter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femnudge%2Flazy-iter/lists"}