updated sys and created publi
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016, Brian Woodward.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
# set-getter [](https://www.npmjs.com/package/set-getter) [](https://npmjs.org/package/set-getter) [](https://travis-ci.org/doowb/set-getter)
|
||||
|
||||
> Create nested getter properties and any intermediary dot notation (`'a.b.c'`) paths
|
||||
|
||||
## Install
|
||||
|
||||
Install with [npm](https://www.npmjs.com/):
|
||||
|
||||
```sh
|
||||
$ npm install set-getter --save
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
var getter = require('set-getter');
|
||||
```
|
||||
|
||||
set-getter works like [set-value](https://github.com/jonschlinkert/set-value) by adding a property to an object or an object hierarchy using dot notation. The main difference is that the property is added using `Object.defineProperty` and is expected to be a getter function that returns a value.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var obj = {};
|
||||
|
||||
// root level property
|
||||
getter(obj, 'foo', function() {
|
||||
return 'bar';
|
||||
});
|
||||
console.log(obj.foo);
|
||||
//=> 'bar'
|
||||
|
||||
// property dot notation
|
||||
getter(obj, 'bar.baz', function() {
|
||||
return 'qux';
|
||||
});
|
||||
console.log(obj.bar.baz);
|
||||
//=> 'qux'
|
||||
|
||||
// property array notation
|
||||
getter(obj, ['beep', 'boop'], function() {
|
||||
return 'bop';
|
||||
});
|
||||
console.log(obj.beep.boop);
|
||||
//=> 'bop'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### [setGetter](index.js#L27)
|
||||
|
||||
Defines a getter function on an object using property path notation.
|
||||
|
||||
**Params**
|
||||
|
||||
* `obj` **{Object}**: Object to add property to.
|
||||
* `prop` **{String|Array}**: Property string or array to add.
|
||||
* `getter` **{Function}**: Getter function to add as a property.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var obj = {};
|
||||
getter(obj, 'foo', function() {
|
||||
return 'bar';
|
||||
});
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/set-getter/issues/new).
|
||||
|
||||
## Building docs
|
||||
|
||||
Generate readme and API documentation with [verb](https://github.com/verbose/verb):
|
||||
|
||||
```sh
|
||||
$ npm install verb && npm run docs
|
||||
```
|
||||
|
||||
Or, if [verb](https://github.com/verbose/verb) is installed globally:
|
||||
|
||||
```sh
|
||||
$ verb
|
||||
```
|
||||
|
||||
## Running tests
|
||||
|
||||
Install dev dependencies:
|
||||
|
||||
```sh
|
||||
$ npm install -d && npm test
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
**Brian Woodward**
|
||||
|
||||
* [github/doowb](https://github.com/doowb)
|
||||
* [twitter/doowb](http://twitter.com/doowb)
|
||||
|
||||
## License
|
||||
|
||||
Copyright © 2016, [Brian Woodward](https://github.com/doowb).
|
||||
Released under the [MIT license](https://github.com/doowb/set-getter/blob/master/LICENSE).
|
||||
|
||||
***
|
||||
|
||||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 29, 2016._
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*!
|
||||
* set-getter (https://github.com/doowb/set-getter)
|
||||
*
|
||||
* Copyright (c) 2016, Brian Woodward.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var toPath = require('to-object-path');
|
||||
|
||||
/**
|
||||
* Defines a getter function on an object using property path notation.
|
||||
*
|
||||
* ```js
|
||||
* var obj = {};
|
||||
* getter(obj, 'foo', function() {
|
||||
* return 'bar';
|
||||
* });
|
||||
* ```
|
||||
* @param {Object} `obj` Object to add property to.
|
||||
* @param {String|Array} `prop` Property string or array to add.
|
||||
* @param {Function} `getter` Getter function to add as a property.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function setGetter(obj, prop, getter) {
|
||||
var key = toPath(arguments);
|
||||
return define(obj, key, getter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define getter function on object or object hierarchy using dot notation.
|
||||
*
|
||||
* @param {Object} `obj` Object to define getter property on.
|
||||
* @param {String} `prop` Property string to define.
|
||||
* @param {Function} `getter` Getter function to define.
|
||||
* @return {Object} Returns original object.
|
||||
*/
|
||||
|
||||
function define(obj, prop, getter) {
|
||||
if (!~prop.indexOf('.')) {
|
||||
defineProperty(obj, prop, getter);
|
||||
return obj;
|
||||
}
|
||||
|
||||
var keys = prop.split('.');
|
||||
var last = keys.pop();
|
||||
var target = obj;
|
||||
var key;
|
||||
|
||||
while ((key = keys.shift())) {
|
||||
while (key.slice(-1) === '\\') {
|
||||
key = key.slice(0, -1) + '.' + keys.shift();
|
||||
}
|
||||
target = target[key] || (target[key] = {});
|
||||
}
|
||||
|
||||
defineProperty(target, last, getter);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define getter function on object as a configurable and enumerable property.
|
||||
*
|
||||
* @param {Object} `obj` Object to define property on.
|
||||
* @param {String} `prop` Property to define.
|
||||
* @param {Function} `getter` Getter function to define.
|
||||
*/
|
||||
|
||||
function defineProperty(obj, prop, getter) {
|
||||
Object.defineProperty(obj, prop, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get: getter
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose `setGetter`
|
||||
*/
|
||||
|
||||
module.exports = setGetter;
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"_from": "set-getter@^0.1.0",
|
||||
"_id": "set-getter@0.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-12nBgsnVpR9AkUXy+6guXoboA3Y=",
|
||||
"_location": "/set-getter",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "set-getter@^0.1.0",
|
||||
"name": "set-getter",
|
||||
"escapedName": "set-getter",
|
||||
"rawSpec": "^0.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^0.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/lazy-cache"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/set-getter/-/set-getter-0.1.0.tgz",
|
||||
"_shasum": "d769c182c9d5a51f409145f2fba82e5e86e80376",
|
||||
"_spec": "set-getter@^0.1.0",
|
||||
"_where": "/mnt/Data/bach/Documents/ola/OLA#5/ola5doc/publi/node_modules/lazy-cache",
|
||||
"author": {
|
||||
"name": "Brian Woodward",
|
||||
"url": "https://github.com/doowb"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/doowb/set-getter/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"to-object-path": "^0.3.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Create nested getter properties and any intermediary dot notation (`'a.b.c'`) paths",
|
||||
"devDependencies": {
|
||||
"gulp-format-md": "^0.1.9",
|
||||
"mocha": "^2.4.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/doowb/set-getter",
|
||||
"keywords": [],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "set-getter",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/doowb/set-getter.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"verb": {
|
||||
"plugins": [
|
||||
"gulp-format-md"
|
||||
],
|
||||
"reflinks": [
|
||||
"verb",
|
||||
"set-value"
|
||||
],
|
||||
"layout": "default",
|
||||
"toc": false,
|
||||
"tasks": [
|
||||
"readme"
|
||||
],
|
||||
"lint": {
|
||||
"reflinks": true
|
||||
}
|
||||
},
|
||||
"version": "0.1.0"
|
||||
}
|
||||
Reference in New Issue
Block a user