GithubHelp home page GithubHelp logo

propo's Introduction

propo

This module exposes a single function, a functional helper to define chained getter / setter on an object.

Eases the process of building a chaining API.

Install

npm install propo

Usage

On a prototype.

var prop = require('propo');

function Stuff() {}
prop(Stuff.prototype)('src');
prop(Stuff.prototype)('dest');
prop(Stuff.prototype)('output');


var stuff = new Stuff();

stuff
  .src('file.json')
  .dest('tmp/output.json')
  .output('log/stdout.log');

console.log(stuff.src());
// => file.json

console.log(stuff._src);
// => file.json

Hash / Object.create

var obj = {};
prop(obj)('src');
prop(obj)('dest');
prop(obj)('output');

Object.create(obj)
  .src('file.json')
  .dest('tmp/output.json')
  .output('log/stdout.log')

Using a forEach iterator

['url', 'auth', 'file', 'action'].forEach(prop(Stuff.prototype));

Getter / Setter strategy

A way to get and set props in and out of an object.

  • _ - Uses "private-like" properties with a leading _. Ex. obj._src
  • attr - Uses a hash object named attributes to hold props. Ex. obj.attributes.src

Custom:

// Using this.options instead

function opts(name, value) {
  this.options = this.options || {};

  if (!value) return this.options[name];
  this.options[name] = value;
  return this;
}

var obj = {};
prop(obj, { strategy: opts })('file');

// Set
obj.file('file.json');

console.log(obj.options.file);
// => file.json

Validation

Used before setting a property, a simple function returning true on proper input.

function validate(name, value) {
  if (!value) return;
  if (!value.name) return;
  if (!value.pass) return;
  return true;
}

var obj = {};
prop(obj, { validate: validate })('auth');

// Wrong input are noop
obj.auth({ foo: 'bar' });

console.log(obj.auth());
// => undefined

// Ok input
obj.auth({
  name: 'foo',
  pass: 'bar'
});

console.log(obj.auth());
// => { name: 'foo', pass: 'bar' });

...

propo's People

Contributors

mklabs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.