nutgaard / data-pack

Utility pack for working with random data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data-pack

Utility pack for working with random data

File utils

Simple wrapper around fs and fs/promises.

function read(path: string): Promise<string>;
function readSync(path: string): string;
function readJSON<T>(path: string): Promise<T>;
function readJSONSync<T>(path: string): Promise<T>;

function write(path: string, content: any): Promise<void>;
function writeSync(path: string, content: any): void;
function withWriteStream(path: string, block: (stream: WriteStream) => void): void;

CSV

Utilities for parsing and writing CSV files

function* parseCSV(content: string, delimiter: string = ''): Generator<Array<string>, void>;
function* parseCSVSync(content: string, delimiter: string = ''): Array<Array<string>>;
function* parseCSVToFlow(content: string, delimiter: string = ''): GeneratorFlow<Array<string>>;

JSON

Utilities for parsing and writing JSON

function serialize(content: any): string;
function deserialize<T>(content: string): T;

KeyValue (KV)

Utilities for parsing and writing in key-value pairs

function writeKVSync(object: object, delimiters: KvDelimiters): string;
function parseKVSync<T>(content: string, delimiters: KvDelimiters): T;

Generator flows

Utilities for working with generators as if they are arrays

// Creation
const flow = GeneratorFlow.ofArray([1, 2, 3, 4]);
const numFlow = GeneratorFlow.ofArray([5, 6]);
const otherFlow = new GeneratorFlow(function* () {
    yield 'a';
    yield 'b';
    yield 'c';
    yield 'd';
});

// Stream functions
flow.skipWhile((it) => it < 2); // Flow[3,4]
flow.takeWhile((it) => it < 2); // Flow[1]
flow.skip(2); // Flow[3,4]
flow.tail(); // Flow[2,3,4]
flow.take(2); // Flow[1,2]
flow.map((it) => it * 2); // Flow[2,4,6,8]
flow.flatMap((it) => [it * 2]); // Flow[2,4,6,8]
flow.filter((it) => it % 2 == 0); // Flow[2,4]
flow.zip(otherFlow); // Flow[[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]
flow.cartesianProduct(otherFlow); // Flow[[1, 'a'], [1, 'b'], [1, 'c'], [1, 'd'], [2, 'a'], ... [4, 'd']]
flow.concat(otherFlow); // Flow[1, 2, 3, 4, 5, 6]
flow.peek(console.log); // Flow[1, 2, 3, 4]
flow.reduce(add, 0); // 10
flow.size(); // 4
flow.toArray(); // [1, 2, 3, 4]
flow.toSet(); // Set<1, 2, 3, 4>

About

Utility pack for working with random data


Languages

Language:TypeScript 95.1%Language:JavaScript 3.9%Language:Shell 1.1%