nnmap (No nulls map)
A function for selectively mapping through arrays
- Just return null to exclude values from map
- Quit from map at any time
No dependencies
Usage:
function returnIfEven(n: any): any | null {
return (n % 2 === 0) ? n : null;
}
function quitIfThree(n: any): boolean {
if (n === 3) return true;
return false;
}
function quitOnThirdItem(n: any, index: number): boolean {
if (index >= 3) return true;
return false;
}
const evenValues = nnmap([1, 2, 3, 4, 5, 6], returnIfEven);
const quitWhenThree = nnmap([1, 2, 3, 4, 5]), (n) => n, quitIfThree);
const firstThreeMappedItems = nnmap([1, 2, 3, 4, 5]), (n) => n, quitOnThirdItem);
API
Parameters
Name | type | Description |
---|
arr | any[] | The array to be mapped |
cb | Callback | The mapping callback function |
qcb? | QuitCallback | If this callback returns true, nnmap quits immediately |
const mappedArray = nnmap([]), (n) => n, (n, i) => null);
cb: Callback
Whatever this callback returns is inserted in the resulting array
Parameters
Name | type | Description |
---|
currentItem | any | The current array item to map to a new value |
(item) => item.foo
qcb?: QuitCallback
When this callback returns true, nnmap quits immediately and returns an array with the
values mapped up to this point
Name | type | Description |
---|
currentItem | any[] | The current array item to map to a new value |
currentIndex | number | The current index being mapped |
(item) => item.quit === true;
(item, index) => index > 10;