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

ramble-on

v0.2.1

Published

Your one-stop property returning shop!

Downloads

2

Readme

ramble-on

ramble-on is your one-stop property returning shop!

Why should you use it?

A good question! Let's say you are working with a really big Javascript object.

var big_object = {
    foo: {
        bar: {
            baz: {
                main: [
                  "test"
                ]
              , niam: 3
            }
          , zab: false
        }
      , rab: null
    }
  , oof: [
      2
    ]
};

Let's also say that you want to return or set any arbitrary value in that object. At first, it may seem easy.

var x = big_object.foo.bar.baz.main[0]; // returns "test"

However, what if the exact structure of the object is hard to determine at "compile" time?

big_object.foo.bar = "Hello";
var x = big_object.foo.bar.baz.main[0]; // throws Error because of invalid property

In these cases, it would be nice to have a way to programmatically specify property access. You could start by creating a function that uses eval() to dynamically create a retrieval statement for a given object and properties array.

function get_property(object, properties) {
  var prop;
  eval("prop = object[" + properties.join("][") + "];");
  return prop;
}

However, you should not use eval (most of the time anyway). Also, you would need to create another function to set properties!

There has to be a better way!

Enter ramble-on...

ramble-on makes it easy to retrieve and set properties on nearly any object. You just call it with the object you want to traverse and an array of strings and integers representing the location of the object.

Example:

var ramble = require('ramble-on');

var object = big_object; // i.e. the object from the first code snippet
var property = ramble(object, ["foo", "bar", "baz"]);
console.log(JSON.stringify(property, null, 2));

Output:

{
  "main": [
   "test"
  ],
  "niam": 3
}

You can even set properties as well. Just add one parameter: the value to set

Example:

ramble(object, ["foo", "bar"], "Hello");
console.log(JSON.stringify(object, null, 2));

Output:

{
  "foo": {
    "bar": "Hello",
    "rab": null
  },
  "oof": [
    2
  ]
}

Heck, you can even handle circular dependencies!

Example:

var object = big_object; // NOTE: If you are following along, you might want to reset big_object.
ramble(object, ["foo", "bar", "baz", "main", 1], object);
var prop = ramble(object, ["foo", "bar", "baz", "main", 1, "oof", 0]);
console.log(prop); // Outputs: 2

NEW: If you want to create a nested value and do not want to manually create its parent values, just append a true to the arguments to tell ramble-on to create nested values.

Example:

var object = { foo: 'bar' };
ramble(object, ['fizz', 'buzz'], 15, true);
console.log(object.fizz.buzz); // Prints 15

NOTE: If you specify an integer as one of the properties, ramble-on will assume the previous property is an array instead of an object.

Example:

var object = { foo: 'bar' };
ramble(object, ['fizz', 0], 'buzz', true);
object.fizz.push('baz');
console.log(object.fizz.length) // Prints 2

Get ramble-on today!

How much would you pay for such a useful library? 500 dollars? 5,000 dollars?! ONE MILLION DOLLARS?!!!!

Well, you can pay $0 because ramble-on is released as Free Libre Open-Source Software under the terms of the MIT License. Copy it; share it; put it in a blender! Do all three at once!

Start using this sexy-cool library today by rambling to the root directory of your project and typing npm install ramble-on --save into the *nix console of your choice! (Requires: node.js; other options coming soon).