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

hoser

v0.0.5

Published

Hoser.js is a small JavaScript library (< 4kb minified) for creating observable data objects whose attributes can be lexically scoped.

Downloads

2

Readme

Hoser.js

Hoser.js is a small JavaScript library (< 4kb minified) for creating observable data objects whose attributes can be lexically scoped.

API Reference

Creating a new instance

Create a new instance in hoser by wrapping an object in hoser.

var instance = hoser({ value: 'value', object: { deepValue: 'deepValue' } });

get

You can retreive any value by calling #get on a hoser instance.

var instance = hoser({ key: 'value', object: { deepKey: 'deepValue' } });

instance.get('key');
// => 'value'

instance.get('object');
// => { deepKey: 'deepValue' }

instance.get('object.deepKey');
// => 'deepValue'

set

You can set any value by calling #set on a hoser instance. This also works for arbitrary levels of nesting.

var instance = hoser({ key: 'value', object: { deepKey: 'deepValue' } });

instance.set('key', 'otherValue');
// => true

instance.set('object.deepKey', 'otherDeepValue');
// => true

instance.get('key');
// => 'otherValue'

instance.get('object.deepKey');
// => 'otherDeepValue'

watch

You can observe changes in a hoser instance by calling #watch with a callback. You cannot observe values that have not yet been defined.

var instance = hoser({ key: 'value', object: { deepKey: 'deepValue' } });

instance.watch('object.deepKey', function (value) {
  // value is the new value of object.deepKey
});

instance.watch('object', function (value) {
  // value is the new value of object
});

instance.set('object.deepKey', 'otherDeepValue');
// => true
// both callbacks will be executed

unwatch

You can remove watchers on a hoser instance by calling #unwatch.

var instance = hoser({ key: 'value', object: { deepKey: 'deepValue' } });
var someCallback = function () { /* ... */ };

instance.watch('key', someCallback);

instnace.unwatch('key', someCallback);

scope

Creating new scopes of your data can be done with #scope. When an instance is scoped, a new child object is returned which holds a reference to all previous ancestors. This means that if an attribute is set on a parent object, it will immediately be available to all of it's children; however, the inverse is not true when an attribute is set on a child object; that is, the new attribute is scoped to the child.

var instance = hoser({ key: 'value', object: { deepKey: 'deepValue' } });
var scoped = instance.scope();

scoped.get('key');
// => 'value'

instance.set('key', 'otherValue');

scoped.get('key');
// => 'otherValue'

scoped.set('otherKey', 123);

instance.get('otherKey');
// => undefined

Observing changes is also determined by the scope where the attribute was created.

var instance = hoser({ key: 'value', object: { deepKey: 'deepValue' } });
var scoped = instance.scope();

scoped.watch('key', function () {
  alert('this is an alert!');
});

instance.watch('key', function () {
  console.log('this is a log!');
});

instance.set('key', 'otherValue');
// => true
// alert will appear and console will log

scoped.set('key', 'yetAnotherValue');
// => true
// alert will appear and console will log