identitate
A tiny custom identity function creator for composable softwares.
Table of contents
Usage
import {createIdentity, identity, identitySecond, identityLast, identitySecondLast} from 'identitate';
console.log(identity('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identitySecond('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identityLast('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identitySecondLast('first', 'second', 'third', 'fourth', 'fifth'));
const identityThird = createIdentity(2);
const identityThirdLast = createIdentity(-3);
const identityFirstNested = createIdentity(0, 'deeply[0].nested');
console.log(identityThird('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identityThirdLast('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identityFirstNested({deeply: [{nested: 'value'}]}, 'second', 'third', 'fourth', 'fifth'));
Pre-built methods
identity
returns first argument passed to it
The classic identity function:
console.log(identity('first', 'second', 'third', 'fourth', 'fifth'));
identitySecond
returns second argument passed to it
console.log(identitySecond('first', 'second', 'third', 'fourth', 'fifth'));
Example usage when creating meta
properties with the popular redux-actions
package:
import {identity, identitySecond} from 'identitate';
import {createAction} from 'redux-actions';
export const doThing = createAction('DO_THING', identity, identitySecond);
console.log(doThing('payload', 'meta'));
identityLast
returns last argument passed to it, regardless of total number of arguments
console.log(identityLast('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identityLast('first', 'second', 'third'));
identitySecondLast
returns second-to-last argument passed to it, regardless of total number of arguments
console.log(identityLast('first', 'second', 'third', 'fourth', 'fifth'));
console.log(identityLast('first', 'second', 'third'));
Custom methods
createIdentity
creates a new identity method based on the parameters passed
createIdentity(position: number[, path: (Array<number|string>|number|string)]): any
const identityFourth = createIdentity(3);
console.log(identityLast('first', 'second', 'third', 'fourth', 'fifth'));
const identityThirdLast = createidentity(-3);
console.log(identityLast('first', 'second', 'third', 'fourth', 'fifth'));
const identityNested = createIdentity(0, 'deeply.nested');
console.log(identityNested({deeply: {nested: 'value'}}));
The path
parameter uses pathington under the hood for path parsing, so check there for valid values.
Browser support
- Chrome (all versions)
- Firefox (all versions)
- Edge (all versions)
- Opera 15+
- IE 9+
- Safari 6+
- iOS 8+
- Android 4+
Development
Standard stuff, clone the repo and npm install
dependencies. The npm scripts available:
build
=> run webpack to build development dist
file with NODE_ENV=developmentbuild:minified
=> run webpack to build production dist
file with NODE_ENV=productiondev
=> run webpack dev server to run example app / playgrounddist
=> runs build
and build-minified
lint
=> run ESLint against all files in the src
folderprepublish
=> runs compile-for-publish
prepublish:compile
=> run lint
, test:coverage
, transpile:es
, transpile:lib
, dist
test
=> run AVA test functions with NODE_ENV=test
test:coverage
=> run test
but with nyc
for coverage checkertest:watch
=> run test
, but with persistent watchertranspile:lib
=> run babel against all files in src
to create files in lib
transpile:es
=> run babel against all files in src
to create files in es
, preserving ES2015 modules (for
pkg.module
)