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

digger-container

v2.3.1

Published

A JQuery style array wrapper for JSON models

Downloads

59

Readme

digger-container

Build status

A JQuery style wrapper for JSON model arrays.

install

node.js

$ npm install digger-container

overview

This library provides an api very similar to JQuery but for an array of JSON models rather than DOM elements.

It can be used a stand-alone data tool or as part of a greater digger network.

containers

You can create a container from some existing data:

var Container = require('digger-container');

// create a container with a single model
var post = Container({
	name:'Hello World',
	height:34
})

// create a container from an array of models
var posts = Container([{
	name:'Post A'
},{
	name:'Post B'
}])

container format

Once some data has been containerized - it will have a _digger property injected into each model.

This allows us to add meta-data like 'tagname', 'id' and 'class' to the containers models.

var post = Container({
	name:'Hello World'
})

post.addClass('frontpage');

console.log(post.toJSON());

/*

	{
		name:'Hello World',
		_digger:{
			class:['frontpage']
		}
	}
	
*/

Notice how the class property lives inside of the _digger property.

This is so anything that digger requires for the model will not get in the way of your model data.

An example of a more complete model:

{
	name:'Ladder',
	height:354,
	_digger:{
		tag:'product',
		class:['tall', 'narrow']
	},
	_children:[]
}

children

Each model in a container can also have a '_children' property.

This is an array of model data that lives inside of the parent container.

This is how the digger tree structure works - by containers living inside of other containers.

examples

Creating containers

Create a new container with a specific tagname - the tagname is like the table name in a traditional database.

// this line will be assumed through all the rest of the examples
var Container = require('digger-container');

var product = Container('product');

You can also pass an attributes object as the second argument:

var product = Container('product', {
	name:'Blue Suede Shoes',
	price:78
})

If you have an array of JSON models already - you can just pass that:

var data = [{
	name:"Superman",
	rating:7.8
},{
	name:"Spiderman",
	rating:7.9
}]

var superheroes = Container(data);

Changing attributes

Once you have a container - you can change the attributes of ALL models within it at once - just like JQuery:

// take our superheros container from above
var superheroes = Container(data);

// set the tagnames of every model to superhero
superheroes.tag('superhero')

// set a deep attribute for each superhero - this creates an object for 'filming'
superheroes.attr('filming.city', 'Los Angeles');

Spawning new containers

We can also generate new containers from the data inside of existing ones:

// get a container with just superman inside
var superman = superheroes.eq(0);

superman.attr('loves', 'Louis Lane');

Accessing models

You can get to the raw underlying model also:

// get the data inside the superman model
var raw_superman = superheroes.get(0);

console.dir(raw_superman)

/*

	{
		name:"Superman",
		rating:7.8,
		loves:"Louis Lane",
		_digger:{
			tag:'superhero'
		}
	}
	
*/

api

these methods can be called on an instantiated container

toJSON

returns an array of the containers underlying models

spawn

returns a new container based upon the provided models

clone

returns a copy of the current container but with all of the diggerids changed

var test = Container('product', {
  price:100,
  address:{
    postcode:'apples'
  }
})

var copy = test.clone();

copy.attr('price').should.equal(100);
copy.diggerid().should.not.equal(test.diggerid());

containers

return an array of containers each one holding a single model in the current models array

var data = [{
	name:"Superman",
	rating:7.8
},{
	name:"Spiderman",
	rating:7.9
}]

var superheroes = Container(data);

var containers = superheroes.containers();

// containers is now an array of 2 containers each with 1 model

eq(index)

return a container for the model at the given index

get(index)

return the model at index

add(models)

add some models to the current models array

each(fn)

run a function over this.containers()

map(fn)

map a function over this.containers()

count()

returns the length of the models array

first()

return a container for the first model

last

return a container for the last model

children

returns a container that is all of the container models children merged into one array

recurse

run a function over a container for each model and all descendents

descendents

return a container containing a flat model array of every model and it's descendents

property getter/setters

the following methods allow you to get/set the data for the models:

  • attr - top level properties
  • digger - '_digger' properties
  • data - '_digger.data' properties - not saved to database
  • diggerid - property accessor for '_digger.diggerid'
  • path - property accessor for '_digger.path'
  • inode - property accessor for '_digger.inode'
  • id - property accessor for '_digger.id'
  • tag - property accessor for '_digger.tag'
  • classnames - property accessor for '_digger.classnames'

removeAttr / removeDigger / removeData

remove properties from models

is(tagname)

tells you if the first model is a tagname

addClass(classname)

adds a classname to the models

removeClass(classname)

removed a classname to the models

hasClass(classname)

tells you if the first model has the given class

hasAttr(name)

tells you if the first model has the given attribute

isEmpty()

if models.length<=0

inject_data(data)

extend the model with the given data

diggerurl

return this.path() + '/' + this.inode()