simple typescript utility library
$ npm install helpda
or
$ yarn add helpda
import { sum, map, reduce, sumArr } from 'helpda'
map([1, 2, 3], (x) => x + 1) //[2,3,4]
reduce(sum, 0, [1, 2, 3]) // 6
sumArr([2, 4, 6]) // 12
Type | Action | Function |
---|---|---|
Array | change every value | map |
compute based on custom function and output the final value | reduce | |
flatten return a one-dimensional array, remove nesting | flatten | |
Object | change every value | map |
Functions | memoize | memoize |
revers function args | flip | |
Logic | ||
Math | sum two numbers | sum |
subtract two numbers | subtract | |
multiple two numbers | multiple | |
divide two numbers | divide | |
increment number | inc | |
decrement number | dec | |
negate number | negate | |
get random number between two args | randomN | |
sum all number in array | sumArr |
Map each element of array using a passed callback function.
import { map } from 'helpda'
const array = [1, 2, 3];
const mutiple = x => x * 2;
map(array, mutiple) //[2, 4, 6]
Reduce compute based on custom function and output the final value.
import { reduce } from 'helpda'
const array = [1, 2, 3];
const sum = (a, b) => a + b
reduce(sum, 0, array) // 6
flatten return a new one-level array from nested array
import { flatten } from 'helpda'
const array = [1, 2, 3, [4, 5, [6]]];
flatten(array)) // [1, 2, 3, 4, 5, 6]
return a new function with two reversed args
import { flip } from 'helpda'
const divide = (a, b) = a / b)
const flipped = flip((a, b) => a / b);
divide(12, 3) // 4
flipped(12, 3) // 0.25
sum two numbers
import { sum } from 'helpda'
sum(2, 2) // 4
subtract second arg from first
import { subtract } from 'helpda'
subtract(4, 2) // 2
multiple two numbers
import { multiple } from 'helpda'
multiple(2, 3) // 6
divide two numbers
import { divide } from 'helpda'
divide(12, 3) // 4
increment number
import { inc } from 'helpda'
inc(13) // 14
decrement number
import { dec } from 'helpda'
dec(13) // 12
negate number
import { dec } from 'helpda'
negate(13) // -13
sum all number in an array of numbers
import { sumArr } from 'helpda'
const array = [1, 2, 3, 4, 5];
sumArr(array) // 15
get random number
import { randomN } from 'helpda'
const min = 0;
const max = 100;
randomN(min, max) // 12..85..3..?