npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

montaque-objectreader

v1.1.2

Published

A safe way to access and manipulate object properties and arrays

Downloads

3

Readme

Object-Reader Build Status

Table of Contents

ObjectReader

ObjectReader is a class that allows you to access the properties of objects and arrays without fear of having illegal access errors It also allows you to quickly add data and construct your object using simple string dot notation.

Parameters

  • source

Examples

Install Via NPM

npm install --save montaque-objectreader

Install Via Bower

bower install montaque-objectreader

Meta

  • author: Michael Montaque on 8/14/16.

doesSourceExists

Returns whether or not the reader has a source associated with it

Returns boolean

setSource

sets the source of the reader. (The source is the object that will be read.)

Parameters

  • source (Object | Array) changes the source of the reader to the value provided

getSource

Returns the source as given or as an array of objects.

Parameters

  • asArray {Boolean} If true will return source as array

Returns (Object | Array) returns the ObjectReader's source. NOTE: the returned source is a deep clone of the original so it can be manipulated without mutating the original

inspect

This method allows you to read into an object many levels deep using a string dot notation. If you try to access properties from undefined members it will return undefined or whatever you tell it to return in the defaultProperty param. You can also access array members by referring to the index in the dot notation.

Parameters

  • propertyString String Dot notations string path to the key you want to access
  • defaultProperty any value you want to return if the accessed key is non-existent

Examples

var obj = {a:1, b:{c:'test', d:{e:'me'}, f:[30,29,{g:31}]}}
 var reader = new ObjectReader(obj);
 reader.inspect('a')                         // -> 1
 reader.inspect('b.c')                       // -> 'test'
 reader.inspect('b.d.e')                     // -> 'me'
 reader.inspect('b.f.2.g')                   // -> 31
 reader.inspect(b.d.f.g, 'Does not exist')   // -> 'Does not exist'

Returns any

inject

Sets the given value to the key dictated by the keyString using recursion. If isStrict is set to true then the function will fail and return false when trying to set a value to illegal objects or if the path dictated by the keyString does not exist. If successful, this method mutates the original object (source).

Parameters

  • keyString String Path (Denoted in string dot notation) of where to set the value
  • value Any value you want to set
  • isStrict Boolean protects the object from unintended mutations.

Examples

var obj = {a:1, b:{c:'test', d:{e:'me'}}}
 var reader = new ObjectReader(obj);

 // To set a new value at an existing key path
 reader.inject('b.c', 3)   // true  ( obj.b.c -> 3 )

 // To set a new value at a shallow non-existent path
 reader.inject('b.d.f', 'new value' )   // true ( obj.b.d.f -> 'new value' )

 // To set a new value at a deep non-existent path
 reader.inspect('e.f.g', 'created')          // true ({a:1, b:{c:'test', d:{e:'me'}}, e:{f:{g:created}})

 // You can also set arrays by using number
 reader.inspect(a.0.b.0, 'exist')            // true (obj -> {a:[{b:['exist']}], b:{c:'test', d:{e:'me'}})

 // To safely set a new value at a deep non-existent path without unexpected mutations
 reader.inspect('b.c.d', 'fail', true)       // false (obj.b.c.d does not exist so nothing happens)
 reader.inspect('e.f.g', 'fail', true)       // false (obj.e does not exist so nothing happens)
 reader.inspect('e', 'pass', true)           // true (obj -> {a:1, b:{c:'test', d:{e:'me'}}, e:'pass')

Returns Boolean Returns true if successful.

toString

Overrides the toString method to return the object back as a string