Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-pointer

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-pointer

A javascript object pointer class

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

object-pointer (NodeJS)

Build Status

A class which points to a certain position inside an object and allows you to modify everything relative to that position.

Installation

First get the package from the npm registry

npm install object-pointer

Then get the class in your application

var Pointer = require('object-pointer');

Example

Create an object and create a Pointer instance first:

var obj = {};
var p = new Pointer(obj, ['nested', 'object']);

You'll see your object is still unmodified

obj.should.eql({});

Now lets set a new key-value:

p.set('key', 'value');

Now your object has been modified

obj.should.eql({
  nested: {
    object: {
      key: 'value'
    }
  }
});

// In other words:

obj.nested.object.key === 'value';

location-argument

A lot of times there's a location argument. A location can be nothing, a string or an array and they point to a certain location inside an object.

Lets go over all the valid location arguments inside our "rootObject":

// resolves to rootObject
location = undefined;
location = null;
location = [];

// resolves to rootObject.nested
location = 'nested';

// resolves to rootObject.nested.object
location = ['nested', 'object'];

Api

new Pointer(root, rootLocation)

/**
 * This points to a certain position inside an object and allows you to read and
 * modify everything nested inside that position.
 *
 * @param {Object|Pointer} root - The root object of the Pointer
 * @param {null|string|array} location - The location inside the root object
 */
var Pointer = function Pointer(root, location)

Please refer to Pointer.setRoot as the constructor merely bridges that function.

var p = new Pointer(rootObject, location);

Pointer.setRoot(root, location)

/**
 * This sets the root location of the Pointer
 *
 * @param {Object|Pointer} root - The root object of the Pointer
 * @param {null|string|array} location - The location inside the root object
 */
Pointer.prototype.setRoot = function(root, rootLocation)

Pointer.getRoot()

/**
 * Returns the root object recursively
 */
Pointer.prototype.getRoot = function()

Pointer.setLocation(location)

/**
 * Sets the location inside the master object to which this pointer points.
 * @param {null|string|array} location - The location inside the root object
 */
Pointer.prototype.setLocation = function(location)

Pointer.getLocationObject(force)

/**
 * This returns the root of the Pointer instance.
 * @param {boolean} force -  If root object doesn't exist, create it.
 */
Pointer.prototype.getLocationObject = function(force)

Pointer.create(location)

IMPORTANT: The location is relative to the Pointer's root location, not the root object!

/**
 * This creates the nested objects up until the given location, and returns the
 * deepest object.
 *
 * @param  {null|string|array} location
 * @return {Object}
 */
Pointer.prototype.create = function(location)

Pointer.set(location, value)

IMPORTANT: The location is relative to the Pointer's root location, not the root object!

/**
 * Will set a value to the pointed object.
 *
 * @param {null|string|array} location - The location of the key, the final
 * element is the actual key.
 * @param {[type]} value
 */
Pointer.prototype.set = function(location, value)

Pointer.get(location, defaultValue)

IMPORTANT: The location is relative to the Pointer's root location, not the root object!

/**
 * Will return a value from the pointed object
 *
 * @param  {null|string|array} location
 * @param  {mixed} defaultValue - The default value to return if value not
 * found.
 * @return {mixed}
 */
Pointer.prototype.get = function(location, defaultValue)

Pointer.clear(location)

IMPORTANT: The location is relative to the Pointer's root location, not the root object!

/**
* Deletes the value at the given location.
*
* @param  {string|array} location
*/
Pointer.prototype.clear= function(location)

Test

grunt test

License

MIT.

Keywords

FAQs

Package last updated on 28 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc