arrayfilter
v1.1.2
Published
Array filtering utility.
Downloads
2,697
Readme
arrayfilter
Array filtering utility.
Installation
npm install arrayfilter --save
Usage
- Accepting By Type
- Accepting By Pattern
- Rejecting Empty Entries
- Rejecting Duplicated Entries
- Rejecting By Pattern
- Rejecting By Type
Accepting By Type
arrayfilter.typeAccept(type)
create a function which accepts entries by type matching.
"use strict";
const arrayfilter = require('arrayfilter');
// Define a filter function.
let acceptString = arrayfilter.typeAccept('string');
// Execute filtering.
let values = ['foo', {}, 123].filter(acceptString);
console.log(values); // -> ['foo']
Accepting By Pattern
arrayfilter.patternAccept(pattern)
create a function which accepts entries by regex (or string) matching.
"use strict";
const arrayfilter = require('arrayfilter');
// Define a filter function.
let acceptFo = arrayfilter.patternAccept(/^fo/);
// Execute filtering.
let values = ['foo', 'bar', 'baz'].filter(acceptFo);
console.log(values); // -> ['foo']
Rejecting Empty Entries
arrayfilter.emptyReject()
create a function which rejects empty with rejects null, undefined and empty string.
"use strict";
const arrayfilter = require('arrayfilter');
// Define a filter function.
let rejectEmpty = arrayfilter.emptyReject();
// Execute filtering.
let values = ['foo', '', null, 'bar', undefined].filter(rejectEmpty);
console.log(values); // -> ['foo', 'bar']
Rejecting Duplicated Entries
arrayfilter.duplicateReject()
create a function which rejects empty with rejects null, undefined and empty string.
"use strict";
const arrayfilter = require('arrayfilter');
// Define a filter function.
let duplicateReject = arrayfilter.duplicateReject();
// Execute filtering.
let values = ['foo', 'bar', 'foo'].filter(duplicateReject);
console.log(values); // -> ['foo', 'bar']
Rejecting By Pattern
arrayfilter.patternReject(pattern)
create a function which rejects entries by regex (or string) matching.
"use strict";
const arrayfilter = require('arrayfilter');
// Define a filter function.
let rejectFo = arrayfilter.patternReject(/^fo/);
// Execute filtering.
let values = ['foo', 'bar', 'baz'].filter(rejectFo);
console.log(values); // -> ['bar', 'baz']
Rejecting By Type
arrayfilter.typeReject(type)
create a function which rejects entries by type matching.
"use strict";
const arrayfilter = require('arrayfilter');
// Define a filter function.
let rejectString = arrayfilter.typeReject('object');
// Execute filtering.
let values = ['foo', {}, {}].filter(rejectString);
console.log(values); // -> ['foo']
License
This software is released under the MIT License.