type-filter v4.0.0
type-filter (4.0.0)
typeFilter([ value ] [, handler | handlerList | typeHandler ] [, options ] [, ...otherArguments ])
valueis any typehandleris a functionhandlerListis an arraytypeHandleris an objectoptionsis boolean or function or objectotherArgumentsare any typesfirst argument: get type
second argument: handler | handlerList | typeHandler
third argument: once | options
other arguments: other arguments
links: npm |
github
start: install | import
default handlers: no | yes | on, off | type | typeClass | call | recheck | callRecheck | error | map | handler
install
npm i type-filterimport
import typeFilter, { handler } from 'type-filter'or
import { typeFilter, handler } from 'type-filter'to minimize code size use this importing
import typeFilter from 'type-filter/typeFilter'
import handler from 'type-filter/handlers/handler'get type
Use only one argument to get a type.
typeFilter(undefined) // returns 'undefined'
typeFilter(null) // returns 'null'
typeFilter(NaN) // returns 'nan'
typeFilter(1) // returns 'number'
typeFilter('1') // returns 'string'
typeFilter(false) // returns 'boolean'
typeFilter(Symbol()) // returns 'symbol'
typeFilter(() => {}) // returns 'function'
typeFilter([]) // returns 'array'
typeFilter({}) // returns 'object'
typeFilter(Object.create(null)) // returns 'object'
typeFilter(new class {}) // returns 'class'
typeFilter(new Map()) // returns 'class'
typeFilter() // returns 'undefined'handler
If the second argument of typeFilter is a function then it will be used as a handler.
Any handler gets 2 arguments: value and options.
value equals the first argument of typeFilter.
const handler = value => value + 1
typeFilter(1, handler) // returns 2options.type equals what typeFilter returns for current value (see get type)
const type = (value, {type}) => value + ': ' + type
typeFilter(1, type) // returns '1: number'
typeFilter('1', type) // returns '1: string'options.className equals empty string for all types except for class.
If the type equals class then options.className contains name of class.
const classType = (value, {type, className}) => {
return value + ': ' + type + (className && '(' + className + ')')
}
typeFilter(1, classType) // returns '1: number'
typeFilter({}, classType) // returns '[object Object]: object'
typeFilter(new Map(), classType) // returns '[object Object]: class(Map)'
typeFilter(new class MyClass {}, classType) // returns '[object Object]: class(MyClass)'handlerList
If the second argument of typeFilter is an array then this argument is handlerList.
handlerList can contain handler, handlerList or typeHandler.
const square = value => value * value
const addOne = value => value + 1
typeFilter(1, [addOne, square]) // returns 4
typeFilter(2, [square, addOne]) // returns 5
typeFilter(1, [addOne, [square, square]]) // returns 16typeHandler
If the second argument of typeFilter is an object then this argument is typeHandler.
keys of typeHandler equals type or class name of value.
values of typeHandler can contain handler, handlerList or typeHandler.
if a typeHandler does not contain key which equals current value type or class name
then typeFilter returns this value.
const noNumber = {
number: () => {}
}
typeFilter(1, noNumber) // returns undefined
typeFilter('2', noNumber) // returns '2'const noMap = {
Map: () => {}
}
typeFilter(new Map(), noMap) // returns undefined
typeFilter(new Set(), noMap) // returns instance of Setother in typeHandler
you may use other key of typeHandler to handle other types
const onlyNumber = {
number: value => value,
other: () => {}
}
typeFilter(1, onlyNumber) // returns 1
typeFilter('2', onlyNumber) // returns undefinedonce
handler and typeHandler run only one handle but handlerList runs
each handler inside it and
each handler gets value which equals result of previous handler.
to have result of the first handler in handlerList
which returns needed result you may use once.
if the third argument (or once key of the third argument) equals true or function
then each handler gets original value.
if the third argument (or once key of the third argument)
equals true the typeFilter returns a result of the first handler
which returns some equals true (1, true, {}, [], ...).
import {yes, no, off, type} from 'type-filter' // default handlers
typeFilter(1, [no, off, type, yes]) // 1 > [no] > undefined > [off] > false > [type] > 'boolean' > [yes] > 'boolean' typeFilter(1, [no, off, type, yes], true) // 1 > [no] > undefined // 1 > [off] > false // 1 > [type] > 'number' typeFilter(1, [no, off, yes, type], true) // 1 > [no] > undefined // 1 > [off] > false // 1 > [yes] > 1if the third argument (or
oncekey of the third argument) equalsfunctionthefunctiongets result of handlerList's handler call and the typeFilter returns a result which pass thefunctiontest. to pass the test thefunctionshould returns some equalstrue(1,true,{},[], ...).const addOne = value => value + 1 const addTwo = value => value + 2 const addThree = value => value + 3
typeFilter(1, addOne, addTwo, addThree, value => value > 2) // returns 3 (the result of addTwo) // addOne and addTwo are called
if all handlers fail the test then typeFilter returns `undefined`
```javascript
typeFilter(1, [yes, on, off], () => false) // returns undefinedonce works only for handlerList
typeFilter(0, yes, true) // returns 0
typeFilter(0, [yes], true) // returns undefinedoptions
you may use the third argument as options which can contain
type, classType, once, rootHandler like the second argument of handler.
each handler get the same options object and you can share variables between handlers
const deep = (value, options) => {
const deep = options.deep || 0
options.deep = deep + 1
return value
}
const deepHandler = {
function: [deep, callRecheck],
other: (value, {deep}) => deep || 0
}
typeFilter(1, deepHandler) // returns 0
typeFilter(() => 1, deepHandler) // returns 1
typeFilter(() => () => 1, deepHandler) // returns 2
typeFilter(() => () => 1, deepHandler, {deep: 1}) // returns 3other arguments
you may provide any variables to all handlers
typeFilter(0, (value, options, ...args) => ([value, ...args]), undefined, 1, 2, 3, 4, 5)
// returns [0, 1, 2, 3, 4, 5]default handlers
you may use default handlers from this library
no
always returns undefined
const noNumber = {
number: no
}
typeFilter(1, noNumber) // returns undefined
typeFilter('2', noNumber) // returns '2'yes
always returns value
const onlyNumber = {
number: yes,
other: no
}
typeFilter(1, onlyNumber) // returns 1
typeFilter('2', onlyNumber) // returns undefinedon, off
on always returns trueoff always returns false
const isNumber = {
number: on,
other: off
}
typeFilter(1, isNumber) // returns true
typeFilter('2', isNumber) // returns falsetype
type always returns type of value
typeFilter('2', type) // returns 'string'typeClass
typeClass returns type of value or className for type equals class
typeFilter(new Map(), typeClass) // returns 'Map'
typeFilter({}, typeClass) // returns 'object'call
call always returns result of value call
typeFilter(() => 1, call) // returns 1call.args
args can provide arguments to function
typeFilter(x => x, call.args(1)) // returns 1recheck
recheck is rechecking value like this type filter gets value of handler
const isNumberHandler = {
function: [call, recheck],
number: () => 'this is number',
other: () => 'this is not number'
}
typeFilter(1, isNumberHandler) // returns 'this is number'
typeFilter('1', isNumberHandler) // returns 'this is not number'
typeFilter(() => 1, isNumberHandler) // returns 'this is number'
typeFilter(() => '1', isNumberHandler) // returns 'this is not number'
typeFilter(() => () => 1, isNumberHandler) // returns 'this is number'be careful, recheck can create an infinite loop
typeFilter(1, recheck)callRecheck
callRecheck is combine of call and recheck
callRecheck contains args property like call handler
const isNumberHandler = {
function: callRecheck,
number: () => 'this is number',
other: () => 'this is not number'
}
typeFilter(() => 1, isNumberHandler) // returns 'this is number'error
error runs exception
typeFilter(1, error('Text')) // runs throw Error('Text')you may use one of default variable (value, type, className) inside text of error's argument
// runs throw Error('value: 1, type: number, className: ')
typeFilter(1, error('value: {value}, type: {type}, className: {className}'))also you may use own properties
typeFilter(1, error('error: {custom}', {
custom: 'custom value'
})) // 'error: custom value'if the custom value is a function it will be runs
typeFilter(1, error('error: {custom}', {
custom: () => 'custom value'
})) // 'error: custom value'this custom property gets value, type, className as a handler
typeFilter(1, error('error: {custom}', {
custom: (value, {type, className}) => `value: ${value}, type: ${type}, className: ${className}`
})) // runs throw Error('error: value: 1, type: number, className: ')map
map uses a handler for each item of it like map of an array
typeFilter([
() => 0,
() => 1,
() => 2
], map(call)) // returns [0, 1, 2]handlerList also works
typeFilter([
() => 0,
() => 1,
() => 2
], map([call, v => ++v])) // returns [1, 2, 3]and of course typeHandler
typeFilter([
() => 0,
1,
'2',
() => '2'
], map({
function: callRecheck,
number: yes,
other: off
})) // returns [0, 1, false, false]handler
handler like bind method for a function but you may set up handler as default and use value later
const isNumber = typeFilter({
number: on,
other: off
}, handler)
isNumber(1) // returns true
isNumber('1') // returns falseyou may use the handler anywhere like other handlers
const getFilter = value => typeFilter(value, {
array: handler,
function: handler,
object: handler,
other: error('handler has wrong type which equals {type}')
})
const isNumber = getFilter({
number: on,
other: off
})
isNumber(1) // true
isNumber('1') // false
getFilter(1) // error: handler has wrong type which equals numberchange list
4.0.0
- removed
arrayhandler - added other arguments functionality
- added
callRecheckto handlers
3.5.1
- updated version of
jest
3.5.0
- deprecated
arrayhandler - added
maphandler
3.4.0
typeFilter({...}, handler /* , options */)handlerdoes not supportoptions- custom handlers support
options
const myHandler = typeFilter({...}, handler)
const value = myHandler('test', {...})3.3.0
- fixed import bug for babel
- added
arrayhandler
3.2.2
call handler has args property
3.2.0
added callRecheck handler
3.1.0
now all options in handlers are the same object which you pass to the third argument of typeFilter
3.0.0
now the second argument of any handler is object which contains
type, className, handler, once, rootHandler
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago