object_utils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var stringUtils = require('utils/string_utils');
  19. var types = {
  20. 'get': function(prop) {
  21. return Object.prototype.toString.call(prop);
  22. },
  23. 'object': '[object Object]',
  24. 'array': '[object Array]',
  25. 'string': '[object String]',
  26. 'boolean': '[object Boolean]',
  27. 'number': '[object Number]'
  28. };
  29. module.exports = {
  30. isChild: function(obj)
  31. {
  32. for (var k in obj) {
  33. if (obj.hasOwnProperty(k)) {
  34. if (obj[k] instanceof Object) {
  35. return false;
  36. }
  37. }
  38. }
  39. return true;
  40. },
  41. recursiveKeysCount: function(obj) {
  42. if (!(obj instanceof Object)) {
  43. return null;
  44. }
  45. var self = this;
  46. function r(obj) {
  47. var count = 0;
  48. for (var k in obj) {
  49. if(self.isChild(obj[k])){
  50. count++;
  51. } else {
  52. count += r(obj[k]);
  53. }
  54. }
  55. return count;
  56. }
  57. return r(obj);
  58. },
  59. deepEqual: function() {
  60. var i, l, leftChain, rightChain;
  61. var values = arguments;
  62. function compare2Objects (x, y) {
  63. var p;
  64. if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
  65. return true;
  66. }
  67. if (x === y) {
  68. return true;
  69. }
  70. if ((typeof x === 'function' && typeof y === 'function') ||
  71. (x instanceof Date && y instanceof Date) ||
  72. (x instanceof RegExp && y instanceof RegExp) ||
  73. (x instanceof String && y instanceof String) ||
  74. (x instanceof Number && y instanceof Number)) {
  75. return x.toString() === y.toString();
  76. }
  77. if (!(x instanceof Object && y instanceof Object)) {
  78. return false;
  79. }
  80. if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
  81. return false;
  82. }
  83. if (x.constructor !== y.constructor) {
  84. return false;
  85. }
  86. if (x.prototype !== y.prototype) {
  87. return false;
  88. }
  89. if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
  90. return false;
  91. }
  92. for (p in y) {
  93. if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
  94. return false;
  95. }
  96. else if (typeof y[p] !== typeof x[p]) {
  97. return false;
  98. }
  99. }
  100. for (p in x) {
  101. if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
  102. return false;
  103. }
  104. else if (typeof y[p] !== typeof x[p]) {
  105. return false;
  106. }
  107. switch (typeof (x[p])) {
  108. case 'object':
  109. case 'function':
  110. leftChain.push(x);
  111. rightChain.push(y);
  112. if (!compare2Objects (x[p], y[p])) {
  113. return false;
  114. }
  115. leftChain.pop();
  116. rightChain.pop();
  117. break;
  118. default:
  119. if (x[p] !== y[p]) {
  120. return false;
  121. }
  122. break;
  123. }
  124. }
  125. return true;
  126. }
  127. if (arguments.length < 1) {
  128. return true;
  129. }
  130. for (i = 1, l = arguments.length; i < l; i++) {
  131. leftChain = [];
  132. rightChain = [];
  133. if (!compare2Objects(arguments[0], arguments[i])) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. },
  139. recursiveTree: function(obj) {
  140. if (!(obj instanceof Object)) {
  141. return null;
  142. }
  143. var self = this;
  144. function r(obj,parent) {
  145. var leaf = '';
  146. for (var k in obj) {
  147. if(self.isChild(obj[k])){
  148. leaf += k + ' ('+parent+')' + '<br/>';
  149. } else {
  150. leaf += r(obj[k],parent +'/' + k);
  151. }
  152. }
  153. return leaf;
  154. }
  155. return r(obj,'');
  156. },
  157. /**
  158. *
  159. * @param {object|array|object[]} target
  160. * @param {object|array|object[]} source
  161. * @param {function} handler
  162. * @returns {object|array|object[]}
  163. */
  164. deepMerge: function(target, source, handler) {
  165. if (typeof target !== 'object' || typeof source !== 'object') return target;
  166. var handlerOpts = Array.prototype.slice.call(arguments, 3);
  167. var isArray = Em.isArray(source);
  168. var ret = handler && typeof handler.apply(this, [target, source].concat(handlerOpts)) !== 'undefined' ?
  169. handler(target, source) :
  170. isArray ? [] : {};
  171. var self = this;
  172. // handle array
  173. if (isArray) {
  174. target = target || [];
  175. ret = ret.concat(target);
  176. if (types.object === types.get(target[0])) {
  177. ret = self.smartArrayObjectMerge(target, source);
  178. } else {
  179. for(var i = 0; i < source.length; i++) {
  180. if (typeof ret[i] === 'undefined') {
  181. ret[i] = source[i];
  182. } else if (typeof source[i] === 'object') {
  183. ret[i] = this.deepMerge(target[i], source[i], handler, target, source);
  184. } else {
  185. if (target.indexOf(source[i]) === -1) {
  186. ret.push(source[i]);
  187. }
  188. }
  189. }
  190. }
  191. } else {
  192. if (target && typeof target === 'object') {
  193. Em.keys(target).forEach(function(key) {
  194. ret[key] = target[key];
  195. });
  196. }
  197. Em.keys(source).forEach(function(key) {
  198. // handle value which is not Array or Object
  199. if (typeof source[key] !== 'object' || !source[key]) {
  200. ret[key] = source[key];
  201. } else {
  202. if (!target[key]) {
  203. ret[key] = source[key];
  204. } else {
  205. ret[key] = self.deepMerge(target[key], source[key], handler, target, source);
  206. }
  207. }
  208. });
  209. }
  210. return ret;
  211. },
  212. /**
  213. * Find objects by index key (@see detectIndexedKey) and merge them.
  214. *
  215. * @param {object[]} target
  216. * @param {object[]} source
  217. * @returns {object[]}
  218. */
  219. smartArrayObjectMerge: function(target, source) {
  220. // keep the first object and take all keys that contains primitive value
  221. var id = this.detectIndexedKey(target);
  222. var self = this;
  223. // when uniq key not found let's merge items by the key itself
  224. if (!id) {
  225. source.forEach(function(obj) {
  226. Em.keys(obj).forEach(function(objKey) {
  227. var ret = self.objectByRoot(objKey, target);
  228. if (!Em.isNone(ret)) {
  229. if ([types.object, types.array].contains(types.get(ret))) {
  230. target[objKey] = self.deepMerge(obj[objKey], ret);
  231. } else {
  232. target[objKey] = ret;
  233. }
  234. } else {
  235. var _obj = {};
  236. _obj[objKey] = obj[objKey];
  237. target.push(_obj);
  238. }
  239. });
  240. });
  241. return target;
  242. }
  243. return target.mapProperty(id).concat(source.mapProperty(id)).uniq().map(function(value) {
  244. if (!target.someProperty(id, value)) {
  245. return source.findProperty(id, value);
  246. } else if (!source.someProperty(id, value)) {
  247. return target.findProperty(id, value);
  248. }
  249. return self.deepMerge(target.findProperty(id, value), source.findProperty(id, value));
  250. });
  251. },
  252. /**
  253. * Determines key with uniq value. This key will be used to find correct objects in target and source to merge.
  254. *
  255. * @param {object} target
  256. * @returns {string|undefined}
  257. */
  258. detectIndexedKey: function(target) {
  259. var keys = Em.keys(target[0]).map(function(key) {
  260. if ([types.object, types.array].contains(types.get(target[0][key]))) {
  261. return null;
  262. }
  263. return key;
  264. }).compact();
  265. return keys.filter(function(key) {
  266. var values = target.mapProperty(key);
  267. return values.length === values.uniq().length;
  268. })[0];
  269. },
  270. /**
  271. *
  272. * @param {string} rootKey
  273. * @param {object[]} target
  274. */
  275. objectByRoot: function(rootKey, target) {
  276. return target.map(function(item) {
  277. return item[rootKey] || null;
  278. }).compact()[0];
  279. }
  280. };