1.1.0 • Published 8 years ago

array-filter-n v1.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

array-filter-n

The filterN() method creates a new array with the first N elements that pass the test implemented by the provided function. Fast!

Syntax

filterN(inputArr, N, checkFn[, thisArg])

The checkFn is the standard EcmaScript function (elem, index, array)

thisArg if supplied is set as the this context for checkFn.

Usage

var filterN = require('array-filter-n');

var filter4even = 
    filterN([12, 8, 3, 1, 0, 4, 7, 9, 12], 4, function(i) { return !i%2});
//=> [12, 8, 0, 4]

// works also on strings
var filter3nums = 
    filterN('n0d3_R0ck5', 3, function(c) { return !isNaN(parseInt(c)); });
//=> [0, 3, 0]

// Fastened safety belts
var first4pallindromes = 
    filterN(['aat', 'aba', 'pop'], 5, function (s) { 
        s == s.split('').reverse().join(''); 
    });
//=> ['aba', 'pop']

var crazy = filterN([1,2,3,4], -3);
//=> []

var notSoCrazy = filterN([1,2,3,4], 3);
//=> [1, 2, 3]
1.1.0

8 years ago

1.0.8

8 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.2

9 years ago

1.0.2222

9 years ago

1.0.3

9 years ago

1.0.333

9 years ago