-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (59 loc) · 1.45 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
// UMD: https://github.com/umdjs/umd/blob/master/returnExports.js
(function (root, factory) {
/* global define: false */
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.compiledAccessors = factory();
}
}(this, function () {
'use strict';
/* jshint evil: true */
var is = (function () {
var toS = Object.prototype.toString;
toS = toS.call.bind(toS);
var _ = function (ref) {
ref = toS(ref);
return function (val) {
return toS(val) === ref;
};
};
return {
array: _([]),
string: _(''),
};
})();
var pathToString = function (path) {
if (!is.array(path)) {
if (!is.string(path)) {
path = ''+ path;
}
path = path.split('.');
}
return '['+ path.map(JSON.stringify).join('][') +']';
};
var getter = function (path) {
return new Function(
'obj',
'return obj'+ pathToString(path)
);
};
var setter = function (path) {
return new Function(
'obj', 'val',
'obj'+ pathToString(path) +'= val'
);
};
return {
getter: getter,
setter: setter,
};
}));