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

cheiron

v0.3.2

Published

small model class

Downloads

2

Readme

Cheiron

npm version Travis CI Badge (master branch)

Small basic model (observable) class for Node.js and the browser.

How to include

Node.js / Browserify

In your shell:

$ npm install --save cheiron

In your javascript:

var Cheiron = require('cheiron')

Browser ("classic" way)

Just add a script tag with either cheiron.js or cheiron.min.js from this repos root directory. This makes the Cheiron variable globally available.

typeof window.Cheiron
// --> 'function'

API

Cheiron#constructor([<data>[, <context>]])

Creates a new instance. data will be used as the initial data, all observers will be invoked in context. Both data and context are optional.

If data does not contain a field _id, it will automatically be set to and ID with the pattern xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx (GUID) where x represents one of '0' to '9' or 'a' to 'f' and y represents one of '8', '9', 'a' or 'b'.

var model = new Cheiron({
  name: '[Betelgeusian]',
  planet: 'Betelgeuse'
}, obj)

console.log(model.get('_id'))
// --> (something like) da93fac1-a469-47f9-98c9-b1d7f41f4be9

/*
var model = new Cheiron()

var model = new Cheiron({
  name: 'Arthur Dent',
  planet: 'Magrathea'
})

var model = new Cheiron({}, obj)
*/

Cheiron#get(<key>)

Simply returns the value of key, if key has no value then undefined.

console.log(model.get('planet'))
// --> 'Betelgeuse'

Cheiron#has(<key>)

Returns false if key is undefined/not defined, else true.

console.log(model.has('name'))
// --> true

Cheiron#keys()

Returns an array of all set keys.

console.log(model.keys())
// --> [ 'name', 'planet' ]

Cheiron#events

An instance of reg4in/reeal.

// will be fired every time the model changes
model.events.on('change', callback)

model.events.on('change:name', function (value, key, old) {
  console.log(key + ' has been changed from ' + old + ' to ' + value)
  console.log(this === obj)
})

model.events.off('change', callback)

Cheiron#set(<key>, <value>[, <silent>])

Triggers events: change, change:<key> (if silent is falsy)

Sets key to value if value is actually different from the current value, executes all bound observers.

If value is of type function, it will be invoked with the current value as the first and the key as the second argument. You should note that the function will be executed only when you set the value, not everytime you query it.

model.set('name', 'Ford')
// --> 'name has been changed from [Betelgeusian] to Ford'
// --> true

model.set('name', function (name, key) {
  return name + ' Prefect'
})
// --> 'name has been changed from Ford to Ford Prefect'
// --> true

model.set('name', 'Arthur Dent', true)
// [does not invoke listeners]

Cheiron#setAll(<object>[, <silent>])

Calls Cheiron#set for every key-value pair of object and passes on silent.

var earth = new Cheiron({
  name: 'Earth'
})

earth.setAll({
  radius: 6370000,
  type: 'Actually a very big computer'
})

Cheiron#unset(<key>[, silent])

Triggers events: change, change:<key> (if silent is falsy)

Removes key from the internal data object.

model.unset('name')

model.unset('name', true)

Testing/Building

Installing development dependencies

$ npm install

Running tests

$ npm test

Building for the browser

$ npm run build
$ # for building on file change
$ npm run watch

License

MIT license, see LICENSE.