-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (48 loc) · 1.67 KB
/
index.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
var unlike = module.exports = function(input,example,options,key){
"use strict";
var i, diff;
if(options && options.nullAllowed && input === null) return false;
if(options && options.undefinedLikeEmptyArray && typeof(input) === "undefined" && Array.isArray(example)) return false;
if(!example) return false;
//different value types
if(typeof(input) !== typeof(example)){
ret = {
input:typeof(input),
example:typeof(example)
};
if(typeof(key) === "number") ret.index = key;
else ret.key = key;
return ret;
}
//object and array or vice versa
else if(typeof(input) === typeof(example) && Array.isArray(input) !== Array.isArray(example)){
return {
key:key,
input:Array.isArray(input) ? "array" : "object",
example:Array.isArray(example) ? "array" : "object"
};
}
//both objects, check unlike for all keys
else if(typeof(example) === "object" && !Array.isArray(example)){
if(input === null || example === null) return false;
var keys = Object.keys(example);
for(i=0; i< keys.length; i++){
var k = keys[i];
diff = unlike(input[k],example[k],options,k);
if(diff) break;
}
}
//both arrays
else if(Array.isArray(example)){
if(example.length === 0) return false;
for(i=0; i< input.length; i++){
diff = unlike(input[i],example[0],options,i);
if(diff) break;
}
}
if(!diff) return false;
if(typeof(key) === "undefined") return diff;
var ret = {};
ret[String(key)] = diff;
return ret;
};