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

arya

v0.0.5

Published

A generic, lightweight, immutable data-layer for javascript

Downloads

19

Readme

Arya

The safest data layer for javascript. Complete with immutable properties, validation/error scheme Written with ES6 specificiations

Install

npm install --save arya

Prototype Methods

Usage

Extend the Arya constructor to create your new class

class Person extends Arya {
 constructor(name, age) {
   super();
   this.register('name', name);
   this.register('age', age);
 }
}

Person.prototype.valid_name = {
 func: function(name) {
   return name && name.length > 3;
 },
 error: 'Name must be atleast 4 characters'
};

Person.prototype.valid_age = {
 func: function(age) {
   return !isNaN(age) && age >= 0 && age <= 1000;
 },
 error: 'Age must be a number between 0 and 1000'
};

var odin = new Person('odin', 1000);
odin.isValid() // true
odin.name() // 'odin'
odin.age() // 1000
odin.name('BAD') // 'BAD'
odin.name.valid() // false
odin.isValid() // false
odin.errors() // { name: 'Name must be atleast 4 characters' };

Arya.prototype.noop(attribute)

object: a nooped object to that is used internally as a default setting for attributes

example

Arya.prototype.noop('age');
//returns
{
  func: function() { return true; },
  error: 'Invalid age'
}

Arya.prototype.errors()

object: an object of errors if any of the attributes on the current object have errors

example

var odin = new Person('odin', 1001);
odin.errors()
// returns
{
  age: 'Age must be a number between 0 and 1000'
}

Arya.prototype.isValid()

boolean: Checks if any properties have any errors

example

var odin = new Person('odin', 1001);
odin.isValid()
// returns
false

Arya.prototype.toJSON()

object: Converts the respective attributes into a simple javascript object

example

var odin = new Person('odin', 1001);
odin.toJSON()
// returns
{
  name: 'odin',
  age: 1001
}

Arya.prototype.register(attribute, value)

Where most of the magic happens. Call this inside your constructor to overload attribute as a method with more methods!

example

var p = new Person('odin', 1001);
p.name
// return
{ name:
  { [Function]
    valid: [Function: valid],
    error: [Function: error],
    validate: [Function: validate]
  }
}

Instance Methods

These methods are loaded onto each property when the prototype register method is called for that property.

instance[prop].valid([,boolean])

boolean: returns whether or not given property is valid. If argument is given, it will set the current validity to that argument. It gets reset the next time the validation function executes.

example

var p = new Person('foo', 100);

p.name.valid();
// return false

p.name.valid(true);
// return true

p.name.validate();
// return false

p.name.valid();
// return false

instance[prop].error()

string: Returns the error message for the given property, if the property is invalid. Default: 'Invalid ' + prop.

example

var p = new Person('foo', 1000);

p.error();
// return 'Name must be atleast 4 characters

instance[prop].validate()

boolean: calls the validation function for the respective property and returns if valid

example

p = new Person('Odin', 100);

p.name.valid();
// return true
p.name.validate();
// return true

p.name.valid(false)
// return false

p.name.validate()
// return true
p.name.valid()
// return true

Validation scheme

How do you add a validation function and error message to a property? The convention currently in place is to just add those methods/error message to the prototype using the keyword valid_ + attribute.
The validation function gets called everytime the attribute changes. The error message gets printing out when calling instance[prop].error() if the attribute is invalid.

example

class Person extends Arya {
  constructor(name) {
    super();
    this.register('name', name);
  }
}

Person.prototype.valid_name = {
  func: function(name) {
    return name && name.length > 2;
  },
  error: 'Name must be at least 3 characters'
};

Contributing

Issues, and pull requests welcome

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/my-new-feature)
  5. Create a new Pull Request