@kennethober/helpers v0.0.2
Helpers
A module of helper functions.
The guiding principle of this module is to provide functions that are consistently useful, have no external dependencies, encourage good coding, and don't encourage forgetting how to use Javascript.
Installation
npm install @kennethober/helpers
Usage
// Browser
import * as H from './path/to/node_modules/@kennethober/helpers/index.js'
import { partial as p } from './path/to/node_modules/@kennethober/helpers/index.js'
// Node.js
import * as H from '@kennethober/helpers'
import { partial as p } from '@kennethober/helpers'
const add = (x, y) => x + y
const add1 = H.partial(add, 1)
const add2 = p(add, 2)
Note: if using with Node.js, import either from an .mjs
file, or from a package with "type": "module"
in package.json
.
Development
git clone https://github.com/kennethober/helpers.git
npm install
npm run lint
npm test
npm run docs
Exported functions
Table of Contents
partial
Partially apply a function.
Parameters
fn
function Function to partially apply.args
...any Arguments to apply.
Returns function Function with the provided arguments applied.
partialOb
Like partial(), but partially apply a function with a the properties of a single options-object.
Parameters
fn
function Function to partially apply.ob
Object Options-object arguments to apply; individual properties will be overwritten if the same keys are later passed in a new options object to the partially applied function.
Returns function Function with the provided options-object argument applied.
collect
Collect values with the same key and return an object with a one-to-many key-value relationship.
Parameters
Returns Object<(string | symbol), Array\> Object mapping keys to arrays of values.
collectUnique
Like collect(), but eliminate duplicate values (based on set behavior).
Parameters
Returns Object<(string | symbol), set\> Object mapping keys to sets of unique values.
chooseN
Return N randomly chosen elements from an array.
Parameters
Returns Array Array of chosen elements.
logTime
Call a function a number of times and log the total duration to the console.
Parameters
times
number The number of times to call a given function (synchronously, with await); useful for amplifying differences in execution time between functions.fn
function The function to call; may be synchronous or asynchronous.timerId
string An id/label for the timer; shows up in the log. (optional, default`${Date.now()}-${Math.random()}`
)
Returns undefined Function called for side-effect only.