Coverage

71.7%
152
109
43

LineHitsSource

/home/luis/noboxout/js-object-enhancements/index.js

100.0%
3
3
0
LineHitsSource
11(function () {
21 "use strict";
3
41 module.exports = require("/home/luis/noboxout/js-object-enhancements/./lib/objects.js");
5
6}());

/home/luis/noboxout/js-object-enhancements/lib/objects.js

71.1%
149
106
43
LineHitsSource
11(function () {
21 "use strict";
3
41 var hasOwnProperty = Object.prototype.hasOwnProperty,
5 hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
6 dontEnums = [
7 'toString',
8 'toLocaleString',
9 'valueOf',
10 'hasOwnProperty',
11 'isPrototypeOf',
12 'propertyIsEnumerable',
13 'constructor'
14 ],
15 dontEnumsLength = dontEnums.length,
16 ArrayPush = Array.prototype.push,
17 ObjectConstructor = Object.prototype.constructor;
18
191 function __typeof(val) {
2081 if (val === null || val === undefined) {
213 return "null";
22 }
23 // dont deal with undefine...
2478 if (val === true || val === false) {
259 return "boolean";
26 }
27
2869 var type = typeof val;
29
3069 if (type === "object") {
31 // for performance, we check if it's a plain object first
3241 if (type.constructor === ObjectConstructor) {
330 return type;
34 }
35
3641 if (val.push === ArrayPush && val.length != null) {
379 return "array";
38 }
39 // for performance, I will keep this insecure
40 // if (hasOwnProperty.call(val, "callee")) {
4132 if (val.hasOwnProperty && val.hasOwnProperty("callee")) {
423 return "arguments";
43 }
4429 if (val instanceof Date) {
452 return "date";
46 }
4727 if (val instanceof RegExp) {
482 return "regexp";
49 }
50
51 // this is an instance of something?
5228 } else if (type === "number" && isNaN(val)) {
531 return "null";
54 }
55
5652 return type;
57 }
58
59 //
60 // Object
61 //
62
63 // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
641 if (!Object.keys) {
650 Object.keys = function (obj) {
660 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
670 throw new TypeError('Object.keys called on non-object');
68 }
69
700 var result = [], prop, i;
71
720 for (prop in obj) {
730 if (hasOwnProperty.call(obj, prop)) {
740 result.push(prop);
75 }
76 }
77
780 if (hasDontEnumBug) {
790 for (i = 0; i < dontEnumsLength; i++) {
800 if (hasOwnProperty.call(obj, dontEnums[i])) {
810 result.push(dontEnums[i]);
82 }
83 }
84 }
850 return result;
86 };
87 }
88
89 // define Object.defineProperty if not found, no functionality just a replacement so your code not throw!
901 if (!Object.defineProperty) {
910 Object.defineProperty = function (obj, name, prop) {
920 if (prop.get || prop.set) {
930 throw new Error("this is not supported in your js.engine");
94 }
950 obj[name] = prop.value;
96 };
97 }
98
99
100 // define Object.seal if not found, no functionality just a replacement so your code not throw!
1011 if (!Object.seal) {
1020 Object.seal = function (obj) {
1030 return obj;
104 };
105 }
106
107 /**
108 * get the keys of an object (or anything iterable for...in) note: remove prototype key
109 *
110 * @param {Object} object
111 * @param {Function} fn
112 * @returns {Object} object
113 */
1141 if (!Object.each) {
1151 Object.each = function (object, fn) {
1160 var key = null;
117
1180 for (key in object) {
1190 fn(object[key], key);
120 }
121
1220 return object;
123 };
124 }
125
1261 if (!Object.clone) {
1271 Object.clone = function (obj) {
1282 return Object.merge({}, obj, true, false);
129 };
130 }
131
132 /**
133 * merge two object
134 *
135 *
136 * @params {Object} to, this parameter is modified
137 * @params {Object} from
138 * @params {Boolean} clone
139 * @params {Boolean} must_exists do not allow undefined in the objects
140 */
1411 if (!Object.merge) {
1421 Object.merge = function (to, from, clone, must_exists) {
143 //console.log("Object.merge", from);
14441 clone = clone || false;
14541 must_exists = must_exists || false;
146
14741 var ftype = __typeof(from),
148 key,
149 ret;
150
15141 switch (ftype) {
152 case "string":
1533 return clone ? "" + from : from;
154 case "number":
15516 return clone ? 0 + from : from;
156 case "array": // maybe need more deep clone ?
157
1585 if (clone) {
1594 ret = [];
1604 for (key = 0; key < from.length; ++key) {
16115 ret[key] = Object.merge(to[key] || {}, from[key], clone, must_exists);
162 }
163
1644 return ret;
165 }
166
1671 return from;
168 case "boolean":
1695 return clone ? (from ? true : false) : from;
170 case "null":
1710 return null;
172 case "function":
1730 return from;
174 case "object":
175 // to it not an object, overwrite!
17612 ret = __typeof(to) !== "object" ? {} : to || {};
177 // if has prototype just copy
17812 key = null;
179
18012 for (key in from) {
18119 if (key !== "prototype") {
18219 if (ret[key] === undefined) {
18317 if (must_exists) {
1841 continue;
185 }
18616 ret[key] = {};
187 }
18818 ret[key] = Object.merge(ret[key] || {}, from[key], clone, must_exists);
189 }
190 }
191
19212 return ret;
193 case "regexp":
1940 return new RegExp(from.source);
195 case "date":
1960 return clone ? new Date(from) : from;
197 }
198 // unknown type... just return
1990 return from;
200 };
201 }
202
2031 if (!Object.combine) {
2041 Object.combine = function (keys, values) {
2053 values = values || [];
2063 var i,
207 ret = {};
208
2093 for (i = 0; i < keys.length; ++i) {
2106 ret[keys[i]] = values[i] === undefined ? null : values[i];
211 }
2123 return ret;
213 };
214 }
215
2161 if (!Object.ksort) {
2171 Object.ksort = function (from) {
2181 var keys = Object.keys(from),
219 i,
220 ret = {};
221
2221 for (i = 0; i < keys.length; ++i) {
2233 ret[keys[i]] = from[keys[i]];
224 }
225
2261 return ret;
227 };
228 }
229
2301 if (!Object.extend) {
2311 Object.extend = function () {
2320 var target = arguments[0] || {},
233 o,
234 p,
235 i,
236 len;
237
2380 for (i = 1, len = arguments.length; i < len; i++) {
2390 o = arguments[i];
240
2410 if ('object' === typeof o && o !== null) {
2420 for (p in o) {
2430 target[p] = o[p];
244 }
245 }
246 }
247
2480 return target;
249 };
250 }
251
2521 if (!Object.extract) {
2531 Object.extract = function (from, keys, default_value) {
2541 var i,
255 ret = {};
256
2571 default_value = default_value === undefined ? null : default_value;
258
2591 for (i = 0; i < keys.length; ++i) {
2602 ret[keys[i]] = from[keys[i]] === undefined ? default_value : from[keys[i]];
261 }
262
2631 return ret;
264 };
265 }
266
2671 if (!Object.empty) {
2681 Object.empty = function (obj) {
2690 var name;
2700 for (name in obj) {
2710 return false;
272 }
2730 return true;
274 };
275 }
276
2771 if (!Object.depth) {
2781 Object.depth = function (obj) {
2798 var i,
280 max,
281 props = false,
282 d = 0;
283
2848 if (obj === null || obj === undefined) {
2850 return 0;
286 }
287
2888 if (Array.isArray(obj)) {
289 // array
290
2910 for (i = 0, max = obj.length; i < max; ++i) {
2920 d = Math.max(d, Object.depth(obj[i]));
293 }
2940 props = max > 0;
2958 } else if ("object" === typeof obj) {
296 // object
297
2987 for (i in obj) {
2994 props = true;
3004 d = Math.max(d, Object.depth(obj[i]));
301 }
302 }
303
3048 return (props ? 1 : 0) + d;
305 };
306 }
307
3081 if (!Object.rFilter) {
3091 Object.rFilter = function (obj, callback, loop_arrays) {
31012 var i,
311 max;
31212 loop_arrays = loop_arrays === true;
313
31412 if (Array.isArray(obj)) {
315 // array
3163 if (!loop_arrays) {
3172 obj = callback(obj);
318 } else {
3191 for (i = 0, max = obj.length; i < max; ++i) {
3202 obj[i] = Object.rFilter(obj[i], callback, loop_arrays);
321 }
322 }
323
3243 return obj;
325 }
326
3279 if ("object" === typeof obj) {
328 // object
3294 if (!(obj instanceof Date || obj instanceof RegExp)) {
330
3312 for (i in obj) {
3327 obj[i] = Object.rFilter(obj[i], callback, loop_arrays);
333 }
3342 return obj;
335 }
336 }
337
3387 return callback(obj);
339 };
340 }
341
342
3431 module.exports = {
344 __typeof: __typeof
345 };
346
347
348}());