expose-via-accessor

1.1.3 • Public • Published

expose-via-accessor

A simple helper lib that makes it easy to expose the properties of a private object through a public object using ECMAScript 5.1 getter/setter.

Install

$ npm install expose-via-accessor

Usage | Demo

import {expose, via} from 'expose-via-accessor';
 
var publicObject = {
      x: 'Public X'
    },
 
    privateObject = {
      y: 'Private Y'
    };
 
expose (privateObject)
   via (publicObject)
       ();
 
console.log(publicObject);
// Object {x: "Public X", y: "Private Y"}

API

expose(privateObject: Object): void

Indicates the private object to be exposed. Must be used in conjunction with via.

via(publicObject: Object)([options: Object]): void

First invocation indicates the public object to be used. Second invocation attaches getter-setter pairs onto publicObject, each pair pointing to a property on privateObject; a config object can be passed in optionally with the following options available:

options.overwrite: boolean = false

Whether to attach accessors when there exists properties of the same names on publicObject or further up on its prototype chain.

options.transform: Function = x => x

Transform the inputs before setters on publicObject assign them to corresponding properties on privateObject.

Example
var publicObject = {},
 
    privateObject = {
      answer: null
    };
 
expose (privateObject)
   via (publicObject)
       ({transform: x => x.reduce((m, n) => m * n)});
 
publicObject.question = [6, 7];
publicObject.answer = [6, 7];
 
console.log(publicObject);
// Object {answer: 42, question: [6, 7]}
 
console.log(privateObject);
// Object {answer: 42}

Notice that the transformation only applies to properties that are exposed through the attached accessors.

Changelog

See CHANGELOG.md.

License

MIT © G. Kay Lee

Package Sidebar

Install

npm i expose-via-accessor

Weekly Downloads

1

Version

1.1.3

License

MIT

Last publish

Collaborators

  • gsklee