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

collectionize

v1.1.2

Published

A lightweight JS model/collection library.

Downloads

1,747

Readme

Collectionize

Build Status

A lightweight JS model/collection management library, built on top of Lodash.

Installation

bower install collectionize
npm install collectionize

Example Usage

var Things = Collectionize('things');

Things.add({ id: 2, color: 'blue', shape: 'square' });
Things.add({ id: 3, color: 'red', shape: 'circle' });
Things.add({ id: 5, color: 'green', shape: 'polygon' });
Things.add({ id: 6, color: 'blue', shape: 'triangle' });

Things.size();
// 4

Things.search({ color: 'blue' });
// [{ id: 2, color: 'blue', shape: 'square' }, { id: 6, color: 'blue', shape: 'triangle' }]

Things.get({ color: 'blue' });
// [{ id: 2, color: 'blue', shape: 'square' }]

Things.index({ id: 3 });
// 1

Things.update({ id: 3, color: 'yellow' });
// [{ id: 3, color: 'yellow', shape: 'circle' }]

Methods

add(obj)

Add an object to the collection.

Things.add({ id: 2, color: 'blue', shape: 'square' });
// undefined

flush(), flush(newCollection)

Flush out and optionally replace the collection.

Things.flush()

get

Alias for the Lodash find method.

getById(id)

Uses an ID-based index to get a single object. This can be faster than get for larger collections.

Things.getById(123);
// { id: 123, color: 'green', shape: 'triangle' }

incr(query, property)

Increments the property number or sets it to zero if it doesn't exist.

Things.incr({ id: 2 }, 'id');
// undefined

index

Alias for the Lodash findIndex method.

isEmpty(query)

Returns true if the filtered query returns empty, otherwise returns false.

Things.isEmpty({ color: 'yellow' });
// true

length

Alias for the Lodash size method.

move(oldIndex, newIndex)

Change the order of an object in the collection.

Things.move(1, 2);

remove(query)

Remove all matching elements from the collection.

// Remove object with id 3
Things.remove({ id: 3 });
// Remove all squares
Things.remove({ shape: 'square' });

search

Alias for the Lodash filter method.

update(obj, property)

Updates all matching objects by property, or adds object if no matches are found.

// Change color to 'red' for object with id '2'
Things.update({ id: 2, color: 'red' });
// Change all squares to color 'red'
Things.update({ shape: 'square', color: 'red' }, 'shape');
// Change all 'blue' colors to 'green'
Things.update({ color: 'green' }, { color: 'blue' });

updateById(obj)

Searches the ID index for a matching object and updates it, otherwise adds it to the collection. This can be faster than update for larger collections.

// Change color to 'red' for object with id '2'
Things.updateById({ id: 2, color: 'red' });

Lodash Methods

at, each, every, filter, find, findIndex, findLastIndex, first, last, map, max, min, pluck, reduce, reduceRight, reject, sample, size, shuffle, some, sortBy, where

Collectionize simply decorates your collection with these methods, meaning instead of _.filter(Things, query) you would write Things.filter(query).

Event Methods

on(eventName, fn)

off(eventName)

trigger(eventName)

Events

beforeAdd

Decorate the object before it's added to the collection.

Things.on('beforeAdd', function (thing) {
  thing.initialized = true;
});

added

Do something after an object has been added to the collection, such as save the new object on the server.

Things.on('added', function (thing) {
  $.ajax({ url: '/thing', type: 'POST', data: thing });
});

beforeUpdate

Decorate the object before it's updated.

Things.on('beforeUpdate', function (thing) {
  thing.initialized = true;
});

updated

Do something after an object has been updated, such as save the new object on the server.

Things.on('updated', function (thing) {
  $.ajax({ url: '/thing/' + thing.id, type: 'PUT', data: thing });
});

removed

Do something after an object has been removed from the collection.

Things.on('removed', function (thing) {
  $.ajax({ url: '/thing/' + thing.id, type: 'DELETE' });
});

Running the Tests

yarn
yarn test

License

MIT. Copyright © 2017 Andrew Childs.