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
Demo
Usage |; var publicObject = x: 'Public X' privateObject = y: 'Private Y' ; ; console;// 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 ; x; publicObjectquestion = 6 7;publicObjectanswer = 6 7; console;// Object {answer: 42, question: [6, 7]} console;// 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