comprehension v1.1.1
Comprehension.js
Using
Array with static elements
c$(3, () => "asd")
[ "asd", "asd", "asd" ]
same as new Array(3).fill("asd")
Incremented elements
c$(5)
[ 0, 1, 2, 3, 4 ]
same as new Array(5).fill(0).map((_, i) => i)
Incrementing value
c$({ count: 5, step: 3 })
[ 0, 3, 6, 9, 12 ]
same as new Array(5).fill(0).map((_, i) => 3 * i)
Incrementing from-to
c$({ from: 4, to: 9 })
[ 4, 5, 6, 7, 8, 9 ]
same as new Array(6).fill(0).map((_, i) => i + 4)
Negative step support
c$({ from: 4, count: 5, step: -3 })
[ 4, 1, -2, -5, -8 ]
same as new Array(5).fill(0).map((_, i) => i * -3 + 4)
Mapping
c$({ from: 3, count: 4 }, i => 2 ** i)
[ 8, 16, 32, 64 ]
same as new Array(4).fill(0).map((_, i) => 2 ** (i + 3))
Index independent
c$({ count: 5 }, () => Math.floor(Math.random() * 8))
[ 2, 3, 6, 4, 2 ]
same as new Array(5).fill(0).map(() => Math.floor(Math.random() * 8))
Example of usage
Asynchronously creates folders with names "folder_100", "folder_106", ... , "folder_220" in directory "dist".
import c$ from "comprehension";
import { mkdirp } from "fs-extra";
async function createIndexedDirectory(index: number): string {
const path = `${__dirname}/dist/folder_${index}`;
await mkdirp(path);
return path;
}
// ...
await mkdirp(`${__dirname}/dist`);
const promises = c$(
{ from: 100, to: 220, step: 6 },
createIndexedDirectory,
);
console.log(await Promise.all(promises));