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

xsor

v0.1.0

Published

create object descriptors (accessors) backed by private variables

Downloads

6

Readme

xsor

This is a JavaScript module that eases the creation of object property descriptors (also known as "accessors") backed by pseudo-private variables. This saves you from having to mint unique symbols for your "private" data and focus on the logic of your application.

Code like this:

var MyClass = function() {
};

var x = '__x';
var onChangeX = function(value, previous) {
  // logic for when stuff changes
};

MyClass.prototype = {
  get x() {
    return this[x];
  },
  set x(value) {
    if (this[x] !== value) {
      onChangeX.call(this, value, this[x]);
    }
  }
};

// "hide" __x from for..in loops
Object.defineProperty(MyClass, x, {enumerable: false});

becomes:

var MyClass = function() {
};

var xsor = require('xsor');
Object.defineProperty(MyClass.prototype, 'x', xsor(function(previous, value) {
  // logic for when stuff changes
});

If you're unfamiliar with property descriptors, check out the docs for Object.defineProperty, Object.defineProperties and Object.create.

API

The module exports a top-level function that returns a property descriptor, and can be called in a couple of different ways. Generally speaking, descriptors are

xsor(change)

This is the simplest form, and creates a descriptor that fires the change function whenever the value of any instance changes.

var xsor = require('xsor');
var obj = {};
Object.defineProperty(obj, 'value', xsor(function(value, previous) {
  console.log('value changed from', previous, 'to', value);
}));
obj.value = 1;
// logs: "value changed from undefined to 1"

See below for more information on the change handler.

xsor(name [, options])

Creates a descriptor that refernces the private variable name with the following accepted options:

initial: value

Declares the initial value for this accessor, which will be returned by the first get.

change: function(value, previous, symbol)

Declares a change handler for the value of each individual instance, where value is the new value, previous is the previous value (or undefined, if no initial value was set), and symbol is the name of the symbol used to access the private value.

If you return false from this function, the change will not be applied.

var xsor = require('xsor');
var x = {};
var change = function(value, previous, name) {
  console.log(name, 'changed from', previous, 'to', value);
  return value !== undefined;
};
Object.defineProperty(x, 'foo', xsor('foo', change));

x.foo = 1;
// logs: "foo changed from undefined to 1"
x.foo = 2;
// logs: "foo changed from 1 to 2"

parse: function(value, symbol)

Declares a parse function for the input value, which will be called whenever the value is set, but before the change handler. The return value is the parsed or coerced value. For instance, to coerce values to numbers, you could do:

var xsor = require('xsor');
var assert = require('assert');

var Thing = function() {
};

Object.defineProperty(MyClass.prototype, 'size', xsor({
  parse: Number,
  initial: 0
});

var instance = new Thing();
instance.size = '1';
assert.strictEqual(instance.size, 1);

enumerable: true

By default, descriptors returned by xsor() are not enumerable, which means that they won't be returned by Object.keys() or iterated over by for..in loops. You can make them enumerable by passing enumerable: true.