array-helper-functions
v1.4.0
Published
Lightweight Array helper methods
Downloads
6
Maintainers
Readme
Array Helpers
Some helpful array methods. Spent sometime using Underscore in a previous project so I decided to build a helper library from scratch using some ideas behind this library. :honeybee:
Installation
This package is distributed via npm:
npm install --save-dev array-helper-functions
// Global install
npm install -g array-helper-functions
API overview
- filter
- flatten
- compact
- partial
- every
- object
- map
Usage
const arrayHelper = require('array-helper-functions');
filter
[array, filterItem, boolean]
let filter = arrayHelper.filter([1, 3, 100, 50], 50); // => [1, 3, 100];
Boolean: true
Filters deep nested arrays.
let filter = arrayHelper.filter([1, 3, 100, [[[50]]], 50, true); // => [1, 3, 100];
flatten
[array, boolean]
let flatten = arrayHelper.flatten([1, 3, 100, [50]]); // => [1, 3, 100, 50];
Boolean: true
Flattens deep nested arrays.
let flatten = arrayHelper.flatten([1, 3, 100, [[[50]]], true); // => [1, 3, 100, 50];
compact
[array]
Removes Falsy values from array set.
let compact = arrayHelper.compact([1, 2, 3, 100, null, 0]); // => [1, 2, 3, 100];
partial
[func, array]
Invokes a given function with the provided array set passed.
const foo = (a, b, c) => {
return a + b + b;
};
let partial = arrayHelper.partial(foo, [1, 2, 3]); // Invokes method `foo` with given arguments. => 6
every
[func, array]
Executes the provided callback function once for each element present in the array until it finds one where the callback returns a falsy value.
const isFooBigger = (element) => {
return element >= 5;
};
let every = arrayHelper.every(isFooBigger, [1, 2, 3]); // Returns false.
let every = arrayHelper.every(isFooBigger, [10, 5, 7]); // Returns true.
object
[array, list]
Converts arrays to objects.
let object = arrayHelper.object([1, 2, 3]); // => {'0': 1, '1': 2, '2': 3}
let object = arrayHelper.object(['foo', 'baz', 'fizz']); // => { '0': 'foo', '1': 'baz', '2': 'fizz' }
### `map` [array]
Returns a new array from a tranformation applied by a function.
const multiply = (num) => {
return num * 2;
};
let map = arrayHelper.map([1, 2, 3], multiply); // => [2, 4, 6]
Project Setup
This project assumes you have NodeJS v6 or greater installed. You should also have npm v3 or greater installed as well (this comes packaged with Node 6). You'll also need a recent version of git installed as well.
To get started with the project, start with this:
- Sign up for a GitHub Account (if you don't already have one)
- Fork this repo
- Clone your fork
- Require the module (see
Usage
above) and get started!
If you get any failures at this point something is wrong and needs to be fixed. Remember, Google and StackOverflow are your friends.. :raised_hands:
Contributing
If you'd like to play around with this library yourself please follow the steps below:
- Clone the project from Github and
cd /your-path/array-helper-functions
folder - Run:
npm run setup
- This will runnpm install
&npm run validate
: lint, build & test configurations for you ensuring everything is working as expected.
Other
This library was developed by me as a fun little project.