npm.io
1.0.1 • Published 4 years ago

iwrap

Licence
MIT
Version
1.0.1
Deps
0
Size
38 kB
Vulns
0
Weekly
0

Iterable Wrapper

Version License: MIT

A wrapper around Iterable to bring access to powerful methods like filter, map, etc ...

Install

Install by running this command:

npm i iwrap

How to use

import { IWrap } from "iwrap";

const scores = new Map([
    ["Alice", 500],
    ["Bob", 356],
    ["Charles", 42],
    ["David", 999],
]);

// Output wanted:
// Alice.........................500
// Bob...........................356
// Charles.......................42
// David.........................999

// Without IWrap
const display1 = Array.from(scores) //from: scores is iterated and a array is created.
    // map: previous array is iterated and a new array is created.
    .map(([name, score]) => `${name}${".".repeat(30 - name.length)}${score}`)
    // join: previous array is iterated and return a string.
    .join("\n");
// Result: 3 iterations
console.log(display1);

// With IWrap
const display2 = IWrap.from(scores) // from: creates a wrapper around scores.
    // map: a new wrapper is created around the prevous one.
    .map(([name, score]) => `${name}${".".repeat(30 - name.length)}${score}`)
    // join: iterate the previous wrapper and return a string.
    .join("\n");
// Result: 1 iteration
console.log(display2);

Author

Tristan Guichaoua

License

Copyright 2021 Tristan Guichaoua.
This project is MIT licensed.

Keywords