obj-each-break
v0.1.14
Published
each, map, filter, reduce and reverse version with break and return emulation adapted to objects
Downloads
582
Readme
obj-each-break
Small utility library implementing each
, map
, filter
, and their reverse versions for
Arrays and Objects on all own properties not just integer indexed ones. Uses for-each-break
to
create break and return emulation as if it was a regular for
or while
loop.
To simulate break return BREAK
or RETURN
, to simulate return
return BREAK(value)
or
RETURN(value)
both are equivalent but the one communicating the right intent should be used.
The functions assume this
is an array or object so they should be invoked with
.call(arrayLike, callback)
defines:
eachProp
- invoke callback for each own property, order looping over the properties is not definedhasOwnProperties
- true if has any own non-undefined propertiesarrayObjectKeys
- collect array like indexed values and keys from both objects and arrays returns{ arrayValues: [], objectKeys: [], }
wherearrayValues
is an array of property values for integer>=0 props and objectKeys other own property keyseachDir
- property looping where direction is provided as an argumenteach
- invoke callback for each own property, first array index like properties in increasing index order then own properties in natural sort ordereachRight
- invoke callback for each own property, first own properties in reverse natural sort order then array index like properties in decreasing index ordercloneArrayObject
- clone array or object with all own propertiesdeleteItems
- delete items in an array, arguments are deep flattenedarrayLength
- return array like length for an object or arraydeepClone
- deep clone array or objectmergeDefaults
- merge defaults into object propertiescopyFiltered
- copy properties chosen by filtercopyFilteredNot
- copy properties not chosen by filterobjFiltered
- filter for object/array own properties, not only integer indexed properties, result is object/array depending on this for callobjFilter
- filter for object/array own properties, not only integer indexed properties, result is an arrayobjMapped
- object map for object/array own properties, not only integer indexed properties, result is object/array depending on this for callobjMap
- map for object/array own properties, not only integer indexed properties, result is arrayobjSome
- some for object/array own propertiesobjEvery
- some for object/array own propertiesobjReduceIterated
- reduce taking iteration functions and callbackobjReduce
- reduce for object/array own properties, order of looping over properties not definedobjReduceLeft
- reduce for object/array own properties, order of looping over properties is same aseach
objReduceRight
- reduce for object/array own properties, order of looping over properties same aseachRight
For convenience for-each-break
module's exports are re-exported:
BREAK
- used to break or return from loop, usingBREAK
to break out of loop is preferred since it communicates clear intentionRETURN
- used to break or return from loop, usingRETURN
to break out of loop and return a value is preferred since it communicates clear intentionfilter
- use to filter array like object, with early break or return:filter.call(arrayLike, callback, thisArg)
forEach
- use to loop over array like object:forEach.call(arrayLike, callback, thisArg, defaultReturn)
map
- use to map array like object, with early break or return:map.call(arrayLike, callback, thisArg)
filterRight
- use to filter array like object in reverse, with early break or return:filter.call(arrayLike, callback, thisArg)
forEachRight
- use to loop over array like object in reverse:forEach.call(arrayLike, callback, thisArg, defaultReturn)
mapRight
- use to map array like object in reverse, with early break or return:map.call(arrayLike, callback, thisArg)
Install
Use npm to install.
npm install obj-each-break --save
Usage
License
MIT, see LICENSE.md for details.