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

active-model

v0.2.0

Published

Data source agnostic opinionated models for Node.js with classic inheritance, basic validations and a clean API

Downloads

20

Readme

ActiveModel

Data source agnostic opinionated models for Node.js with classic inheritance, basic validations and a clean API. Models only keep pre-defined properties on initialization which protect against property leakage and ensure your models remain clean however tey are instantiated.

Api

model(schema)

Create and return a new model based on schema.

Schema

  • attributes: object with allowed attributes as key and validation as value e.g. width: Number
  • constructor: custom constructor for model that will be executed before model initialization
  • methods: methods for the model instances
  • properties: object with properties to define as Object.defineProperty e.g. area: { get: function () {return this.width * this.height } }

Validations

Validations supported at the moment are type validation for Number, String, Boolean, Date, Object, and Array. When values are not of the correct type, the model will try to cast them to the correct one, otherwise it will throw an error.

Example

var model = require('active-model').model;
var assert = require('assert');

var Rectangle = model({
  attributes: {
    height: Number,
    width: Number
  },
  methods: {
    area: function () { return this.width * this.height; }
  },
  properties: {
    perimeter: {
      get: function () {
        return (this.width + this.height) * 2;
      }
    }
  }
});

var rectangle = new Rectangle({ width: 2, height: 3, unknown: true });
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
assert.notEqual(rectangle.unknown, true);
assert.equal(rectangle.area(), 6);
assert.equal(rectangle.perimeter, 10);

It is also possible to configure the attributes, methods, and properties individually.

var model = require('active-model').model;
var assert = require('assert');

var Rectangle = model({
    height: Number,
    width: { type: Number } // support object notation
});

Rectangle.methods({
  area: function () { return this.width * this.height; }
});

Rectangle.properties({
  perimeter: {
    get: function () {
      return (this.width + this.height) * 2;
    }
  }
});

var rectangle = new Rectangle({ width: 2, height: 3, unknown: true });
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
assert.notEqual(rectangle.unknown, true);
assert.equal(rectangle.area(), 6);
assert.equal(rectangle.perimeter, 10);

Use a custom constructor

var model = require('active-model').model;
var assert = require('assert');

var Rectangle = model({
    height: Number,
    width: Number,
    constructor: function (width, height) {
      this.width = width;
      this.height = height;
    }
});

var rectangle = new Rectangle(2, 3);
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);

augment(superclass, properties)

Create a new object inherited form superclass with class methods added to object and properties added to object instances.

Example

var augment = require('active-model').augment;
var Square = augment(Rectangle, {
    constructor: function (side) {
        Rectangle.call(this, side, side);
    }
});

var square = new Square(3)
assert.equal(square.width, 3);
assert.equal(square.height, 3);
assert.equal(square.area(), 9);
assert.equal(square instanceof Rectangle, true);

Credits