way2web-helpers

    0.2.0 • Public • Published

    Way 2 Help

    NPM version Downloads

    Array helpers, so you can get very fast your data from the array.

    here an example array.

    var exampleArray = [
        {
            id: 0,
            name: 'John',
            age: 93,
            city: ['Patmos', 'Rome'],
        },
        {
            id: 1,
            name: 'Peter',
            age: 62,
            city: 'Rome',
        },
        {
            id: 2,
            name: 'Luke',
            age: 84,
            city: 'Boeotia'
        },
        {
            id: 2,
            name: 'Paul',
            age: 62,
            city: 'Rome'
        },
    ];
    

    multisort

    Sort an array with objects by the key of the object.

    By default it is asc, and you can set desc as an optional value to the 2nd parameter.

    exampleArray.multisort('age', 'desc');
    
    [
        {
            id: 0,
            name: 'John',
            age: 93,
            city: ['Patmos', 'Rome'],
        },
        {
            id: 2,
            name: 'Luke',
            age: 84,
            city: 'Boeotia'
        },
        {
            id: 1,
            name: 'Peter',
            age: 62,
            city: 'Rome',
        },
        {
            id: 2,
            name: 'Paul',
            age: 62,
            city: 'Rome'
        },
    ]
    

    multifilter

    Filters the array for a given key and value.

    If the 3th parameter is true, excluding a given key and value.

    exampleArray.multifilter('age', '62');
    
    [
        {
            id: 1,
            name: 'Peter',
            age: 62,
            city: 'Rome',
        },
        {
            id: 2,
            name: 'Paul',
            age: 62,
            city: 'Rome'
        },
    ]
    
    
    exampleArray.multifilter('age', '62', '!=');
    
    [
        {
            id: 0,
            name: 'John',
            age: 93,
            city: ['Patmos', 'Rome'],
        },
        {
            id: 2,
            name: 'Luke',
            age: 84,
            city: 'Boeotia'
        },
    ]
    
    
    exampleArray.multifilter('age', '62', '>');
    
    [
        {
            id: 0,
            name: 'John',
            age: 93,
            city: ['Patmos', 'Rome'],
        },
        {
            id: 2,
            name: 'Luke',
            age: 84,
            city: 'Boeotia'
        },
    ]
    
    
    exampleArray.multifilter('age', '84', '<=');
    
    [
        {
            id: 2,
            name: 'Luke',
            age: 84,
            city: 'Boeotia'
        },
        {
            id: 1,
            name: 'Peter',
            age: 62,
            city: 'Rome',
        },
        {
            id: 2,
            name: 'Paul',
            age: 62,
            city: 'Rome'
        },
    ]
    
    
    exampleArray.multifilter('name', ['John', 'Peter']);
    
    [
        {
            id: 0,
            name: 'John',
            age: 93,
            city: ['Patmos', 'Rome'],
        },
        {
            id: 1,
            name: 'Peter',
            age: 62,
            city: 'Rome',
        },
    ]
    
    exampleArray.multifilter('city', 'Rome');
    
    [
        {
            id:   0,
            name: 'John',
            age:  93,
            city: ['Patmos', 'Rome']
        },
        {
            id:   1,
            name: 'Peter',
            age:  62,
            city: 'Rome'
        },
        {
            id:   2,
            name: 'Paul',
            age:  62,
            city: 'Rome'
        },
    ]
    

    multikey

    Plucks the given keys from the array.

    exampleArray.multikey(['name', 'age']);
    
    [
        {
            name: 'John',
            age: 93
        },
        {
            name: 'Peter',
            age: 62
        },
        {
            name: 'Luke',
            age: 84
        },
        {
            name: 'Paul',
            age: 62
        },
    ]
    

    intersect

    Computes the intersection of arrays. Returns an array containing the values that are present in all the arrays.

    If the 2nd parameter is true, the 1st parameter can contain multiple arrays.

    var a = ['John', 'Peter', 'Luke'];
    var b = ['Peter', 'Luke', 'Paul'];
    var c = ['Luke', 'Paul', 'John'];
    
    a.intersect(b);
    
    ['Peter', 'Luke']
    
    
    a.intersect([b, c], true);
    
    ['Luke']
    

    diff

    Computes the difference of arrays. Compares the array values, and return all values from array a that arent present in array b.

    If the 2nd parameter is true, it return all values that are not present in any of the arrays.

    
    var a = ['John', 'Peter', 'Luke'];
    var b = ['Peter', 'Luke', 'Paul'];
    
    a.diff(b);
    
    ['John']
    
    a.diff(b, true);
    
    ['John', 'Paul']
    

    unique

    Removes duplicate values from an array. Takes the array and returns a new array without duplicate values.

    var a = ['John', 'Peter', 'Luke', 'Peter', 'Luke', 'Paul'];
    
    a.unique();
    
    ['John', 'Peter', 'Luke', 'Paul']
    

    pushIfNotExists

    Only push the value to the array if the value doesnt exists in the array.

    Returns the new length property of the object upon which the method was called.

    var a = [];
    
    a.pushIfNotExists('John');
    
    ['John']
    
    a.pushIfNotExists('Peter');
    
    ['John', 'Peter']
    
    a.pushIfNotExists('John');
    
    ['John', 'Peter']
    

    pushMultipleIfNotExists

    Add multiple values to an array. Only push the value to the array if the value doesnt exists in the array.

    Returns the new length property of the object upon which the method was called.

    var a = [];
    
    a.pushMultipleIfNotExists([
        'John',
        'Peter'
    ]);
    
    [
        'John',
        'Peter'
    ]
    
    a.pushMultipleIfNotExists([
        'Luke',
        'Paul'
    ]);
    
    [
        'John',
        'Peter',
        'Luke',
        'Paul'
    ]
    
    a.pushMultipleIfNotExists([
        'John',
        'Peter'
    ]);
    
    [
        'John',
        'Peter',
        'Luke',
        'Paul'
    ]
    

    pushMultiple

    Add multiple values to an array.

    Returns the new length property of the object upon which the method was called.

    var a = [];
    
    a.pushMultiple([
        'John',
        'Peter'
    ]);
    
    [
        'John',
        'Peter'
    ]
    
    a.pushMultiple([
        'Luke',
        'Paul'
    ]);
    
    [
        'John',
        'Peter',
        'Luke',
        'Paul'
    ]
    

    max

    The largest of the given numbers. If at least one of the arguments cannot be converted to a number, NaN is returned.

    var exampleArray = [1,2,3];
    
    exampleArray.max();
    
    3
    

    min

    The smallest of the given numbers. If at least one of the arguments cannot be converted to a number, NaN is returned.

    var exampleArray = [1,2,3];
    
    exampleArray.min();
    
    1
    

    random

    Get a random value of an array.

    var exampleArray = [1,2,3];
    
    exampleArray.random();
    
    e.g. 2
    

    summ

    The summ of all values.

    var exampleArray = [1,2,3];
    
    exampleArray.summ();
    
    6
    

    average

    Get the average of all values.

    var exampleArray = [1,2,3];
    
    exampleArray.average();
    
    2
    

    Test the package.

    To test the package, clone the package to your system. Than run this command.

    npm run build
    

    This will copy the example files to the dist, and also build the package files include the dependencies.

    When this script is complete without errors, you can open dist/index.html in your browser. Open the dev tools, tab console, and you see all the results of the tests.

    If you only want to check the eslint rules, just run.

    npm run lint
    

    node

    To include way2web-helpers in Node, first install with npm.

    npm install way2web-helpers
    

    Use the package in your node files.

    require('way2web-helpers');
    

    Than you can use all array helpers from this package in your node files.

    An example is included example/node.js

    Test

    npm test
    

    This will run all the tests in the test folder with mocha.

    Keywords

    Install

    npm i way2web-helpers

    DownloadsWeekly Downloads

    29

    Version

    0.2.0

    License

    MIT

    Unpacked Size

    16.7 kB

    Total Files

    4

    Last publish

    Collaborators

    • pkeulers
    • johanvanhelden
    • elwinvaneede
    • tomw2w
    • w3news