-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.objects.js
executable file
·137 lines (112 loc) · 2.77 KB
/
crafity.objects.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
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
/*jslint bitwise: true, unparam: true, maxerr: 50, white: true */
/*globals window */
/*!
* crafity.objects
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/
(function (crafity) {
"use strict";
var objects = (crafity.objects = {})
, valueTypes = ['string', 'number', 'array', 'date'];
/**
*
* @param obj
* @param fn
*/
objects.forEach = function forEach(obj, fn, thisp) {
thisp = thisp || this;
var member;
if (typeof fn !== "function") {
throw new TypeError();
}
for (member in obj) {
if (obj.hasOwnProperty(member)) {
fn.call(thisp, obj[member], member, obj);
}
}
};
/**
*
* @param obj
* @param fn
* @param thisp
*/
objects.map = function map(obj, fn, thisp) {
thisp = thisp || this;
var member, result = [];
if (typeof fn !== "function") {
throw new TypeError();
}
for (member in obj) {
if (obj.hasOwnProperty(member)) {
result.push(fn.call(thisp, obj[member], member, obj));
}
}
return result;
};
/**
*
* @param obj
* @param extension
*/
objects.extend = function extend(obj, extension) {
objects.forEach(extension, function (value, name) {
obj[name] = extension[name];
});
return obj;
};
/**
*
* @param obj
*/
objects.clone = function clone(obj) {
return objects.extend({}, obj);
};
/**
*
* @param obj1
* @param obj2
*/
objects.areEqual = function (obj1, obj2) {
// False if not the same type
if (typeof obj1 !== typeof obj2) { return false; }
// if they are equal, they are equal
if (obj1 === obj2) { return true; }
// The following types had to be ===
if (~valueTypes.indexOf(typeof obj1)) {
return false;
}
// If one object === null, return false.
// problem with null is that it is an object
if (obj1 === null || obj2 === null) {
return false;
}
// If a function, compare as string
if (obj1 instanceof Function) {
return objects.areEqual(obj1.toString(), obj2.toString());
}
// Compare the left with the right and the right with the left
// The first check is if the right object has all the left properties
// Then the right object should not have more properties
// The content of the members must be the same
var cache = [];
objects.forEach(obj1, function (value, member) {
cache.push(member);
});
objects.forEach(obj2, function (value, member) {
var index = cache.indexOf(member);
// If the member was not found in the cache, stop checking
if (!~index) { return false; }
// Compare the content of the member
if (!objects.areEqual(value, obj1[member])) {
return false;
}
cache.splice(index, 1);
});
// The objects are equal when there is nothing left
return !cache.length;
};
}(window.crafity = window.crafity || {}));