Set of simple callback functions for daily work with arrays
npm install @radmen/array-utilsfilter, reduce) which I use quite often.``js
import {excludeEmpty} from '@radmen/array-utils';
const values = [0, 1, '', null];
values.filter(excludeEmpty()); // [1]
`
`js
import {replaceWith} from '@radmen/array-utils';
const values = ['foo', 'bar'];
values.map(replaceWith(item => item === 'foo', 'bar')); // ['bar', 'bar'];
`
Second argument may be a callback which will provide data for replacement:
`js${item}Bar
values.map(replaceWith(filterByValue('foo'), item => )); // ['fooBar', 'bar'];`
`js
import {extendWith} from '@radmen/array-utils'
const values = [
{name: 'foo'},
{name: 'bar'},
];
values.map(extendWith(item => item.name === 'foo', { active: true }); // [{name: 'foo', active: true}, {name: 'bar'}];
`
`js
import {pushToBottom} from '@radmen/array-utils';
const values = ['foo', 'bar', 'baz'];
values.sort(pushToBottom(item => item === 'foo')); // ['bar', 'baz', 'foo'];
`
`js
import {pushToTop} from '@radmen/array-utils';
const values = ['foo', 'bar', 'baz'];
values.sort(pushToTop(item => item === 'baz')); // ['baz', 'foo', 'bar'];
`
`js
import {filterByValue} from '@radmen/array-utils';
const values = ['foo', 'bar', 'baz'];
values.filter(filterByValue('foo')); // ['foo'];
`
Function accepts more then one value:
`js`
values.filter(filterByValue('foo', 'bar')); // ['foo', 'bar'];
`js
import {filterByKeyValue} from '@radmen/array-utils';
const values = [{name: 'foo'}, {name: 'bar'}, {name: 'baz'}];
values.filter(filterByKeyValue('name', 'bar')); // [{name: 'bar'}];
`
Function accepts more then one value:
`js`
values.filter(filterByKeyValue('name', 'bar', 'foo')); // [{name: 'foo'}, {name: 'bar'}];
`js
import {collapse} from '@radmen/array-utils';
const values = [[1, 2], [3, 4], [5]];
values.reduce(collapse()); // [1, 2, 3, 4, 5];
``