-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepsortobj.js
166 lines (130 loc) · 4.42 KB
/
deepsortobj.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
158
159
160
161
162
163
164
165
166
/*jslint indent: 2, maxlen: 80, continue: false, unparam: false, node: true */
/* -*- tab-width: 2 -*- */
/*globals define:true */
'use strict';
(function (factory) {
if (('function' === typeof define) && define.amd) { return define(factory); }
if (('object' === typeof module) && module && module.exports) {
module.exports = factory();
}
}(function () {
function undefNoOp() { return; }
var EX, isBuf = undefNoOp;
if ('function' === typeof Buffer) { // may be missing in web browsers
if ('function' === typeof Buffer.isBuffer) {
isBuf = Buffer.isBuffer;
}
}
function sortArrayInplace(keyList) { return keyList.sort(); }
function numCmp(a, b) { return a - b; }
function limitedLastIndexOf(needle, haystack, maxIdx) {
while ((maxIdx >= 0) && (haystack[maxIdx] !== needle)) { maxIdx -= 1; }
return maxIdx;
}
function throwCircRefErr(ref, info) {
var err = 'Encountered a circular reference at ' +
(info ? info.srcPath.join('.') : '<unknown path>');
err = new Error(err);
err.circRef = (info || ref);
throw err;
}
function expectFunc(f) {
var t = typeof f;
if (t === 'function') { return f; }
throw new TypeError('Expected a function. not ' + t);
}
function ifUndef(val, dflt) { return (val === undefined ? dflt : val); }
function parseOpt_sortKeys(val) {
switch (val) {
case undefined:
case true:
return EX.numsLast;
case 'fast':
return sortArrayInplace;
}
return expectFunc(val);
}
function parseOpt_circ(val) {
switch (val) {
case undefined:
case 'copy': // deprecated
case 'congruent':
return false;
case 'ign':
// nothing special here; instead, the ignorance is achieved by
// having an empty sortedParents array.
return false;
case 'err':
return throwCircRefErr;
}
return expectFunc(val);
}
EX = function sortObj(obj, how) {
how = (how || false);
if (typeof how === 'function') { how = { sort: how }; }
var srcPath = [], onCircRef = how.circular, diveUnlessCircular, circIdx,
origParents = (onCircRef === 'ign' ? null : []),
sortedParents = (origParents && []),
isAry = expectFunc(ifUndef(how.isArray, Array.isArray)),
dictKeys = expectFunc(ifUndef(how.dictKeys, EX.dictKeys)),
keyPrefix = (how.keyPrefix || ''),
keySuffix = (how.keySuffix || ''),
sortKeys = parseOpt_sortKeys(how.sortKeys);
onCircRef = parseOpt_circ(onCircRef);
function dive(origDepth, origVal) {
if ((origVal && typeof origVal) !== 'object') { return origVal; }
var maxParentIdx, keys, sorted, subDepth;
if (!isAry(origVal)) {
keys = dictKeys(origVal);
if (!keys) { return origVal; }
}
sorted = (keys ? {} : []);
if (keys && sortKeys) { keys = sortKeys(keys); }
if (origParents) {
origParents[origDepth] = origVal;
sortedParents[origDepth] = sorted;
maxParentIdx = origDepth - 1;
}
subDepth = origDepth + 1;
(keys || origVal).forEach(function (subVal, subKey) {
if (keys) {
subKey = subVal;
subVal = origVal[subKey];
}
srcPath[origDepth] = subKey;
subVal = diveUnlessCircular(subDepth, subVal, maxParentIdx);
if (keys) { subKey = keyPrefix + subKey + keySuffix; }
sorted[subKey] = subVal;
});
return sorted;
}
diveUnlessCircular = (origParents ? function (depth, val, maxParentIdx) {
circIdx = limitedLastIndexOf(val, origParents, maxParentIdx);
if (circIdx < 0) { return dive(depth, val); }
if (!onCircRef) { return sortedParents[circIdx]; }
var shortPath = srcPath.slice(0, depth);
return onCircRef(val, { srcPath: shortPath, ref: val,
parents: origParents, });
} : dive);
return ((obj && typeof obj) === 'object' ? dive(0, obj) : obj);
};
// BEGIN define integer key
EX.intRgx = /^0$|^-?[1-9][0-9]*$/;
// ENDOF define integer key
EX.numsLast = (function () {
return function numsLast(keys) {
var texts = [], nums = [];
keys.forEach(function (k) {
if (EX.intRgx.test(k)) { return nums.push(k); }
texts.push(k);
});
return texts.sort().concat(nums.sort(numCmp));
};
}());
EX.dictKeys = function dictKeys(x) {
return (((x && typeof x) === 'object') && (!(Array.isArray(x)
|| isBuf(x)
)) && Object.keys(x));
};
return EX;
}));