Skip to content

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

utils

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.

fp

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)

Normalized

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

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

trimNumber

Trim number to a max 4 digits with decimal place inbetween and suffixed by 'K', 'M', 'B', 'T' values.

trimNumber(number: number)

addOrdinal

Return a number with ordinal suffix added.

addOrdinal(21) // => '21st'

formatNumber

Format a number by adding commas with trailing zeros

formatNumber(10000, 2) // => "10,000.00"

strToUint8

Convert utf-8 source string to an optimized uint8 (binary) array.

strToUint8(source: string)

uint8ToStr

Convert uint8 (binary) array created by strToUint8 to the original source string

uint8ToStr(buffer: Uint8Array)

checksum

Generate a numeric checksum value for the given input string

checksum(src: string): number

shuffle

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

delay

Promise that resolve after n milliseconds

await delay(1000); // waits for 1 seconds

createMemo

Memoise a function

For Developers

Tests

  • 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.

Details


Assets

  • utils-1.0.75.tgz

Download activity

  • Total downloads 2,286
  • Last 30 days 9
  • Last week 3
  • Today 0