aboutsummaryrefslogtreecommitdiff
path: root/node_modules/sort-keys/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/sort-keys/index.js')
-rw-r--r--node_modules/sort-keys/index.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/node_modules/sort-keys/index.js b/node_modules/sort-keys/index.js
new file mode 100644
index 0000000..f75a0e0
--- /dev/null
+++ b/node_modules/sort-keys/index.js
@@ -0,0 +1,44 @@
+'use strict';
+var isPlainObj = require('is-plain-obj');
+
+module.exports = function (obj, opts) {
+ if (!isPlainObj(obj)) {
+ throw new TypeError('Expected a plain object');
+ }
+
+ opts = opts || {};
+
+ // DEPRECATED
+ if (typeof opts === 'function') {
+ opts = {compare: opts};
+ }
+
+ var deep = opts.deep;
+ var seenInput = [];
+ var seenOutput = [];
+
+ var sortKeys = function (x) {
+ var seenIndex = seenInput.indexOf(x);
+
+ if (seenIndex !== -1) {
+ return seenOutput[seenIndex];
+ }
+
+ var ret = {};
+ var keys = Object.keys(x).sort(opts.compare);
+
+ seenInput.push(x);
+ seenOutput.push(ret);
+
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ var val = x[key];
+
+ ret[key] = deep && isPlainObj(val) ? sortKeys(val) : val;
+ }
+
+ return ret;
+ };
+
+ return sortKeys(obj);
+};