-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
89 lines (75 loc) · 2.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*!
* 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');
function isValidKey(key) {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
}
/**
* 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('.')) {
if (isValidKey(prop)) {
defineProperty(obj, prop, getter);
}
return obj;
}
var keys = prop.split('.').filter(isValidKey);
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;