-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.compare.js
157 lines (137 loc) · 4.19 KB
/
jquery.compare.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* jQuery Compare
*
* jQuery Compare is a simple jQuery plugin that helps compare array and object litterals.
*
* version: 0.7.0
* author: Steve Worley <[email protected]>
* url: http://steveworley.me/jquery-compare
*
* Copyright (C) 2013 Steve Worley.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+function($) {
$.fn.compare = function(compare, opts, cb) {
var passed = true,
isArray = $.isArray(compare),
a = (isArray) ? this : this[0],
b = (isArray) ? $.extend([], compare) : $.extend({}, compare),
$a = $(a),
options = {};
// Set up options
if (typeof opts == 'object') {
$.extend(options, $.fn.compare.defaults, opts);
} else {
$.extend(options, $.fn.compare.defaults);
}
options.success = (arguments.length == 2 && typeof opts == 'function') ? opts
: (typeof cb == 'function') ? cb : options.success;
if (options.sort && isArray) {
if (options.caseSensitive) {
a.sort();
b.sort();
} else {
a.sort($.fn.compare.caseSort);
b.sort($.fn.compare.caseSort)
}
}
var lenA = $.map(a, function(n, i) { return i; }).length,
lenB = $.map(b, function(n, i) { return i; }).length;
if (lenA != lenB && !options.fuzzy) {
passed = false;
}
if (isArray === false) {
if (options.caseSensitive == false) {
var newA = {}, newB = {};
$.map(a, function(val, key) {
newA[key.toLowerCase()] = val;
});
$.map(b, function(val, key) {
newB[key.toLowerCase()] = val;
});
a = newA;
b = newB;
}
for(var key in a) {
if (typeof a[key] !== typeof b[key]) {
passed = false;
} else if (typeof a[key] == 'function') {
passed = a[key].toString() == b[key].toString();
} else if (a[key] instanceof Object && b[key] instanceof Object) {
passed = $(a[key]).compare(b[key]);
} else if (options.caseSensitive == false && typeof a[key] == 'string') {
passed = a[key].toLowerCase() == b[key].toLowerCase();
} else if (a[key] !== b[key]) {
passed = false;
}
if (!passed) {
break;
}
}
} else {
for(var i = 0; i < a.length; i++) {
if (typeof a[i] == 'string' && !options.caseSensitive) {
if (typeof b[i] != 'string' || a[i].toLowerCase() != b[i].toLowerCase()) {
passed = false;
break;
}
} else if (a[i] != b[i]) {
passed = false;
break;
}
}
}
if (!passed) {
$a.compareError = true;
options.error.apply(this, [$a]);
return $a;
}
$a.compareError = false;
options.success.apply(this, [$a]);
return $a;
}
/**
* defaults.
*
* Set the default options for the plugin, this will make the defaults public so
* they can be overridden globally.
*/
$.fn.compare.defaults = {
fuzzy: false,
sort: true,
caseSensitive: true,
success: function() {},
error: function() {}
}
/**
* Callback caseSort().
*
* Determines the sort order of an array regardless of properties case.
*
* @param mixed a
* Index in an array
* @param mixed b
* Index in an array
*
* @return int
* Where b should be inserted into the array in relation to a.
*/
$.fn.compare.caseSort = function(a, b) {
if (typeof a == 'number') {
return a - b;
}
return a.toLowerCase().localeCompare(b.toLowerCase());
}
}(jQuery);