identitate
A tiny custom identity function creator for composable softwares.
Table of contents
Usage
; // use built-in methodsconsole; // 'first'console; // 'second'console; // 'fifth'console; // 'fourth' // or create your ownconst identityThird = ;const identityThirdLast = ;const identityFirstNested = ; console; // 'third'console; // 'third'console; // 'value'
Pre-built methods
identity
returns first argument passed to it
The classic identity function:
console; // 'first'
identitySecond
returns second argument passed to it
console; // 'second'
Example usage when creating meta
properties with the popular redux-actions
package:
;; const doThing = ; console; // {meta: 'meta', payload: 'payload', type: 'DO_THING'}
identityLast
returns last argument passed to it, regardless of total number of arguments
console; // 'fifth'console; // 'third'
identitySecondLast
returns second-to-last argument passed to it, regardless of total number of arguments
console; // 'fourth'console; // 'second'
Custom methods
createIdentity
creates a new identity method based on the parameters passed
createIdentity(position: number[, path: (Array<number|string>|number|string)]): any
// use a positive number to get the index of the arguments (zero-indexed)const identityFourth = ; console; // 'fourth' // use a negative number to get the index of the arguments relative to the lastconst identityThirdLast = ; console; // 'third' // include a path to get the deeply-nested value of that argumentconst identityNested = ; console; // '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 developmentdist
file with NODE_ENV=developmentbuild:minified
=> run webpack to build productiondist
file with NODE_ENV=productiondev
=> run webpack dev server to run example app / playgrounddist
=> runsbuild
andbuild-minified
lint
=> run ESLint against all files in thesrc
folderprepublish
=> runscompile-for-publish
prepublish:compile
=> runlint
,test:coverage
,transpile:es
,transpile:lib
,dist
test
=> run AVA test functions withNODE_ENV=test
test:coverage
=> runtest
but withnyc
for coverage checkertest:watch
=> runtest
, but with persistent watchertranspile:lib
=> run babel against all files insrc
to create files inlib
transpile:es
=> run babel against all files insrc
to create files ines
, preserving ES2015 modules (forpkg.module
)