|
| 1 | +# Data-pack |
| 2 | + |
| 3 | +Utility pack for working with random data |
| 4 | + |
| 5 | +## File utils |
| 6 | + |
| 7 | +Simple wrapper around `fs` and `fs/promises`. |
| 8 | + |
| 9 | +```typescript |
| 10 | +function read(path: string): Promise<string>; |
| 11 | +function readSync(path: string): string; |
| 12 | +function readJSON<T>(path: string): Promise<T>; |
| 13 | +function readJSONSync<T>(path: string): Promise<T>; |
| 14 | + |
| 15 | +function write(path: string, content: any): Promise<void>; |
| 16 | +function writeSync(path: string, content: any): void; |
| 17 | +function withWriteStream(path: string, block: (stream: WriteStream) => void): void; |
| 18 | +``` |
| 19 | + |
| 20 | +## CSV |
| 21 | + |
| 22 | +Utilities for parsing and writing CSV files |
| 23 | + |
| 24 | +```typescript |
| 25 | +function* parseCSV(content: string, delimiter: string = ''): Generator<Array<string>, void>; |
| 26 | +function* parseCSVSync(content: string, delimiter: string = ''): Array<Array<string>>; |
| 27 | +function* parseCSVToFlow(content: string, delimiter: string = ''): GeneratorFlow<Array<string>>; |
| 28 | +``` |
| 29 | + |
| 30 | +## JSON |
| 31 | + |
| 32 | +Utilities for parsing and writing JSON |
| 33 | + |
| 34 | +```typescript |
| 35 | +function serialize(content: any): string; |
| 36 | +function deserialize<T>(content: string): T; |
| 37 | +``` |
| 38 | + |
| 39 | +## Generator flows |
| 40 | + |
| 41 | +Utilities for working with generators as if they are arrays |
| 42 | + |
| 43 | +```typescript |
| 44 | +// Creation |
| 45 | +const flow = GeneratorFlow.ofArray([1, 2, 3, 4]); |
| 46 | +const numFlow = GeneratorFlow.ofArray([5, 6]); |
| 47 | +const otherFlow = new GeneratorFlow(function* () { |
| 48 | + yield 'a'; |
| 49 | + yield 'b'; |
| 50 | + yield 'c'; |
| 51 | + yield 'd'; |
| 52 | +}); |
| 53 | + |
| 54 | +// Stream functions |
| 55 | +flow.skipWhile((it) => it < 2); // Flow[3,4] |
| 56 | +flow.takeWhile((it) => it < 2); // Flow[1] |
| 57 | +flow.skip(2); // Flow[3,4] |
| 58 | +flow.tail(); // Flow[2,3,4] |
| 59 | +flow.take(2); // Flow[1,2] |
| 60 | +flow.map((it) => it * 2); // Flow[2,4,6,8] |
| 61 | +flow.flatMap((it) => [it * 2]); // Flow[2,4,6,8] |
| 62 | +flow.filter((it) => it % 2 == 0); // Flow[2,4] |
| 63 | +flow.zip(otherFlow); // Flow[[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']] |
| 64 | +flow.cartesianProduct(otherFlow); // Flow[[1, 'a'], [1, 'b'], [1, 'c'], [1, 'd'], [2, 'a'], ... [4, 'd']] |
| 65 | +flow.concat(otherFlow); // Flow[1, 2, 3, 4, 5, 6] |
| 66 | +flow.peek(console.log); // Flow[1, 2, 3, 4] |
| 67 | +flow.reduce(add, 0); // 10 |
| 68 | +flow.size(); // 4 |
| 69 | +flow.toArray(); // [1, 2, 3, 4] |
| 70 | +flow.toSet(); // Set<1, 2, 3, 4> |
| 71 | +``` |
0 commit comments