-
Notifications
You must be signed in to change notification settings - Fork 5
/
esq.js
84 lines (69 loc) · 2.25 KB
/
esq.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
(function() {
var root = this;
var previous_ESQ = root.ESQ;
var ESQ = function() {
this._query = Object.create(null);
};
ESQ.prototype.getQuery = function() {
return this._query;
};
ESQ.prototype.query = function() {
var args = this._getArgsAsArray(arguments);
var value = args.pop();
this._createNestedObject(this._query, args, value);
return this._query;
};
ESQ.prototype._createNestedObject = function(base, names, value) {
var lastName = arguments.length === 3 ? names.pop() : false;
for (var i=0; i<names.length; i++) {
if(names[i] instanceof Array) {
names[i] = names[i][0];
base[names[i]] = base[names[i]] || [];
base = base[names[i]][base[names[i]].length] = Object.create(null);
} else {
base = base[names[i]] = base[names[i]] || Object.create(null);
}
}
if (lastName) {
if (lastName instanceof Array) value = [value];
if (value instanceof Array) {
base[lastName] = base[lastName] ? base[lastName].concat(value) : value;
} else if (value instanceof Object) {
base[lastName] ? this._extend(base[lastName], value) : base[lastName] = value;
}
base = base[lastName] = ((typeof base[lastName] === "object") ? base[lastName] : undefined) ||
((value === null || typeof value === "undefined") ? Object.create(null) : value);
}
if ((names.length == 0) && !lastName && value) {
base = value;
}
};
ESQ.prototype._getArgsAsArray = function(args) {
if (args && (args instanceof Array)) return args;
return Array.prototype.slice.call(args);
};
ESQ.prototype._extend = function(obj) {
var args = Array.prototype.slice.call(arguments, 1);
args.forEach(function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
ESQ.noConflict = function() {
root.ESQ = previous_ESQ;
return ESQ;
};
if (typeof define !== 'undefined' && define.amd) { //require.js
define([], function () {
return ESQ;
});
} else if (typeof module !== 'undefined' && module.exports) { //nodejs
module.exports = ESQ;
} else { //direct browser require
root.ESQ = ESQ;
}
}).call(this);