Skip to content
Perform crud operations within a JavaScript object using a string representation of the path at which you wish perform operations.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests
.gitignore
.travis.yml
LICENSE.md
README.md
helpers.js
jsocrud.js
package.json

README.md

jsocrud Build Status

Perform crud operations within a JavaScript object using a string representation of the path at which you wish perform operations.

View the wiki for function documentation.

Usage


Paths in JSOCRUD


A path is the route in the object you would use to access an element. This can be given in dot or bracket notation.

Examples of acceptable paths and their resulting locations in an object:

  • 'foo' -> object.foo
  • '.foo' -> object.foo
  • "['foo']" -> object.foo
  • '["foo"]' -> object.foo
  • 'foo.bar' -> object.foo.bar
  • 'foo bar.baz' -> object["foo bar"].baz
  • '.foo[1]['bar']' -> object.foo[1].bar

Code example


Here are some code examples of how you would use this package interact with JavaScript objects:

> var jsocrud = require('jsocrud');  // Require the package to start

// - Insert (Create) -
> var object = {};
undefined
> jsocrud.insert(object, 'foo', {});
{ foo: {} }
> jsocrud.insert(object, 'foo', []);
Error: An entity already exists at path: .foo
> jsocrud.insert(object, 'foo.baz[1]', 'hello');
Error: There was an error setting the given value at the given path

// - Get (Read) -
> var object = {'foo': [{'bar': 'baz'}], 'something here': 'hello'};
undefined
> jsocrud.get(object, 'something here');
'hello'
> jsocrud.get(object, 'foo[0].bar');
'baz'
> jsocrud.get(object, 'foo[1].bar');
Error: No entity exists in the given object at the given path
> jsocrud.get(object, 'foo[1].bar', 'Nothing there');
'Nothing there'

// - Set (Update) -
> var object = {'foo': [{'bar': 'baz'}]};
undefined
> jsocrud.set(object, 'foo[0].bar', 25);
{ foo: [ { bar: 25 } ] }
> jsocrud.set(object, 'foo[1].baz', 32);
Error: There was an error setting the given value at the given path

// - Remove (Delete) -
> var object = {'foo': [{'bar': 'baz'}]};
undefined
> jsocrud.remove(object, 'foo[0].bar');
{ foo: [ {} ] }
> jsocrud.remove(object, 'foo[1].bar');
Error: There was an error deleting from the given object at the given path

Setup for Development


Installing


npm install

Running Tests


npm test
You can’t perform that action at this time.