Skip to content

js-lib

Standard library for universal (browser + Node.js) javascript

npmmin.gz sizeActionsloc

MaintainabilityTest Coveragecode style: prettier

Design

Inspired by Lodash, bluebird, promise-fun and other useful small packages.

Designed to play well with the rest of opinionated "Natural Cycles JS Platform" (link pending). This package is the lowest-level production dependency (not devDependency) of the Platform. Almost everything else depends on it.

All functions in this package are exported in index.ts (flat), no namespacing is used. So, to avoid conflicts and "global import namespace" pollution , all functions are prefixed with an underscore (e.g _.pick becomes _pick), with some exceptions (later). Promise functions are prefixed with p, e.g pMap.

Decorators are _prefixed and PascalCased (e.g @_Debounce). _is to be consistent with other naming in this package. PascalCase is to distinguish decorators from similar functions that are not decorators. Example:\_debounceis a function (lodash-based),\_Debounceis a decorator (used as@\_Debounce). PascalCase convention follows Angular/Ionic convention (but doesn't follow TypeScript documentation convention; we had to pick one).

Interfaces and Classes are named as usual (no prefix, PascalCase, e.g AppError).

Q: Why not just use lodash?

A:

  • We believe Lodash is outdated (many functions are pre-ES6 / obsolete by ES6).
  • Because it has so many outdated functions - its size is bigger, and solutions to tree-shake exist, but complicated.
  • First-class TypeScript support (all code in this repo is TypeScript).

This package is intended to be 0-dependency (exception: tslib from TypeScript), "not bloated", tree-shakeable. Supported by reasonably modern Browsers and Node.js latest LTS.

To fulfil that requirement it exports ESM version (for Browsers) as es2017.

Exports default CJS version for Node as es2019 (with native async/await, for better performance, async stack-traces, etc).

Mutation

All function does NOT mutate the arguments by default.

Many functions support "mutation flag", which can be set to true to perform a mutation.

For example:

ts
const obj = { a: 'a', b: 'b' }

// Non-mutating (default)
const obj2 = _pick(obj, ['a'])
// { a: 'a' }

// Mutating (opt-in)
_pick(obj, ['a'], true)
// obj was mutated

Highlights