/utils

Shared Utils for Dust Foundation projects

Primary LanguageTypeScriptMIT LicenseMIT

@dustfoundation/utils

CI NPM Version Minimum Node.js Version

Shared Utils for Dust Foundation projects.

Installation

npm install --save @dustfoundation/utils

Features

Util: Build Domain

Builds custom domain for dust.foundation. Acceptable stages: dev, stage, prod.

buildDomain('dev');
// => https://dev.dust.foundation
buildDomain('prod');
// => https://dust.foundation
buildDomain('dev', 'content');
// => https://content.dev.dust.foundation
buildDomain('dev', 'content', 'ws://');
// => ws://content.dev.dust.foundation
buildDomain('###');
// => throws Error

Array: Chunk

Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements. If chunk size is 0, throws error.

chunk([1, 2, 3, 4, 5], 3);
// => [[1, 2, 3], [4, 5]]

Array: Group By

Creates an object composed of keys generated by taking given key from each array element. The order of grouped values is determined by the order they occur in array. The corresponding value of each key is an array of elements responsible for generating the key.

groupBy([
  { symbol: 'BTC', price: 1 },
  { symbol: 'ETH', price: 2 },
  { symbol: 'ETH', price: 3 },
  { symbol: 'BTC', price: 4 },
  { bad: true },
], 'symbol');
// => { BTC: [1, 4], ETH: [2, 3] }

Object: Omit

Creates an object composed of the own and inherited enumerable property paths of object that are not omitted.

omit({
  name: 'Bitcoin',
  symbol: 'BTC',
  price: 50000,
}, 'name', 'price');
// => { symbol: 'BTC' }

Object: Replace Nullish

Creates an object with same properties but all null and undefined values are replaced by the given value. If deep enabled, works recursively (including arrays).

// deep: false (default)
replaceNullish({
  key: 'value',
  _null: null,
  _undefined: undefined,
}, 'any_custom_value');
// => { key: 'value', _null: 'any_custom_value', _undefined: 'any_custom_value' }

// deep: true
replaceNullish({
  key: 'value',
  nested: {
    _null: null,
    _undefined: undefined,
  },
}, 'any_custom_value', true);
// => { key: 'value', nested: { _null: 'any_custom_value', _undefined: 'any_custom_value' } }