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

sledom

v0.1.1

Published

an unassuming library for creating JavaScript models

Downloads

5

Readme

Sledom

Build Status v 0.1.1

Why should you have to commit to one MVC framework for your app? Haven't we all learned that being modular is better? React has broken out the V. This is the M.

Features

  • field normalization
  • field types, automatic conversion
  • enum fields
  • default field values
  • computed fields
  • validation
  • dirty state
  • event binding

Quick Start

Define a Model

var sledom = require('sledom');
var Person = sledom.define('Person', {
  fields: {
    name: { type: sledom.STRING },
    eyeColor: { type: sledom.STRING },
    age: { type: sledom.NUMBER }
  }
});

Create an Instance

var dude = new Person({
  name: 'Lebowski',
  eyeColor: 'blue',
  age: 42
});

Use the Instance

// Listen for changes.
dude.on(sledom.CHANGE, function (changes) {
  console.log(changes);
});

// Set values.
dude.set({ age: 43 });  // logs { age: 43 }

// Get values.
dude.get();  // { name: 'Lebowski', eyeColor: 'blue', age: 43 }

Now that's just the beginning. Keep scrolling for more.

Sledom is in active development, so your feedback and questions are appreciated. https://github.com/reergymerej/sledom/issues

Definition

var sledom = require('sledom');

var Foo = sledom.define('Foo', {
  idField: 'name',
  fields: {
    name: { type: sledom.STRING },
    bar: { type: sledom.NUMBER, default: 42 },
    baz: { type: sledom.BOOLEAN, default: true }
  }
});

var foo = new Foo({ name: 'asdf', bar: 99 });

Getting Field Values

foo.get(); // { name: 'asdf', bar: 99, baz: true }
foo.get('bar'); // 99
foo.id(); // 'asdf'

Setting Field Values

foo.id('new id');
foo.id(); // 'new id'

foo.set('bar', 123);
foo.get('bar'); // 123

foo.set({
    name: 'dude',
    bar: 3.14,
    baz: false
})
foo.get(); // { name: 'dude', bar: 3.14, baz: false }

Computed Fields

var Foo = models.define('Foo', {
  fields: {
    firstName: {
      type: models.STRING
    },
    lastName: {
      type: models.STRING
    },
    fullName: {
      type: models.STRING,
      value: function (fieldValues) {
        return fieldValues.firstName + ' ' + fieldValues.lastName;
      }
    }
  }
});

var foo = new Foo({ firstName: 'Jeremy', lastName: 'Greer' });

foo.get('fullName'); // 'Jeremy Greer'

ENUM Fields

var Foo = models.define('Foo', {
  fields: {
    color: {
      type: models.ENUM,
      default: 'red',
      values: ['red', 'white', 'blue']
    }
  }
});

var foo = new Foo();

foo.get('color');  // 'red'
foo.set('color', 'purple');  // throws error

Validation

var Foo = models.define('Foo', {
  fields: {
    num: {
      type: models.NUMBER,
      valid: function (fieldValue) {
        return fieldValue > 3;
      }
    }
  }
});

var foo = new Foo();
foo.valid(); // false

foo = new Foo({ num: 4 });
foo.valid(); // true

Observing Changes

foo.on(models.CHANGE, function (values) {
  console.log(values);  // { name: 'new name', number: 8675309 }
});

foo.set({ name: 'new name', number: 8675309 });
foo.set({ name: 'new name' });  // won't fire handler because nothing changed

Check Dirty State

foo = new Foo({ name: 'a foo', bar: 66 });
foo.dirty(); // undefined

foo.set({ name: 'new name' });
foo.dirty(); // { name: 'new name' }

foo.set({ bar: 77 });
foo.dirty(); // { name: 'new name', bar: 77 }

Instance Methods

var Foo = app.define('Model', {
  fields: {
    number: { type: app.NUMBER }
  },

  // instance method
  getNumber: function () {
    return 'The number is ' + this.get('number');
  }
});

var foo = new Foo({ number: 999 });
foo.getNumber(); // 'The number is 999'

Custom Field Types

function Foo() {}
Foo.prototype.proveFooness = function () {
  return 'I am a Foo.';
};

var Model = sledom.define('Model', {
  fields: {
    foo: {
      // custom field type
      type: Foo
    }
  }
});

var model = new Model();

model.get('foo').proveFooness(); // 'I am a Foo.'

Nested Models

var Person = sledom.define('Person', {
  fields: {
    name: { type: sledom.STRING },
    gender: { type: sledom.ENUM, values: ['male', 'female' ] }
  }
});

var Child = sledom.define('Child', {
  fields: {
    name: { type: sledom.STRING },
    parent: { type: Person }
  }
});

var dad = new Person({ name: 'Dad', gender: 'male' });
var daughter = new Child({ name: 'Daughter', parent: dad });

daughter.get('parent') instanceof Person; // true

================================================

Please create an issue for feature requests or to report bugs.

Coming Soon

  • save routines
  • shorthand definitions
  • model inheritence