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

supermodels

v0.2.4

Published

Create access functions for an object that has an array of models as a property

Downloads

10

Readme

supermodels

Build status

dot notation get/set for an object or array of objects

installation

$ npm install supermodels

usage

supermodels returns a function that can read and write to an object using dot notation:

var supermodels = require('supermodels');

var data = {
	name:'Starship Enterprise',
	address:{
		street:'Hanger 7',
		city:'London'
	}
}

var supermodel = supermodels(data);

supermodel('address.postcode', 'SW12');

console.log(supermodel('address.postcode'));

// SW12

console.log(data);

/*
	{
		name:'Starship Enterprise',
		address:{
			street:'Hanger 7',
			city:'London',
			postcode:'SW12'
		}
	}
*/

dot notation

when you write values using dot notation and they do not exist - objects will be inserted in order to write the value:

var data = {}

var supermodel = supermodels(data);

supermodel('a.b.c', 10);

console.log(data);

/*
	{
		a:{
			b:{
				c:10
			}
		}
	}
*/

array data

the data can also be an array:

var supermodels = require('supermodels');

var arr = [{
	name:'Starship Enterprise'
},{
	name:'Millenium Falcon'
}]

var supermodel = supermodels(arr);

array read

When you read from an array based supermodel - it will return the value of the first model in the array:

var name = supermodel('name');

console.log(name);

// Starship Enterprise

array write

When you write to an array based supermodel - it will write to all models in the array:

supermodel('speed', 200);

console.log(arr);

/*
	[{
		name:'Starship Enterprise',
		speed:200
	},{
		name:'Millenium Falcon',
		speed:200
	}]
*/

class property access

mymodel.js:

var supermodels = require('supermodels');

module.exports = MyClass;

// a class that has an array as a named property
function MyClass(arr){
  this.modelarray = arr;
}

function getmodels(){
	return this.modelarray;
}

// create a function that updates objects living in modelarray
MyClass.prototype.attr = supermodels(getmodels);

// create a function that updates nested objects living in modelarray
MyClass.prototype.address = supermodels(getmodels, 'address');

// create a function that updates nested properties living in modelarray
MyClass.prototype.postcode = supermodels(getmodels, 'address.postcode', true);

Then create a new instance:

var Model = require('./mymodel.js');

var data = [{
	name:'HQ',
	address:{
		city:'London',
		postcode:'SW12'
	}
},{
	name:'Accounts',
	address:{
		city:'Bristol',
		postcode:'BS1'
	}
}]

var instance = new Model(data)

Our instance can update all models in the list at once:

instance.attr('test', 10);

console.log(instance.modelarray);

data.forEach(function(model){
	console.log(model.test);
})

/*

// 10
// 10

*/

It will return the value of the first model on reading:

console.log(instance.attr('address.city'));

// London

nested attributes

Access functions allow nested attributes:

instance.address('top.middle.bottom', 12);


console.log(data[1]);

/*
{
	name:'Accounts',
	test:10,
	address:{
		city:'Bristol,
		postcode:'BS1',
		top:{
			middle:{
				bottom:12
			}
		}
	}
}
*/

api

supermodels(data, [path], [type]);

Return an accessor function that will modify data.

Data can be a an object, an array or a function.

Path is a dot notation for the property in the data to modify.

If data is an object - it will be converted to an array with one element.

If data is an array - it will be used directly.

If data is a function - it will be called each time to provide the data.

type can be one of:

  • true - which means 'primitive'
  • primitive - return a function that will read and write one value - supermodel(value)
  • object - return a function that will read and write objectvalues - supermodel(name, value)
  • remove - return a function that will delete an object property - supermodel()
  • array:add - return a function that inserts a primitive into an array (if it does not exist) - supermodel(value)
  • array:remove - return a function that removes a primitive from an array - supermodel(value)
  • array:has - return a function that tells you if a value is in the array - supermodel(value)
  • has - return a function that tells you if the model has the property - supermodel(path)
  • is - return a function that tells you if the models property is a value - supermodel(value)

A primitive:

var data = {
	title:'Test'
}
var prop = supermodels(data, 'title', true);

console.log(prop());

// Test

prop('apples');

console.log(prop());

// apples

A removal function:

var data = {
	address:{
		postcode:'SW12'
	}
}

var remover = supermodels(data, 'address', 'remove');

remover();

console.log(data);

/*
 {

 }
*/

license

MIT