Skip to content

bdunford/helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

helpers

Node helper extensions to Object, Array, and String

Requires Nodejs http://nodejs.org

Installing

From the Command Line
npm install git+https://github.com/bdunford/helpers.git

package.json as a dependency
add "helpers": "git+https://github.com/bdunford/helpers.git" to dependencies

{
    "name": "your-app",
    "version": "0.0.0",
    "private": true,
    "description": "just another node app",
    "dependencies": {
        "helpers": "git+https://github.com/bdunford/helpers.git"
    }
}

Raw
git clone https://github.com/bdunford/helpers
Be sure to run npm install from with in the helpers directory

Usage

require helpers in your javascript. Helpers are all extensions methods so there is no need to set a variable.

require('helpers');

Object Helpers

merge will merge the contents of the passed object with the object the extension was called on

var first = {
    color: "green",
    size: "big"
};

var second = {
    color: "yellow",
    age: 10
};

first.merge(second);
//will return  { color: "yellow", size: "big", age: 10}

isEqualTo returns true if the object values are equal since == will always return false unles comaring the same object

var first = {
    color: "green",
    size: "big"
};

first.isEqualTo({color: "green", size: "big"});
// will return true

Array Helpers

first returns the first item of an array, the default value if passed or null

var ar = ["blue","green","red"]
ar.first(); // will return "blue"
[].first("black"); // will return black;

where returns a new array containing the values that met the passed condition.

var ar = [
    {name: "apple", color: "red"},
    {name: "banana", color: "yellow"},
    {name: "lemon", color: "yellow"},
    {name: "plum", color: "purple"},
    {name: "grape", color: "purple"}
];
ar.where(function(x){return x.color == "purple"});
/* will return [
    {name: "plum", color: "purple"},
    {name: "grape", color: "purple"}
]

group_by will return an an object that contains the resulting arrays described by the values of the groupBy parameter that was passed.

var ar = [
    {name: "apple", color: "red"},
    {name: "banana", color: "yellow"},
    {name: "lemon", color: "yellow"},
    {name: "plum", color: "purple"},
    {name: "grape", color: "purple"}
];

ar.group_by("color");
/* will return {
    red: [
        {name: "apple", color: "red"}
    ],
    purple: [
        {name: "plum", color: "purple"},
        {name: "grape", color: "purple"}
    ],
    yellow: [
        {name: "plum", color: "purple"},
        {name: "grape", color: "purple"}
    ]
}*/

pushUnique will only add an object to the array if it does not exit using Object.isEqualTo

var ar = [{color: "blue"},{color: "green"},{color: "red"}]
ar.pushUnique({color: "blue"}); // will not add {color: "blue"} to the array.

String Helpers

tryJsonParse will attempt to parse the string as json to an object using JSON.parse. If the JSON.parse fails tryJsonParse will return false. If returnException = true is passed myString.tryJsonParse(true) and the parse fails the JSON.parse exception will be returned.

var s = '{"color": "blue"}'
s.tryJsonParse(); // will return {color: "blue"}

capitalize returns a new string with the first letter capitalized

var s = "dayton"
s.capitalize(); // will return "Dayton"

About

Node helper extensions to Object, Array, and String

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published