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

obj-format

v2.0.0

Published

Creates a formatting template to parse collections with null-safe variables, functions, and ignores. Useful for 3rd party API formatting.

Downloads

5

Readme

Object Formatter NPM version Build Status

Of course your objects are formatted nicely internally. You wrote the code, and you're a genius. But wait! Evil 3rd party API says your objects are crap, and they want them formatted THE RIGHT (their) WAY!

Never fear, Object Formatter™ is here!

Show me how to use it already!

Installation

$ npm install obj-format

Creating a Formatter instance:

const Formatter = require('object-formatter');
/* Note: You can totally store your maps as JSON files and require them.
 * Like cloth diapers, JSON mapping files are nice, tidy, and reusable! */
const destMap = {

  // Value is replaced by variable and ignored from object search
  project_name: "!$1 is great!",

  // Search property is replaced with supplied variables
  "project_id": "$2.id",
  "first_name": "person.first",
  "last_name": "person.last",
  "address": {
    "geo": {
      "lat": "person.address.geo.lat"
    }
  },

  // Value ignored by parser
  "!ip_address": "127.0.0.1"
}
const THIRD_PARTY_FORMAT = new Formatter(destMap);

Default Behavior

Properties are not parsed by the object-looker-upper (elvis operator).

Values are parsed by the object-looker-upper (Really? Can't think of better name?).

Any property or value containing a $n will be replaced with its corresponding variable in the array you pass before property lookup.

Negating default behavior

Maybe you want a property to be a value from a source object, or maybe you want a constant string as a value. Just prefix your property or value with ! and the default behavior will be reversed!

Using the formatter

THIRD_PARTY_FORMAT.format({
  person: {
    first: 'John',
    last: 'Zoidberg',
    address: {
      street: '1234 Example Blvd',
      geo: {
        lat: 1.2345
        lng: -1.2345
      }
    }
  },
  finance: {
    id: 123,
    name: 'Finance',
    date: new Date()
  }
}, ['Finance', 'finance']);

/*
Returns:
{
  "project_name": "Finance is great!",
  "project_id": "123",
  "first_name": "John",
  "last_name": "Zoidberg",
  "address": {
    "geo": {
      "lat": "1.2345"
    }
  },
  "ip_address": "127.0.0.1"
}
*/

Methods

format(obj, vars) instance method

Populates a new copy of the mapping object formatted data

Arguments
  • obj - Source object to be parsed. Should match values in the destination object supplied in the constructor
  • vars - (Optional) Array of variables to be replaced in matching strings (I.E. ['foo','bar'] + "$2.$1-$1" => "bar.foo-foo")
Returns

Destination object populated with source values

get(props, obj) class method

Class method. Gets the property array or dot-notated field from an object. Null safe.

Arguments
  • props Array of properties in nested order or dot notated string (I.E. ["foo", "bar", "baz"] OR "foo.bar.baz")
  • obj Source object to search.
Returns

Value found from search or undefined.

Worried about undefined? Worry no longer!

Object Formatter takes all the worry out of property lookups. You're welcome.

Problems? Issues? Rude remarks?

Send me an email at [email protected], or die in a hole! Whichever comes first!