baats-core
v0.2.2
Published
Baats Core - BAATS (Baavgai's TypeScript) Library
Downloads
18
Readme
BAATS (Baavgai's TypeScript) Library
This is part of a collection of code I use for my projects.
If you find it interesting, feel free to use it in yours.
Module baats-core
A TypeScript port of node-clone.
clone<T>(parent: T, circular = true, depth = Infinity, prototype: any = undefined): T;
It's amazaing how much functional programming fun opens up with this simple function. Half the time I've loaded all of lodash was for this function and clone.
range(size: number): number[];
Padding functions are so small you just end up copy pasting them... Or, some cases, just finding a library and moving on. I wrote lpad and don't expect anyone to care. But the first guy to get such code out there really managed to cause some drama: How one developer just broke Node, Babel and thousands of projects in 11 lines of JavaScript
repChar(size: number, symbol?: string): string;
lpad(value: any, size: number, symbol?: string): string;
rpad(value: any, size: number, symbol?: string): string;
zpad(value: any, zeros: number): string;
A little 24 char randomized string. Not quite a UUID, but pretty good. Made with simplicity and speed in mind.
uniqueId(): string;
Filter to uniq items, accepts optional key function. Maintains order and is non destructive.
uniq<T>(xs: Array<T>, getKey?: (x: T) => string): T[];
Test Code
import * as lib from "baats-core";
const out = console.log;
const line = () => out(lib.repChar(40, '-'));
line();
lib
.range(5)
.map(x=>x+7)
.forEach(x=>console.log(`${lib.zpad(x,2)}: ${lib.uniqueId()}`));
line();
out(lib.lpad("lpad", 10, '.'));
out(lib.rpad("rpad", 10, '.x'));
line();
class Foo {
constructor(public name: string, private age: number) { }
greeting() { console.log(`Hello ${this.name}`); }
}
const fooTest = (x:Foo) => {
out(JSON.stringify(x));
x.greeting();
return x;
}
let alice = fooTest(new Foo('Alice', 42));
let bob = lib.clone(alice);
bob.name = 'Bob';
fooTest(bob);
fooTest(alice);
line();
const ut = ["Bob", "Alice", "Bob", "Chuck", "bob", "Dave"];
out(`uniq([${ut}]) = ${lib.uniq(ut)}`);
out(`uniq([${ut}], x=>x.toLowerCase()) = ${lib.uniq(ut, x=>x.toLowerCase())}`);
line();
Licence
MIT