-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
164 lines (159 loc) · 4.18 KB
/
index.js
File metadata and controls
164 lines (159 loc) · 4.18 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @author Gilles Coomans <gilles.coomans@gmail.com>
*/
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define([], function() {
var utils = {};
utils.toPath = function(object, path, value, pathDelimiter, keepOld) {
if (path[0] == "/" || path.substring(0, 1) == "./")
pathDelimiter = "/";
var parts = path.split(pathDelimiter || ".");
if (pathDelimiter == "/" && (parts[0] === "" || parts[0] == "."))
parts.shift();
var tmp = object;
while (parts.length > 1) {
var part = parts.shift();
if (!tmp[part])
tmp[part] = {};
tmp = tmp[part];
}
var last = parts.shift();
if (keepOld && tmp[last]) {
if (!tmp[last].forEach)
tmp[last] = [tmp[last]];
tmp[last].push(value);
} else
tmp[last] = value;
return value;
};
utils.fromPath = function(object, path, pathDelimiter) {
if (!path)
return object;
if (path[0] == "/" || path.substring(0, 1) == "./")
pathDelimiter = "/";
var parts = path.split(pathDelimiter || ".");
if (pathDelimiter == "/" && (parts[0] === "" || parts[0] == "."))
parts.shift();
var tmp = object;
while (parts.length > 1) {
var part = parts.shift();
if (!tmp[part])
return undefined;
tmp = tmp[part];
}
if (tmp)
return tmp[parts.shift()];
else return undefined;
};
utils.deletePropertyByPath = function(object, path, pathDelimiter) {
if (path[0] == "/" || path.substring(0, 1) == "./")
pathDelimiter = "/";
var parts = path.split(pathDelimiter || ".");
if (pathDelimiter == "/" && (parts[0] === "" || parts[0] == "."))
parts.shift();
var tmp = object;
while (parts.length > 1) {
var part = parts.shift();
if (!tmp[part])
return;
tmp = tmp[part];
}
delete tmp[parts.shift()];
};
/**
* clone a function and copy it's proto or vars.
* @method cloneFunction
* @static
* @param {Function} fct the function to copy
* @return {Function} the cloned function
*/
utils.cloneFunction = function(fct) {
//console.log("cloneFunction : fct.decorator = ", fct.decorator)
var clone = function() {
return fct.apply(this, arguments);
};
clone.prototype = fct.prototype;
for (var property in fct)
if (fct.hasOwnProperty(property))
clone[property] = utils.copy(fct[property]);
return clone;
};
/**
* copy any object/value/array deeply. (e.g. any array will be copied AND also its items).
* Any function encountered will not be cloned (simply use same ref). (just deep decorators will be)
* @method copy
* @static
* @param {Object|Primitive} obj
* @return {Object|Primitive} the copied value/object/array
*/
var copy = utils.copy = function(obj, noClone, excludeGrounds) {
//console.log("utils.copy : ", obj, noClone, excludeGrounds)
if (!obj)
return obj;
var res = null;
if (!noClone && typeof obj._clone === 'function')
return obj._clone();
if (obj.forEach) {
if (obj._deep_shared_)
return obj;
res = [];
var len = obj.length;
for (var i = 0; i < len; ++i) {
var e = obj[i];
if (typeof e === 'object')
res.push(copy(e));
else
res.push(e);
}
} else if (typeof obj === 'object') {
if (obj._deep_shared_)
return obj;
if (obj instanceof RegExp)
return obj;
if (obj instanceof Date)
return new Date(obj.valueOf());
res = {};
for (var j in obj) {
if (j == "_backgrounds" || j == "_foregrounds" || j == "_transformations")
if (excludeGrounds)
continue;
else // clone ground's array and skip recursive call
{
res[j] = obj[j].slice();
continue;
}
var v = obj[j];
//if(obj.hasOwnProperty(j))
if (typeof v === 'object')
res[j] = copy(v);
else
res[j] = v;
}
} else if (typeof obj === 'function') {
if (obj._deep_composer_)
res = utils.cloneFunction(obj);
else
res = obj; //utils.cloneFunction(obj);
} else
res = obj;
return res;
};
utils.shallowCopy = function(obj) {
if (obj && obj.forEach)
return obj.slice();
if (obj && typeof obj === 'object') {
if (obj instanceof RegExp)
return obj;
if (obj instanceof Date)
return new Date(obj.valueOf());
var res = {};
for (var i in obj)
res[i] = obj[i];
return res;
}
return obj;
};
return utils;
});