Giter Site home page Giter Site logo

hoops's Introduction

hoops

Nested property access and manipulation library for node and the browser. Available as a standalone module, and integrates with underscore and lodash. Tested in all modern browsers, and IE6+. Inspired by igorw/get-in.

Build Status

Overview

hoops is a library for traversing and manipulating deeply nested properties in JavaScript. It's a set of convenience functions to help reduce some of the repetition faced in these scenarios. For example, to retrieve a deeply nested configuration value from an object, you may have:

var namespace;
if (app.environment && app.environment.redis && app.environment.redis.namespace) {
  namespace = app.environment.redis.namespace;
}

All this to avoid a TypeError resulting from trying to access a property of an undefined value: TypeError: Cannot read property 'redis' of undefined. How can hoops help? You can achieve the same as the previous example using:

var namespace = hoops.getIn(app, 'environment.redis.namespace');

Keys may be accessed via a dot-delimited string as seen above, or by providing an array of strings. The library also integrates nicely with both underscore and lodash, allowing you to do:

_(object).getIn('some.nested.property').pick('id', 'name');

It may not offer the convenience of monads, but it certainly helps reduce friction in dealing with these objects. Some may also wonder how this compares to underscore-contrib or lodash-contrib:

  • The contrib repos only offer getPath and hasPath (getIn, isIn)
  • The libs cannot be used standalone, as they require underscore/lodash

Installation

The library can be installed via npm:

npm install hoops

Or using bower:

bower install hoops

NodeJS

// Standalone
var hoops = require('hoops');

// With underscore
var _     = require('underscore');
var hoops = require('hoops');
_.mixin(hoops);

// With lodash
var _     = require('lodash').runInContext();
var hoops = require('hoops');
_.mixin(hoops);

Browser

Simply load hoops.min.js. For automatic integration with underscore or lodash, the file should be loaded after either script. It can also be loaded via CommonJS and AMD.

Usage

Standalone

Chaining is not available when using hoops on its own.

hoops.getIn(object, 'foo.bar.baz');

Underscore

Underscore chaining requires chain() and value() calls.

_.getIn(object, 'foo.bar.baz');
_(object).chain().getIn('foo.bar.baz').value();

Lodash

Lodash chaining only requires a value call.

_.getIn(object, 'foo.bar.baz');
_(object).getIn('foo.bar.baz').value();

API

hoops.getIn(object, keys, [defaultValue])

Returns the value at the nested property, if it exists. Keys may either be an array, or a dot-delimited string of properties. If the value does not exist, the function returns undefined, or the defaultValue if supplied.

var object = {foo: {bar: {baz: 'example'}}};
hoops.getIn(object, 'foo.bar.baz'); // => 'example'
hoops.getIn(object, ['foo', 'bar', 'baz']); // => 'example'

hoops.isIn(object, keys)

Returns whether or not the nested property is defined. Keys may be an array, or a dot-delimited string of properties.

var object = {foo: {bar: {baz: 'example'}}};
hoops.isIn(object, 'foo.bar.baz'); // => true
hoops.isIn(object, ['foo', 'bar', 'baz']); // => true

hoops.invokeIn(object, keys, [...args])

Invokes the function nested at the provided path of keys, if it exists, and returns the object. The given keys may be an array, or a dot-delimited string of properties. invokeIn accepts a variable number of arguments to be passed. If a function at the key does not exist, the object is simply returned.

var y, object;
object = {foo: {bar: function(x, z) {
  y = x + z;
}}};

hoops.invokeIn(object, 'foo.bar', 10, 5); // => object
console.log(y); // 15

hoops.updateIn(object, keys, value)

Updates the nested key with the given value, if it exists, and returns the object. Keys may either be an array, or a dot-delimited string of properties. If a key does not exist, the object is simply returned.

var object = {foo: {bar: { baz: 'test'}}};
hoops.updateIn(object, 'foo.bar.baz.invalid', 'updatedValue'); // => object

hoops.setIn(object, keys, value)

Sets the nested key to the provided value, creating objects for any non-existent properties, and returns the supplied object. Keys may be supplied an array, or a dot-delimited string of properties.

var object = {foo: {}};
hoops.setIn(object, 'foo.bar.baz', 'example');
// {foo: {bar: {baz: 'example'}}}

hoops's People

Contributors

danielstjules avatar erikrestificar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.