{"id":20046416,"url":"https://github.com/aneldev/dyna-count","last_synced_at":"2025-03-02T07:48:51.334Z","repository":{"id":39100223,"uuid":"269571296","full_name":"aneldev/dyna-count","owner":"aneldev","description":"For loop replacement","archived":false,"fork":false,"pushed_at":"2022-12-12T23:56:25.000Z","size":3631,"stargazers_count":0,"open_issues_count":20,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-12T20:12:10.303Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aneldev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-05T08:19:19.000Z","updated_at":"2020-06-20T11:39:44.000Z","dependencies_parsed_at":"2023-01-28T03:16:01.825Z","dependency_job_id":null,"html_url":"https://github.com/aneldev/dyna-count","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-count","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-count/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-count/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-count/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aneldev","download_url":"https://codeload.github.com/aneldev/dyna-count/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241476435,"owners_count":19968916,"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-13T11:23:44.642Z","updated_at":"2025-03-02T07:48:51.292Z","avatar_url":"https://github.com/aneldev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About dyna-count\n\nSimplifies the `for` loop.\n\nIt also offers a `map`.\n\n# for\n\nInstead of\n\n`for (let i = 0; i \u003c 1000; i++) console.log(i);`\n\nyou can\n\n`count(1000).for(i =\u003e console.log(i))`\n\nor simpler\n\n`count(1000).for(console.log)`\n\nYou can `for` with step\n\n`count(10, 2).for(index =\u003e console.log(index))` // consoles: 0, 2, 4, 6, 8\n\nYou can `break` a `for`, calling the 2nd argument of the `for`'s callback\n\n```\nlet collected = [];\ncount(20).for((index, breakIt) =\u003e {\n    collected.push(index);\n    if (index === 9) breakIt();\n});\n// 0..9 items are only collected\n```\n\n# map\n\nYou can `map` also the iteration, useful for data generation.\n\n```\nlet items = [];\ncount(3).map(i =\u003e items.push(i));\n// items is now [0, 1, 2]\n```\n\nIn Typescript, `map` supports types, for example:\n\n```\nlet items: string[] = [];\ncount(3).map\u003cstring\u003e(i =\u003e items.push(i.toString()));\n// items is now ['0', '1', '2']\n```\n\nYou can `map` with `step` in the same way as with `for`.\n\nYou can `break` in the same way as with `for`.\n\n# countFromTo\n\nSimilarly, we have and the  `countFromTo`\n\n- `for`\n\n`countFromTo(100, 200).for(i =\u003e console.log(i))`\n\n- `map`\n\n```\nlet items = [];\ncountFromTo(10, 13).map(i =\u003e items.push(i));\n// items is now [10, 11, 12]\n```\n\n# reduce\n\n```\nreduced = count(3).reduce\u003e((acc, index) =\u003e {\n  acc[`key-${index}`] = `Key is ${index}`;\n  return acc;\n}, {});\n// reduced is equal to {\"key-0\": \"Key is 0\", \"key-1\": \"Key is 1\", \"key-2\": \"Key is 2\" }\n```\n\n# Import\n\n`import {count, countFromTo} from \"dyna-count\";`\n\n# Signatures\n\n`count/countFromTo(countTo: number).for: (cb: (index: number, cbBreak: () =\u003e void) =\u003e void): void`\n\n`count/countFromTo(countTo: number).map: \u003cT\u003e(cb: (index: number, cbBreak: () =\u003e void) =\u003e T): T[]`\n\n`count/countFromTo(countTo: number).reduce: \u003cT = any\u003e(cb: (acc: T, index: number) =\u003e T, initialValue: T): T`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-count","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faneldev%2Fdyna-count","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-count/lists"}