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

t-objects

v0.1.4

Published

Declarative style JSON objects converter

Downloads

18

Readme

Declarative style JSON objects converter

Features

  • Self descriptive template language
  • Simple syntax for flattening hierarchical objects
  • Possibility to mix declarative and imperative styles

Install

npm install t-objects

Usage example

var T = require('t-objects');

var company = {
	Departments: [
		{
			Name: 'Quality assurance',
			Staff: [
				{
					FirstName: 'Panic',
                    LastName: 'Generator',
                    Position: 'QA-Engineer'
				},
				{
					FirstName: 'Ivanov',
                    LastName: 'Ivan',
                    Position: 'QA-Engineer'
				}
			]
		},
		{
			Name: 'Development',
			Staff: [
				{
					FirstName: 'Bydlo',
                    LastName: 'Coder',
                    Position: 'Junior Developer'
				},
				{
					FirstName: 'Nebydlo',
                    LastName: 'Coder',
                    Position: 'Senior Developer'
				}
			]
		}
	]
};

var t = T({
	'->': true,
	$company: ':external',
	'{$dep}': [ '$company', function(c){ return c.Departments }],
	'{$person}': [ '$dep', function(d){ return d.Staff }],
    FirstName: [ '$person', function(p){ return p.FirstName }],
    LastName: [ '$person', function(p){ return p.LastName }],
    Position: [ '$person', function(p){ return p.Position }],
    Department: [ '$dep', function(d){ return d.Name }]
});

console.log(t.build({ $company: company }));

Output

[
	{
		FirstName: 'Panic',
		LastName: 'Generator',
		Position: 'QA-Engineer',
        Department: 'Quality assurance'
    },
	{
		FirstName: 'Ivanov',
		LastName: 'Ivan',
		Position: 'QA-Engineer',
        Department: 'Quality assurance'
    },
	{
		FirstName: 'Bydlo',
		LastName: 'Coder',
		Position: 'Junior Developer',
        Department: 'Development'
    },
	{
		FirstName: 'Nebydlo',
		LastName: 'Coder',
		Position: 'Senior Developer',
        Department: 'Development'
    }
]

The same template in ECMA 6

template = T({
	'->': true,
	$company: ':external',
	'{$dep}': [ '$company', c => c.Departments ],
	'{$person}': [ '$dep', d => d.Staff ],
    FirstName: [ '$person', p => p.FirstName ],
    LastName: [ '$person', p => p.LastName ],
    Position: [ '$person', p => p.Position ],
    Department: [ '$dep', d => d.Name ]
});

API reference

T Class

Constructor

Params

  • template Object Template object

Returns T Class instance

build

Params

  • data Object Input data object

Returns Object Output data object

Language reference

Special keys

  • -> Template marker or initial queue. Any object in template that has this marker will be processed by build method. Marker may be set in boolean true value or predefined keys queue. Predefined keys queue will be corrected or appended by properties dependencies.

  • $<string> Service property. These properties may be used as temporary variables and will be deleted at the end of processing.

  • ? Condition. If this property is falsy parent object will be rejected from output data object.

  • {<value>,<index>,<total index>} Variety. Property constructor must return array of values for this key. In this case parent object will be expanded in collection of objects. Every element of this collection will be parent object extended by new <value>, its <index> and <total index> (if parent has already become a collection).

  • $return Replace object with this property. Replaces parent object with this property.

  • : Key of object. Replaces array for multiinstance object.

  • $parent Parent object.

  • $root Root object.

Special values

  • :external Find value of this key in input data.

  • [ 'key1', 'key2', ... keyN, function(key1, key2, ... keyN){ ... }] Property constructor. 'key1', 'key2', ... keyN - dependencies. This notation also works in singular forms: [function(){...}] and function(){...}

Property constructor notation

  • [ 'key1', 'key2', ... 'keyN', function(key1, key2, ... keyN){ ... }] Property constructor.

    'key1', 'key2', ... 'keyN' - dependencies. One may mark dependency as mandatory or critical.

    • 'key*' - mandatory. In this case, if value of key is undefined, constructor will not be called.
    • 'key**' - critical. In this case, if value of key is undefined, build_error exception will be thrown.

    This notation also works in singular forms: [function(){...}] and function(){...}