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

jdat

v0.2.0

Published

A wrapper for an array of models with a JQuery like syntax

Downloads

5

Readme

jdat

Build status

A wrapper for an array of models with a JQuery like syntax

installation

$ npm install jdat

usage

jdat will add methods to an object prototype:

var jdat = require('jdat');

var HeroClass = function(arr){
  this.models = arr;
}

HeroClass.prototype.halfspeed = function(){
  return this.models[0].speed/2;
}


jdat({
  // the prototype we will extend
  proto:HeroClass.prototype,
  // the field of the prototype that contains an array of models
  field:'models',
  // the field of each model that is a child array of models
  childfield:'children',
  // a function to return a new instance from an array of models
  spawn:function(data){
  	return new HeroClass(data);
  }
})


var herodata = [{
  name:'Superman',
  speed:9,
  children:[...]
},{
  name:'Spiderman',
  speed:8,
  children:[...]
},{
  name:'SilverSurfer',
  speed:11,
  children:[...]
}]


var heroes = new HeroClass(herodata);

var speeds = heroes.map(function(hero){
  return hero.get(0).speed;
})

var silver = heroes.eq(2);

silver.children().forEach(function(silverchild){
  // silverchild is a HeroClass with the children as models
})

heroes
  .descendents()
  .each(function(instance){
    console.log(instance.get(0).name);
  })

api

jdat(options)

Assign the jdat methods to a prototype - the options are:

  • proto - the prototype we will extend
  • models - the field of the prototype that contains an array of models
  • childfield - the field of each model that is a child array of models
  • spawn - a function to return a new instance from an array of models

children()

Return an instance containing all children of the models in the array:

testdata.children().forEach(function(instance){
  console.log(instance.models[0].name);
})

// Superman

descendents()

Return an instance containing all descendents of the models in the array:

testdata.descendents().forEach(function(instance){
  console.log(instance.models[0].name);
})

// Superman & children names
// Spiderman & children names
// SilverSurfer & children names

recurse(fn)

Run a function over each of the descendents turned into a new instance:

testdata.recurse(function(instance){
  console.log(instance.models[0].name);
})

// Superman & children names
// Spiderman & children names
// SilverSurfer & children names

each(fn)

Loop over each model in the array and pass a spawned instance to a function:

testdata.each(function(item){
  console.log(item.models[0].name);
})

// Superman
// Spiderman
// SilverSurfer

map(fn)

Loop over each model in the array and pass a spawned instance to a function and return the mapped results:

var names = testdata.map(function(item){
  return item.models[0].name.toUpperCase();
})

// SUPERMAN
// SPIDERMAN
// SILVERSURFER

eq(index)

Return an instance of the class with the model from 'index' as the only model in the array:

var secondone = testdata.eq(1);

console.log(secondone.models[0].name);

// Spiderman

first()

Return an instance with only the first model in its collection:

var first = testdata.first();

console.log(first.models[0].name);

// Superman

last()

Return an instance with only the last model in its collection:

var first = testdata.last();

console.log(first.models[0].name);

// SilverSurfer

get(index)

Return the raw model in the model array at 'index':

var secondmodel = testdata.get(1);

console.log(secondmodel.name);

// Spiderman

count()

Return the length of the model array

var count = testdata.count();

console.log(count);

// 3

clone()

Return an instance that has a copy (JSON.stringify / JSON.parse) of the models:

var newone = testdata.clone();

spawn()

Use the factory function to return new instances of the class with different data:

var newone = testdata.spawn([{
	name:'otherdata'
}])

instances()

Return an array of instances each with only one model in their array:

var instances = testdata.instances();

console.log(instances.length);

// 3

license

MIT