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

json-binding

v0.2.1

Published

Simplified json generation

Downloads

1

Readme

js-binding

Simplified json generation

Install

npm install json-binding --save

Usage

This library allows to create json template and bind with your model, for example:

This is json template.

{
    '{{type}}Id': '{{id}}',
    list: [
        { 
            $repeat: 'item in items', 
            $if: 'item.confirm', 
            amount: {{item.amount}} 
        }
    ]
}

This is model.

{
    id: 1,
    type: 'order',
    items: [
        { amount: 4, confirm: true },
        { amount: 9, confirm: false },
    ]
}

And you can bind template and model together by:

let jsonBinder = new JsonBinder();
let result = jsonBinder.bind(template, model);

And the result will be:

{
    orderId: 1,
    list: [
        { amount: 4 }
    ]
}

Directives

$bind

$bind is to output from your model.

result = jsonBinder.bind(
    { test: { $bind: 'foo' } }, 
    { foo: ['bar'] }
);
expect(result).deep.equals({ test: ['bar'] });

$content

$content is to return object inside this directive.

result = jsonBinder.bind(
    { test: { $content: ['bar'] } }, 
    { }
);
expect(result).deep.equals({ test: ['bar'] });

$if

$if is to conditionally add or not add object.

result = jsonBinder.bind({
    foo: { $if: 'x == 1', fooKey: 'fooValue' },
    bar: { $if: 'x == 2', barKey: 'barValue' },
}, { x: 1 });
expect(result).deep.equals({ foo: { fooKey: 'fooValue' } });

You can use $if in conjuction with $content and $else.

result = jsonBinder.bind(
    { test: { $if: 'x == 2', $content: 'yes', $else: 'no' } }, 
    { x: 1 }
);
expect(result).deep.equals({ test: 'no' });

$init

$init is to run command when binding.

result = jsonBinder.bind({ $init: 'x = 2', $content: '{{x}}' }, { x: 1 });
expect(result).equals('2');

$repeat

$repeat is to loop your model and bind to template.

result = jsonBinder.bind(
    { list: [ { $repeat: 'item in items', $if: 'item != "c"', value: '{{item}}' } ] }, 
    { items: [ 'a', 'b', 'c' ]}
);
expect(result).deep.equals({ 
    list: [
        { value: 'a' },
        { value: 'b' },
    ]
});

You can loop through object.

result = jsonBinder.bind(
    { 
        $repeat: '(key, value) in obj', 
        $content: '{{key}}={{value}}' 
    }, 
    { obj: { a: 'b', c: 'd' } }
);
expect(result).deep.equals(['a=b', 'c=d']);

In $repeat, you can access to index by $index, and access to parent scope by $parent.

$template

You can add template by registerTemplate and bind model to template by $template.

jsonBinder.registerTemplate('baz', { foo: 'baz' });

result = jsonBinder.bind({ $template: 'template' }, { template: 'baz' });
expect(result).deep.equals({ foo: 'baz' });

Custom Directives

You can add custom directives by registerDirective. Please see example in unit test.

Promise

If one of operation is Promise, bind method will return Promise. Otherwise JsonBinder will return non-promise value.

Plug to AngularJS

This library can be used in NodeJS or on browser without AngularJS. If you would like to register JsonBinder to AngularJS and using AngularJS filters, you can do following.

angular.module('yourModule')
    .service('jsonBinder', JsonBinder);

Now you can use AngularJS filters.

{
    totalValue: '{{ vm.sumTotal() | number:2 }}'
}