utils 1.0.75
Install from the command line:
Learn more about npm packages
$ npm install @bhoos/utils@1.0.75
Install via package.json:
"@bhoos/utils": "1.0.75"
About this version
This library is a collection of well tested utility functions that is used in many projects across Bhoos (both server and mobile).
Note that because it is to beused in mobile (i.e. react native) applications you are restricted from using fs
or any other server only package.
Utility functions with a functional programming flavour
- partition : partition a list into two using a predicate
- removeDuplicates: remove duplicate entries from a list
- topologicalSort : find topological sort of a graph (represented as a list of all nodes, and a function that returns children nodes of a given node)
Creating a Normalized object from an array allows efficient lookups by id
, and also gives other utility methods.
const normalized = Normalized<string>([{id: 'a', val:'obj1'}, {id:'b', val:'obj2'}])
normalized.get('a').val
// => 'obj1'
Other utility methods given by Normalized is:
- filter
- getAt
- replace
- concat
Normalized can be serialized a string and recovered as follows:
Normalized.fromObj(JSON.parse(JSON.stringify(normalized)))
// => normalized
String manipulation
- prefixp : check a string for a given prefix.
p
stands for predicate (i.e. functions that return boolean) - removePrefix: remove a prefix from string if possible otherwise return undefined
Trim number to a max 4 digits with decimal place inbetween and suffixed by 'K', 'M', 'B', 'T' values.
trimNumber(number: number)
Return a number with ordinal suffix added.
addOrdinal(21)
// => '21st'
Format a number by adding commas with trailing zeros
formatNumber(10000, 2)
// => "10,000.00"
Convert utf-8 source string to an optimized uint8 (binary) array.
strToUint8(source: string)
Convert uint8 (binary) array created by strToUint8
to the original
source string
uint8ToStr(buffer: Uint8Array)
Generate a numeric checksum value for the given input string
checksum(src: string): number
A Fisher-Yates algorithm based array shuffler.
Performs an in-place shuffling and returns the same input array.
shuffle<T>(array: T[]): T[]
array
: An array of any type
Promise that resolve after n
milliseconds
await delay(1000);
// waits for 1 seconds
Memoise a function
- Write tests for the utilities you add. See any of the
__tests__/*.spec.ts
file for example. - Also, run
yarn coverage
and ensure that you have 100% coverage on the code you add.