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

object-formatter

v1.0.2

Published

format object safely

Downloads

31

Readme

object-formatter Build Status npm version

format object safely

Install

npm i --save object-formatter

Usage

var ObjectFormat = require('object-formatter');
var of = new ObjectFormat();

var object = {
    a: 'lorem',
    b: 'hoge',
    c: {
        ca: 'foo',
        cb: [ 1, 2, 3 ]
    },
    d: [
        { aa: 'a-a', bb: 'b-b' },
        { aa: 'a--', bb: 'b--' },
        { aa: '---', cc: 'ccc' }
    ]
};

var schema = {
    raw: 'raw value',
    foo: '@a',
    bar: '@b.c.d="ipsum"',
    baz: {
        raw: 111,
        a: '@c.cb',
        b: '@c.c.c',
        c: [ '@d', {
            hoge: '@aa',
            fuga: '@bb="b default"'
        } ],
        d: [ '@d', '@cc="c default"' ]
    }
};

of.format(schema, object);
// ->
// {
//     raw: 'raw value',
//     foo: 'lorem',
//     bar: 'ipsum',
//     baz: {
//         raw: 111,
//         a: [ 1, 2, 3 ],
//         b: undefined,
//         c: [
//             { hoge: 'a-a', fuga: 'b-b' },
//             { hoge: 'a--', fuga: 'b--' },
//             { hoge: '---', fuga: 'b default' }
//         ],
//         d: [ 'c default', 'c default', 'ccc' ]
//     }
// }

more examples, see test file

Constructor

ObjectFormatter(accessorSymbol='@', defaultValue=undefined)

Instance methods

of.format(schema, object) -> object

It format the object according to the schema definition.

Schema definitions

The schema must be an object. Schema's keys will be the key of the formatted object with no modifications. Schema's value means new value. If value is not a string, that value will be the value of the formatted object with no modifications. If value is a string but not start with of.accessor(@), that value will also be the value of the formatted object with no modifications.

If value is string and start with of.accessor(@), it means new value's path. If path doesn't exists on object, this definition will return of.default(undefined).

Simple accessor (path string)

@path.to.value

It accesses object.path.to.value. Fields of object are separate with . like a javascript.

var object = { a: { b: ['this', 'is', 'a.b'] } };
of.format({ result: '@a.b' }, object);
// -> { result: ['this', 'is', 'a.b'] }

@path.to.value="temporary default"

= means temporary default value. That definition accesses object.path.to.value and returns exact value, or 'temporary default' value when path doesn't exist.

var object = { a: { b: ['this', 'is', 'a.b'] } };
of.format({ result: '@a.b.c="not exists"' }, object);
// -> { result: 'not exists' }

Collection accessor

The array value that 1st arg is path string and 2nd arg is schema object or path string, is collection accessor. (collection is objected array). 1st arg path string defines object's collection path. Its accessor returns array or collection or default value.

["@path.to.collection", path]

It returns array (maybe not collection). 2nd arg's path refers to collection's element object.

var object = { a: [{ b: 1 }, { b: 2 }, { b:3 }] };
of.format({ result: ['@a', '@b'] });
// -> { result: [1, 2, 3] }

["@path.to.collection", schema]

It returns collection. 2nd arg's schema object is schema of collection's element object.

var object = { a: [{ b: 1 }, { b: 2 }, { b:3 }] };
of.format({ result: ['@a', { new_b: '@b' }] });
// -> { result: [{ new_b: 1 }, { new_b: 2 }, { new_b: 3 }] }