object.js
6.94 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @fileoverview This module has some functions for handling a plain object, json.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var type = require('./type');
var array = require('./array');
/**
* The last id of stamp
* @type {number}
* @private
*/
var lastId = 0;
/**
* Extend the target object from other objects.
* @param {object} target - Object that will be extended
* @param {...object} objects - Objects as sources
* @returns {object} Extended object
* @memberof tui.util
*/
function extend(target, objects) { // eslint-disable-line no-unused-vars
var hasOwnProp = Object.prototype.hasOwnProperty;
var source, prop, i, len;
for (i = 1, len = arguments.length; i < len; i += 1) {
source = arguments[i];
for (prop in source) {
if (hasOwnProp.call(source, prop)) {
target[prop] = source[prop];
}
}
}
return target;
}
/**
* Assign a unique id to an object
* @param {object} obj - Object that will be assigned id.
* @returns {number} Stamped id
* @memberof tui.util
*/
function stamp(obj) {
if (!obj.__fe_id) {
lastId += 1;
obj.__fe_id = lastId; // eslint-disable-line camelcase
}
return obj.__fe_id;
}
/**
* Verify whether an object has a stamped id or not.
* @param {object} obj - adjusted object
* @returns {boolean}
* @memberof tui.util
*/
function hasStamp(obj) {
return type.isExisty(pick(obj, '__fe_id'));
}
/**
* Reset the last id of stamp
* @private
*/
function resetLastId() {
lastId = 0;
}
/**
* Return a key-list(array) of a given object
* @param {object} obj - Object from which a key-list will be extracted
* @returns {Array} A key-list(array)
* @memberof tui.util
*/
function keys(obj) {
var keyArray = [];
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
keyArray.push(key);
}
}
return keyArray;
}
/**
* Return the equality for multiple objects(jsonObjects).<br>
* See {@link http://stackoverflow.com/questions/1068834/object-comparison-in-javascript}
* @param {...object} object - Multiple objects for comparing.
* @returns {boolean} Equality
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var jsonObj1 = {name:'milk', price: 1000};
* var jsonObj2 = {name:'milk', price: 1000};
* var jsonObj3 = {name:'milk', price: 1000};
* util.compareJSON(jsonObj1, jsonObj2, jsonObj3); // true
*
* var jsonObj4 = {name:'milk', price: 1000};
* var jsonObj5 = {name:'beer', price: 3000};
* util.compareJSON(jsonObj4, jsonObj5); // false
*/
function compareJSON(object) {
var argsLen = arguments.length;
var i = 1;
if (argsLen < 1) {
return true;
}
for (; i < argsLen; i += 1) {
if (!isSameObject(object, arguments[i])) {
return false;
}
}
return true;
}
/**
* @param {*} x - object to compare
* @param {*} y - object to compare
* @returns {boolean} - whether object x and y is same or not
* @private
*/
function isSameObject(x, y) { // eslint-disable-line complexity
var leftChain = [];
var rightChain = [];
var p;
// remember that NaN === NaN returns false
// and isNaN(undefined) returns true
if (isNaN(x) &&
isNaN(y) &&
type.isNumber(x) &&
type.isNumber(y)) {
return true;
}
// Compare primitives and functions.
// Check if both arguments link to the same object.
// Especially useful on step when comparing prototypes
if (x === y) {
return true;
}
// Works in case when functions are created in constructor.
// Comparing dates is a common scenario. Another built-ins?
// We can even handle functions passed across iframes
if ((type.isFunction(x) && type.isFunction(y)) ||
(x instanceof Date && y instanceof Date) ||
(x instanceof RegExp && y instanceof RegExp) ||
(x instanceof String && y instanceof String) ||
(x instanceof Number && y instanceof Number)) {
return x.toString() === y.toString();
}
// At last checking prototypes as good a we can
if (!(x instanceof Object && y instanceof Object)) {
return false;
}
if (x.isPrototypeOf(y) ||
y.isPrototypeOf(x) ||
x.constructor !== y.constructor ||
x.prototype !== y.prototype) {
return false;
}
// check for infinitive linking loops
if (array.inArray(x, leftChain) > -1 ||
array.inArray(y, rightChain) > -1) {
return false;
}
// Quick checking of one object beeing a subset of another.
for (p in y) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
}
// This for loop executes comparing with hasOwnProperty() and typeof for each property in 'x' object,
// and verifying equality for x[property] and y[property].
for (p in x) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
if (typeof (x[p]) === 'object' || typeof (x[p]) === 'function') {
leftChain.push(x);
rightChain.push(y);
if (!isSameObject(x[p], y[p])) {
return false;
}
leftChain.pop();
rightChain.pop();
} else if (x[p] !== y[p]) {
return false;
}
}
return true;
}
/* eslint-enable complexity */
/**
* Retrieve a nested item from the given object/array
* @param {object|Array} obj - Object for retrieving
* @param {...string|number} paths - Paths of property
* @returns {*} Value
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var obj = {
* 'key1': 1,
* 'nested' : {
* 'key1': 11,
* 'nested': {
* 'key1': 21
* }
* }
* };
* util.pick(obj, 'nested', 'nested', 'key1'); // 21
* util.pick(obj, 'nested', 'nested', 'key2'); // undefined
*
* var arr = ['a', 'b', 'c'];
* util.pick(arr, 1); // 'b'
*/
function pick(obj, paths) { // eslint-disable-line no-unused-vars
var args = arguments;
var target = args[0];
var i = 1;
var length = args.length;
for (; i < length; i += 1) {
if (type.isUndefined(target) ||
type.isNull(target)) {
return;
}
target = target[args[i]];
}
return target; // eslint-disable-line consistent-return
}
module.exports = {
extend: extend,
stamp: stamp,
hasStamp: hasStamp,
resetLastId: resetLastId,
keys: Object.prototype.keys || keys,
compareJSON: compareJSON,
pick: pick
};