advmap
[...].map()
that supports skip, limit, step and more
Installation
As npm package
npm i -S advmap
Importing the module. It will automatically add a method to the array prototype.
;// or;
Table of Contents
Configuration
Skip
skip
controls how many items to skip before returning results
Example:
const array = 1 2 3 4 5;console; // [3, 4, 5]
Limit
limit
controls the maximum number of items returned
Example:
const array = 1 2 3 4 5;console; // [1, 2]
it can be nicely combined with the skip
property to create a pagination
const array = 1 2 3 4 5;console; // [3, 4]
Step
controls the interval between two adjacent elements
const array = 1 2 3 4 5;console; // [1, 3, 5] // if step is bigger than the array length it returns only the first elementconst array = 1 2 3 4 5;console; // [1]
it also provides an additional index parameter that is the actual array index that is being mapped
1 2 3 4 5;/*last parameter is where the item (e) is located in the array[1, 0, 0][3, 1, 2][5, 2, 4]*/1 2 3 4 5;/*If step is set to 1 (default) index parameters will be the same[1, 0, 0][2, 1, 1][3, 2, 2][4, 3, 3][5, 4, 4]*/
Previous params
adds a number of fixed parameters to the advmap
method, before the current element
1 2 3 4 5;/*[undefined, undefined, 1][undefined, 1, 2][1, 2, 3][2, 3, 4][3, 4, 5]*/
elements that are outsite of the array are undefined
Next params
adds a number of fixed parameters to the advmap
method, after the current element
1 2 3 4 5;/*[1, 2, 3][2, 3, 4][3, 4, 5][4, 5, undefined][5, undefined, undefined]*/
elements that are outsite of the array are undefined
Previous and next params combined
1 2 3 4 5;/*[undefined, 1, 2, 3][1, 2, 3, 4][2, 3, 4, 5][3, 4, 5, undefined][4, 5, undefined, undefined]*/
Filtering
advmap provides an additional parameter that can be used to check if the current element, index etc.. respects a particular condition
1 2 3 4 5;// [ '3 apples' ]
it also has all the arguments that the main map function has
0 2 3 1 5;// [ '1 is between two bigger numbers' ]
Examples
[].map
function
Simple usage like the native const array = 1 2 3 4;console; // [2,3,4,5]
Generate the next number in a fibonacci sequence
let array = 1 1;const nextNumber = array;array = ; // [1, 2]array = ; // [2, 3]array = ; // [3, 5]array = ; // [5, 8]array = ; // [8, 13]