NOTE: The functions can be used in JavaScript but I do not recommend it.
import {
arrayIfy,
limitArray,
limitArrayify,
filterMap,
removeAll,
takeUntil,
waitUntil,
} from 'tyvn';
// arrayIfy:
const data: Set<string> = new Set();
data.add('hello');
data.add('world');
console.log(arrayIfy<Set<string>, string>(data)); // ['hello', 'world'];
// limitArray (known as chunk)
console.log(limitArray<string>(arrayIfy<Set<string>, string>(data), 1)); // [ [ 'hello' ], [ 'world' ] ]
// limitArrayify (convenience functions combining limitArray and arrayIfy)
console.log(limitArrayify<Set<string>, string>(data, 1)); // [ ['hello'], ['world'] ]
// filterMap
console.log(
filterMap<string, string>(
['a', 'th', 'is', 'wi', 'll', 'display'],
(accept, deny, value) => {
if (value.length == 2) {
return accept(value);
} else return deny();
}
)
); // ['th', 'is', 'wi', 'll'];
// removeAll
console.log(removeAll<string>(['hello', 'world', 'hello'], 'hello')); // ['world'];
// takeUntil
console.log(
takeUntil<string>(
['hello', 'world', 'this', 'will', 'not', 'be', 'displayed'],
(value: string, index: number) => index < 2
)
); // ['hello', 'world']
// waitUntil
console.log(
waitUntil<string>(
['hello', 'world', 'this', 'will', 'not', 'be', 'displayed'],
(value: string, index: number) => index > 2
)
); // ['hello', 'world', 'this']
More info:
arrayIfy: Turn Objects / Class' / other stuff into an Array.
limitArray: "Chunk" an array into smaller arrays with a maximum length.
limitArrayify: Convenience function. Joins limitArray and arrayIfy togethor.
filterMap: Filter & map merged into one function.
(accept, deny, value, index, array)
removeAll: Remove all elements from an Array that are equal to the specified param.
takeUntil: Map through an Array until the specified function returns false.
(value, index, array)
waitUntil: Map through an Array until the specified function returns true.
(value, index, array)
import { addProp } from 'tyvn';
addProp<{ one: number; two: number }>(['one', 'two'], 5, { one: 1, two: 2 }); // 3
addProp<{ one: number; two: number }>('one', 5, { one: 1, two: 2 }); // 1
addProp<{ one: number; two: number }>(['one', 'two'], 5, {
one: 1,
two: 'Hello world!',
}); // 6 (you'll also be screamed as two should be a number!)
addProp<{ one: number; two: number }>(
['one', 'two'],
5,
0,
{ one: 6, two: 3 },
{ one: 9, two: 12 }
); // 30
More info:
addProp: Add properties togethor on objects inputted that will calculate a final sum. Supports default values.
Generated using TypeDoc, the 2/8/2021 at 8:46:57 AM