json-artisan

0.1.3 • Public • Published

JSON Artisan

A JSON deep extend library

JSON Artisan

Install the module from npm

npm i json-artisan --save

Usage

In its basic form JSON Artisan extends objects.

const artisan = require('json-artisan');
 
artisan({
    name: 'ruddiger',
    age: 37,
    pets: {
        dogs: ['murphey'],
        cats: ['tammy', 'jupiter']
    }
}, {
    age: 27,
    pets: {
        cats: ['jupiter', 'felix']
    }
}, {
    age: 28,
    canDraw: true
});
#=> {
    name: 'ruddiger',
    age: 28,
    pets: {
        dogs: ['murphey'],
        cats: ['jupiter', 'felix'],
    },
    canDraw: true
}

Unset keys by providing a value of undefined.

const target = {
    name: 'ruddiger',
    age: 37,
    income: 3000
};
 
artisan(target, {
    income: undefined
});
#=> {
    name: 'ruddiger',
    age: 37
}

Extensions

JSON Artisan accepts functions as properties, enabling you to do things like this:

const target = {
    age: 37,
    cats: ['tammy', 'jupiter']
};
 
artisan(target, {
    cats: (cats) => cats.map(cat => cat + '-cat')
});
#=> {
    age: 37,
    cats: ['tammy-cat', 'jupiter-cat']
}

The library comes with a useful set of extensions available via the $ object.

const $ = require('json-artisan').$;

$.Array.Concat

Adds provided values to the end of the array.

artisan({
    dogs: ['rex']
}, {
    dogs: $.Array.Concat('jacob', 'murphey')
});
#=> {
    dogs: ['rex', 'jacob', 'murphey']
}

$.Array.AddToSet

Adds provided values to the end of the array only if they do not already exist.

artisan({
    dogs: ['rex']
}, {
    dogs: $.Array.AddToSet('murphey', 'rex')
});
#=> {
    dogs: ['rex', 'murphey']
}

$.Array.Splice

Splice the array.

artisan({
    dogs: ['jacob', 'murphey', 'rex']
}, {
    dogs: $.Array.Splice(1, 1, 'alex', 'fredrick')
});
#=> {
    dogs: ['jacob', 'alex', 'fredrick', 'rex']
}

$.Array.Map

Performs the provided method on every item in the array.

artisan({
    dogs: ['rex', 'catalina']
}, {
    dogs: $.Array.Map(dog => dog + '-dog')
});
#=> {
    dogs: ['rex-dog', 'catalina-dog']
}

$.Boolean.Toggle

Reverses the value of the boolean (default: true).

artisan({
    isParent: true
}, {
    isParent: $.Boolean.Toggle()
});
#=> {
    isParent: false
}

$.Number.Inc $.Number.Dec

Increments or decrements the number by the provided amount (default: 1).

artisan({
    foundTreasuresCount: 10
}, {
    foundTreasuresCount: $.Number.Inc()
});
#=> {
    foundTreasuresCount: 11
}

$.Number.Mul $.Number.Div

Multiplies or divides the number by the provided amount (default: 1).

artisan({
    awesomeness: 5
}, {
    awesomeness: $.Number.Mul(3)
});
#=> {
    awesomeness: 15
}

$.Number.Max $.Number.Min

Sets the number to the highest or lowest number (default: 0).

artisan({
    health: 120
}, {
    health: $.Number.Min(100)
});
#=> {
    health: 100
}

$.Object.Set

Replaces the object rather than extending it (default: {}).

artisan({
    toys: { racers: 3, stuffedAnimals: 1 }
}, {
    toys: $.Object.Set({ racers: 5, baseballs: 2 })
});
#=> {
    toys: { racers: 5, baseballs: 2 }
}

$.String.Append $.String.Prepend

Appends or prepends a string with an optional separator (default: '').

artisan({
    fullName: 'kangaroo'
}, {
    fullName: $.String.Append('rodrigo', '')
});
#=> {
    fullName: 'kangaroo, rodrigo'
}

Contribute

If you like what you see please feel encouraged to get involved report problems and submit pull requests! As of the time of this writing the project is new with one maintainer.

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i json-artisan

    Weekly Downloads

    0

    Version

    0.1.3

    License

    ISC

    Last publish

    Collaborators

    • kequc