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

classing

v0.1.0

Published

Fluent classes for node.js and the browser.

Downloads

1,827

Readme

Classing

Build Status

Easy, flexible classes for JavaScript, works in node and all modern browser (> IE8).

Why another class library?

Because none of the other class libs have a good way to associate meta data with properties, and have that meta data easily available in child classes. This is useful for e.g. defining property labels, types, validation rules etc.

Classing presents an API similar to the native Object.defineProperty() and Object.defineProperties() methods.


var Class = require('classing');

var User = Class();

User.defineProperty('name', {
  label: 'Name',
  value: 'anonymous'
});

User.defineProperties({
  email: {
    label: 'Email Address'
  },
  avatarUrl: {
    label: 'Avatar URL',
    // getter
    get: function () {
      return getGravatarUrl(this.email);
    }
  }

});


var user = new User({
  name: 'charles',
  email: '[email protected]'
});

console.log(User.descriptors.name.label + ':', user.name); // "Name: charles"

console.log(user.avatarUrl);

The main difference between this and the native methods is that the full descriptor declarations are preserved. The native methods discard these extra keys (anything other than enumerable, configurable, writable, value, get and set), making them unsuitable for storing metadata. Classing corrects this and ensures that the descriptors are accessible within child classes.

Installation

Via npm:

npm install --save classing

or bower:

bower install --save classing

Usage

Simple classes

var Class = require('classing');

var Person = Class({
  name: {
    value: 'No Name'
  },
  dateOfBirth: {}
});

var person = new Person();

person.name === 'No Name'; // => true

var person = new Person({
  name: 'Bob',
  dateOfBirth: new Date()
});

Default values

var Collection = Class({
  items: {
    enumerable: false,
    default: function () { return []; }
  },
  length: {
    get: function () {
      return this.items.length;
    }
  },
  push: function () {
    return this.items.push.apply(this.items, arguments);
  }
});

var list = new Collection();

list.length === 0; // true

list.push(1, 2, 3);

list.length === 3; // true

Inheritance

var Vehicle = Class({
  name: {
    value: 'No Name'
  }
});

var RoadVehicle = Vehicle.extend({
  wheels: {
    value: 0
  },
  capacity: {
    value: 0
  },
  capacityPerWheel: {
    get: function () {
      return (this.capacity || 1) / (this.wheels || 1)
    }
  }
});

var Car = RoadVehicle.extend({
  wheels: {
    value: 4
  }
});

var mini = new Car({
  name: 'mini',
  capacity: 28
});

console.log(mini.capacityPerWheel);

Mixins


var Truck = RoadVehicle.extend();

Truck.mixin({
  capacity: 2,
  wheels: 8
});

var truck = new Truck();
console.log(truck.capacityPerWheel);

Auto Binding

var MyConsole = Class.create({
  alert: {
    bind: true,
    value: function (message) {
      this.alertCalledCount++;
      console.warn(message);
    }
  },
  log: {
    bind: console,
    value: console.log
  },
  alertCalledCount: {
    value: 0
  }
});

var myconsole = new MyConsole(),
    alert = myconsole.alert,
    log = myconsole.log;

myconsole.alertCalledCount.should.equal(0);

alert('Hello');
alert('World');

myconsole.alertCalledCount.should.equal(2);

log('If you can see this in the console, it worked.');

Running the tests

First, npm install, then npm test. Code coverage generated with npm run coverage.

License

MIT, see LICENSE.md.