hard-prop

1.0.1 • Public • Published

hard-prop

Easy way to create properties with getter and/or setter.

Build Status NPM version

Install

npm install hard-prop

Usage

var prop = require('hard-prop')(object);
 
prop(name, {get: getter, set: setter});
prop(name, {get: getter});
prop(name, {set: setter});
prop(name, getter, setter);
prop(name, setter, getter);
prop(name, getter);
prop(name, setter);

Creates function, that defines properties for given object.

First parameter name must be string.

Second parameter can be object with properties get and/or set for getter and/or setter. In other way one or more functions can be given as parameters. Function with no arguments will be getter, and function with arguments will be setter (only first argument counts).

If only getter given - property will be read-only (error will thrown on write). If only setter given - property will be write-only (error will thrown on read). At least one getter or setter must be given.

Examples

Class way:

const P = require('hard-prop');
 
function Human(firstname, lastname){
    const _p = P(this);
    const [_firstname, _lastname] = [firstname, lastname];
    _p('name',
        () => [_firstname, _lastname].join(' '),
        v => [_firstname, _lastname] = v.split(' ')
    );
}

Closure way:

const P = require('hard-prop');
 
const Human(firstname, lastname) => {
    const _name = {firstname, lastname};
    const _p = P(_name);
    _p('name',
        () => [_name.firstname, _name.lastname].join(' '),
        v => [_firstname, _lastname] = v.split(' ')
    );
}

License

MIT

Package Sidebar

Install

npm i hard-prop

Weekly Downloads

4,690

Version

1.0.1

License

MIT

Unpacked Size

5.7 kB

Total Files

8

Last publish

Collaborators

  • astur