@znemz/sort-object 3.0.7 → 3.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,117 +1,143 @@
1
- # sort-object [![NPM version](https://img.shields.io/npm/v/sort-object.svg)](https://www.npmjs.com/package/sort-object) [![Build Status](https://img.shields.io/travis/nmccready/sort-object.svg)](https://app.travis-ci.com/github//nmccready/sort-object)
1
+ # @znemz/sort-object
2
2
 
3
- > Sort the keys in an object.
3
+ [![npm version](https://img.shields.io/npm/v/@znemz/sort-object.svg)](https://www.npmjs.com/package/@znemz/sort-object)
4
4
 
5
- ## Install
5
+ > Sort the keys in an object. TypeScript-first, ESM, maintained fork of [sort-object](https://github.com/doowb/sort-object).
6
+
7
+ ## Why this fork?
6
8
 
7
- Install with [npm](https://www.npmjs.com/):
9
+ - **TypeScript** — Full type definitions included
10
+ - ✅ **ESM** — Native ES modules with `type: "module"`
11
+ - ✅ **Maintained** — Active maintenance, modern tooling (vitest, eslint 9)
12
+ - ✅ **New features** — Deep/recursive sort, input validation
13
+ - ✅ **Node 20+** — Modern engine target
14
+
15
+ ## Install
8
16
 
9
17
  ```sh
10
- $ npm i sort-object --save
18
+ npm i @znemz/sort-object
11
19
  ```
12
20
 
13
21
  ## Usage
14
22
 
15
- ```js
16
- var sortObj = require("sort-object");
17
- ```
23
+ ### TypeScript / ESM
18
24
 
19
- By default, the keys on an object will be sorted in ascending order:
25
+ ```ts
26
+ import sortObj from "@znemz/sort-object";
27
+ // or: import { sort } from "@znemz/sort-object";
20
28
 
21
- ```js
22
- sortObj({ a: 1, c: 2, b: 3 });
23
- //=> {a: 1, b: 3, c: 2}
29
+ const sorted = sortObj({ a: 1, c: 2, b: 3 });
30
+ // => { a: 1, b: 3, c: 2 }
24
31
  ```
25
32
 
26
- The second param can be an object of `options` OR an array of `keys`:
33
+ ### With options
27
34
 
28
- **object**
35
+ ```ts
36
+ import sortObj, { type SortOptions } from "@znemz/sort-object";
29
37
 
30
- ```js
31
- sortObj({ a: 1, c: 2, b: 3 }, { keys: ["a", "b"] });
32
- //=> {a: 1, b: 3}
38
+ const options: SortOptions = {
39
+ sortOrder: "desc",
40
+ };
41
+
42
+ sortObj({ a: 1, c: 2, b: 3 }, options);
43
+ // => { c: 2, b: 3, a: 1 }
33
44
  ```
34
45
 
35
- **array**
46
+ ## API
36
47
 
37
- ```js
38
- sortObj({ a: 1, c: 2, b: 3 }, ["a", "c"]);
39
- //=> {a: 1, c: 2}
40
- ```
48
+ ### `sort(obj, options?)`
41
49
 
42
- ## Options
50
+ Returns a **new object** with sorted keys (does not mutate the original).
43
51
 
44
- - `keys` {Array} The returned object will contain only the specified keys, in the same order.
45
- - `sort` {Function} Sort function to sort the keys using JavaScript's `.sort()` method.
46
- - `sortOrder` {String} Valid values are `desc` or `asc`, case insensitive.
47
- - `sortBy` {String} Sort function that is passed the entire object, rather than just the keys - as with the `.sort()` method.
52
+ #### Options
48
53
 
49
- ### options.keys
54
+ | Option | Type | Description |
55
+ |---|---|---|
56
+ | `keys` | `string[]` | Only include these keys, in this order |
57
+ | `sort` | `(a, b) => number` | Custom sort function for keys |
58
+ | `sortOrder` | `'asc' \| 'desc'` | Sort direction (case insensitive) |
59
+ | `sortBy` | `(obj) => string[]` | Function returning array of keys to sort by |
60
+ | `prop` | `string` | Property path to sort by (for nested values) |
61
+ | `get` | `(val, prop?) => unknown` | Custom getter for sort values |
62
+ | `deep` | `boolean` | Recursively sort nested objects |
50
63
 
51
- Create a new object with only the given keys.
64
+ The second parameter can also be an **array of keys** (shorthand for `{ keys: [...] }`):
52
65
 
53
- ```js
54
- var o = { a: 1, c: 2, e: 5, d: 4, b: 3 };
55
- sortObj(o, { keys: ["a", "b"] });
66
+ ```ts
67
+ sortObj({ a: 1, c: 2, b: 3 }, ["c", "a", "b"]);
68
+ // => { c: 2, a: 1, b: 3 }
69
+ ```
56
70
 
57
- //=> {a: 1, b: 3}
71
+ ### options.keys
72
+
73
+ Create a new object with only the given keys:
74
+
75
+ ```ts
76
+ sortObj({ a: 1, c: 2, e: 5, d: 4, b: 3 }, { keys: ["a", "b"] });
77
+ // => { a: 1, b: 3 }
58
78
  ```
59
79
 
60
80
  ### options.sort
61
81
 
62
- Function to be passed to javascript's `.sort()` method:
82
+ Custom sort function passed to JavaScript's `.sort()`:
63
83
 
64
- ```js
65
- var o = { a: 1, c: 2, e: 5, d: 4, b: 3 };
66
- var obj = sortObj(o, {
67
- sort: function (a, b) {
68
- return a < b ? -1 : 1;
69
- },
84
+ ```ts
85
+ sortObj({ a: 1, c: 2, e: 5, d: 4, b: 3 }, {
86
+ sort: (a, b) => (a < b ? -1 : 1),
70
87
  });
71
- obj;
72
- //=> {a: 1, b: 3, c: 2, d: 4, e: 5}
88
+ // => { a: 1, b: 3, c: 2, d: 4, e: 5 }
73
89
  ```
74
90
 
75
91
  ### options.sortOrder
76
92
 
77
- Valid values are `desc` or `asc`, case insensitive:
93
+ Sort ascending or descending:
78
94
 
79
- ```js
80
- var o = { a: 1, c: 2, e: 5, d: 4, b: 3 };
81
- sortObj(o, { sortOrder: "ASC" });
82
- //=> {e: 5, d: 4, c: 3, b: 2, a: 1}
95
+ ```ts
96
+ sortObj({ a: 1, c: 2, b: 3 }, { sortOrder: "desc" });
97
+ // => { c: 2, b: 3, a: 1 }
83
98
  ```
84
99
 
85
100
  ### options.sortBy
86
101
 
87
102
  Function that returns an array of keys to sort by:
88
103
 
89
- ```js
90
- var old = { one: "aa", two: "bc", three: "ab" };
91
- var o = sortObj(old, {
92
- sortBy: function (obj) {
93
- var arr = [];
94
- Object.keys(obj).filter(function (key) {
95
- if (/^a/.test(obj[key])) arr.push(key);
96
- });
97
- return arr.reverse();
98
- },
104
+ ```ts
105
+ const old = { one: "aa", two: "bc", three: "ab" };
106
+ sortObj(old, {
107
+ sortBy: (obj) =>
108
+ Object.keys(obj)
109
+ .filter((key) => /^a/.test(obj[key] as string))
110
+ .reverse(),
99
111
  });
100
- //=> {three: 'ab', one: 'aa'}
112
+ // => { three: 'ab', one: 'aa' }
101
113
  ```
102
114
 
103
- ## Author
115
+ ### options.deep
104
116
 
105
- **Brian Woodward**
117
+ Recursively sort nested objects:
106
118
 
107
- - [github/nmccready](https://github.com/nmccready)
108
- - [twitter/nmccready](http://twitter.com/nmccready)
119
+ ```ts
120
+ sortObj(
121
+ { c: { z: 1, a: 2 }, a: { y: 3, b: 4 } },
122
+ { deep: true },
123
+ );
124
+ // => { a: { b: 4, y: 3 }, c: { a: 2, z: 1 } }
125
+ ```
109
126
 
110
- ## License
127
+ ## Notes
128
+
129
+ - **Immutable**: Always returns a new object; the input is never mutated.
130
+ - **Input validation**: Throws `TypeError` for non-object inputs (null, arrays, primitives).
131
+ - **Prototype-safe**: Only own properties are sorted; inherited properties are ignored.
111
132
 
112
- Copyright © 2014-2016 [Brian Woodward](https://github.com/nmccready)
113
- Released under the MIT license.
133
+ ## Original Author
114
134
 
115
- ---
135
+ **Brian Woodward** — [github/doowb](https://github.com/doowb)
136
+
137
+ ## Maintainer
138
+
139
+ **Nick McCready** — [github/nmccready](https://github.com/nmccready)
140
+
141
+ ## License
116
142
 
117
- _This file was generated by [verb](https://github.com/verbose/verb) on February 03, 2016._
143
+ MIT © [Brian Woodward](https://github.com/doowb), [Nick McCready](https://github.com/nmccready)
package/lib/index.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ /*!
2
+ * sort-object <https://github.com/helpers/sort-object>
3
+ *
4
+ * Copyright (c) 2014-2015, Brian Woodward.
5
+ * Licensed under the MIT License
6
+ */
7
+ /**
8
+ * Sort function type - compares two values for ordering.
9
+ */
10
+ export type SortFunction = (a: string, b: string) => number;
11
+ /**
12
+ * Options for the sort function.
13
+ */
14
+ export interface SortOptions<T extends Record<string, unknown> = Record<string, unknown>> {
15
+ /** Array of keys to include in the result (in order). */
16
+ keys?: string[];
17
+ /** Custom sort function for keys. */
18
+ sort?: SortFunction;
19
+ /** Sort order: 'asc' or 'desc' (case insensitive). */
20
+ sortOrder?: "asc" | "desc" | "ASC" | "DESC" | string;
21
+ /** Function that returns an array of keys to sort by. */
22
+ sortBy?: (obj: T) => string[];
23
+ /** Property path to sort by (for objects with nested values). */
24
+ prop?: string;
25
+ /** Custom getter function for extracting sort values. */
26
+ get?: (val: unknown, prop?: string) => unknown;
27
+ /** If true, recursively sort nested objects. */
28
+ deep?: boolean;
29
+ }
30
+ /**
31
+ * Sort the keys in an object.
32
+ *
33
+ * @param obj - The object to sort.
34
+ * @param options - Sort options, or an array of keys.
35
+ * @returns A new object with sorted keys.
36
+ */
37
+ export declare function sort<T extends Record<string, unknown>>(obj: T, options?: SortOptions<T> | string[]): Partial<T>;
38
+ export default sort;
39
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACtF,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,qCAAqC;IACrC,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB,sDAAsD;IACtD,SAAS,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;IACrD,yDAAyD;IACzD,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,MAAM,EAAE,CAAC;IAC9B,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yDAAyD;IACzD,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC;IAC/C,gDAAgD;IAChD,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpD,GAAG,EAAE,CAAC,EACN,OAAO,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,GAClC,OAAO,CAAC,CAAC,CAAC,CAwEZ;AAwCD,eAAe,IAAI,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,108 @@
1
+ /*!
2
+ * sort-object <https://github.com/helpers/sort-object>
3
+ *
4
+ * Copyright (c) 2014-2015, Brian Woodward.
5
+ * Licensed under the MIT License
6
+ */
7
+ import isObject from "is-extendable";
8
+ import sortDesc from "sort-desc";
9
+ import bytewise from "bytewise";
10
+ import union from "@znemz/union-value";
11
+ import sortAsc from "sort-asc";
12
+ import get from "get-value";
13
+ const sortFns = { desc: sortDesc, asc: sortAsc };
14
+ /**
15
+ * Sort the keys in an object.
16
+ *
17
+ * @param obj - The object to sort.
18
+ * @param options - Sort options, or an array of keys.
19
+ * @returns A new object with sorted keys.
20
+ */
21
+ export function sort(obj, options) {
22
+ if (obj == null || typeof obj !== "object" || Array.isArray(obj)) {
23
+ throw new TypeError(`sort-object expects a plain object, got ${obj === null ? "null" : typeof obj}`);
24
+ }
25
+ if (Array.isArray(options)) {
26
+ options = { keys: options };
27
+ }
28
+ const opts = options || {};
29
+ const prop = opts.prop;
30
+ const getFn = opts.get ||
31
+ function (val) {
32
+ if (prop)
33
+ return get(val, prop);
34
+ };
35
+ let fn = opts.sort || sortAsc;
36
+ if (opts.sortOrder) {
37
+ fn = sortFns[opts.sortOrder.toLowerCase()] || null;
38
+ }
39
+ let keys = opts.keys || [];
40
+ if (opts.sortBy) {
41
+ keys = opts.sortBy(obj);
42
+ fn = null;
43
+ }
44
+ if (opts.keys) {
45
+ if (!opts.sort && !opts.sortOrder && !opts.sortBy) {
46
+ fn = null;
47
+ }
48
+ }
49
+ const tmp = {};
50
+ const sortBy = {};
51
+ const build = keys.length === 0 ? fromObj : fromKeys;
52
+ build(obj, keys, tmp, sortBy, function (val) {
53
+ return getFn(val, prop);
54
+ });
55
+ if (fn) {
56
+ keys.sort(fn);
57
+ }
58
+ let len = keys.length;
59
+ let i = 0;
60
+ let j = 0;
61
+ const res = {};
62
+ let prev;
63
+ while (len--) {
64
+ const key = keys[i++];
65
+ if (prev !== key)
66
+ j = 0;
67
+ const k = get(sortBy, key)[j++];
68
+ res[k] = tmp[k];
69
+ prev = key;
70
+ }
71
+ // Deep sort nested objects if requested
72
+ if (opts.deep) {
73
+ for (const key of Object.keys(res)) {
74
+ if (isObject(res[key]) && !Array.isArray(res[key])) {
75
+ res[key] = sort(res[key], { deep: true });
76
+ }
77
+ }
78
+ }
79
+ return res;
80
+ }
81
+ /** Build sorting information from the object itself. */
82
+ function fromObj(obj, keys, tmp, sortBy, fn) {
83
+ for (const key in obj) {
84
+ if (!Object.prototype.hasOwnProperty.call(obj, key))
85
+ continue;
86
+ const val = obj[key];
87
+ let item = isObject(val) ? fn(val) || key : key;
88
+ item = isObject(item)
89
+ ? bytewise.encode(JSON.stringify(item)).toString()
90
+ : item;
91
+ union(sortBy, item, [key]);
92
+ keys.push(item);
93
+ tmp[key] = val;
94
+ }
95
+ }
96
+ /** Build sorting information from the supplied keys. */
97
+ function fromKeys(obj, keys, tmp, sortBy) {
98
+ let len = keys.length;
99
+ let i = 0;
100
+ while (len--) {
101
+ const key = keys[i++];
102
+ const val = obj[key];
103
+ union(sortBy, key, [key]);
104
+ tmp[key] = val;
105
+ }
106
+ }
107
+ export default sort;
108
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,QAAQ,MAAM,W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C,IAAc,CAAC,CAAC;QAC1B,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IACjB,CAAC;AACH,CAAC;AAED,wDAAwD;AACxD,SAAS,QAAQ,CACf,GAA4B,EAC5B,IAAc,EACd,GAA4B,EAC5B,MAAgC;IAEhC,IAAI,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC;IACtB,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,OAAO,GAAG,EAAE,EAAE,CAAC;QACb,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;QACtB,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;QACrB,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1B,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IACjB,CAAC;AACH,CAAC;AAED,eAAe,IAAI,CAAC"}
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@znemz/sort-object",
3
3
  "description": "Sort the keys in an object.",
4
- "version": "3.0.7",
5
- "homepage": "https://github.com/brickhouse/sort-object",
4
+ "version": "3.0.9",
5
+ "type": "module",
6
+ "homepage": "https://github.com/brickhouse-tech/sort-object",
6
7
  "originalAuthor": "Brian Woodward (https://github.com/doowb)",
7
8
  "maintainers": [
8
9
  "Nick McCready (https://github.com/nmccready)"
@@ -12,37 +13,53 @@
12
13
  "url": "git+https://github.com/brickhouse-tech/sort-object.git"
13
14
  },
14
15
  "bugs": {
15
- "url": "https://github.com/brickhouse/sort-object/issues"
16
+ "url": "https://github.com/brickhouse-tech/sort-object/issues"
16
17
  },
17
18
  "license": "MIT",
18
19
  "files": [
19
- "index.js"
20
+ "lib"
20
21
  ],
21
- "main": "index.js",
22
+ "main": "lib/index.js",
23
+ "types": "lib/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./lib/index.d.ts",
27
+ "import": "./lib/index.js",
28
+ "default": "./lib/index.js"
29
+ }
30
+ },
22
31
  "engines": {
23
- "node": ">=8.0.0"
32
+ "node": ">=20.0.0"
24
33
  },
25
34
  "scripts": {
26
- "test": "nyc --reporter=html --reporter=text mocha",
27
- "lint": "eslint *.js",
35
+ "build": "tsc",
36
+ "prebuild": "rm -rf lib",
37
+ "test": "vitest run",
38
+ "test:watch": "vitest",
39
+ "test:coverage": "vitest run --coverage",
40
+ "lint": "eslint src/**/*.ts",
41
+ "prepublishOnly": "npm run build",
28
42
  "better-audit": "better-npm-audit audit"
29
43
  },
30
44
  "dependencies": {
45
+ "@znemz/union-value": "^2.0.2",
31
46
  "bytewise": "^1.1.0",
32
47
  "get-value": "^2.0.2",
33
48
  "is-extendable": "^0.1.1",
34
49
  "sort-asc": "^0.2.0",
35
- "sort-desc": "^0.2.0",
36
- "@znemz/union-value": "^2.0.2"
50
+ "sort-desc": "^0.2.0"
37
51
  },
38
52
  "devDependencies": {
39
- "@commitlint/cli": "^20",
40
- "@commitlint/config-conventional": "^20",
41
- "better-npm-audit": "^3.3.0",
42
- "eslint": "9",
43
- "mocha": "11",
44
- "nyc": "^15.1.0",
45
- "should": "^13.1.3"
53
+ "@commitlint/cli": "^19",
54
+ "@commitlint/config-conventional": "^19",
55
+ "@typescript-eslint/eslint-plugin": "^8.55.0",
56
+ "@typescript-eslint/parser": "^8.55.0",
57
+ "better-npm-audit": "^3.11.0",
58
+ "eslint": "^9",
59
+ "globals": "^16.0.0",
60
+ "typescript": "^5.9.3",
61
+ "typescript-eslint": "^8.55.0",
62
+ "vitest": "^3"
46
63
  },
47
64
  "keywords": [
48
65
  "arr",
@@ -58,11 +75,7 @@
58
75
  "re-order",
59
76
  "sort",
60
77
  "util",
61
- "utils"
62
- ],
63
- "verb": {
64
- "related": {
65
- "list": []
66
- }
67
- }
78
+ "utils",
79
+ "typescript"
80
+ ]
68
81
  }
package/index.js DELETED
@@ -1,107 +0,0 @@
1
- /*!
2
- * sort-object <https://github.com/helpers/sort-object>
3
- *
4
- * Copyright (c) 2014-2015, Brian Woodward.
5
- * Licensed under the MIT License
6
- */
7
-
8
- "use strict";
9
-
10
- var isObject = require("is-extendable");
11
- var sortDesc = require("sort-desc");
12
- var bytewise = require("bytewise");
13
- var union = require("@znemz/union-value");
14
- var sortAsc = require("sort-asc");
15
- var get = require("get-value");
16
-
17
- var sortFns = { desc: sortDesc, asc: sortAsc };
18
-
19
- /**
20
- * Expose `sort`
21
- */
22
-
23
- module.exports = sort;
24
-
25
- function sort(obj, options) {
26
- if (Array.isArray(options)) {
27
- options = { keys: options };
28
- }
29
-
30
- var opts = options || {};
31
- var prop = opts.prop;
32
- var getFn =
33
- opts.get ||
34
- function (val) {
35
- if (prop) return get(val, prop);
36
- };
37
- var fn = opts.sort || sortAsc;
38
-
39
- if (Boolean(opts.sortOrder)) {
40
- fn = sortFns[opts.sortOrder.toLowerCase()];
41
- }
42
-
43
- var keys = opts.keys || [];
44
-
45
- if (Boolean(opts.sortBy)) {
46
- keys = opts.sortBy(obj);
47
- fn = null;
48
- }
49
-
50
- if (Boolean(opts.keys)) {
51
- if (!opts.sort && !opts.sortOrder && !opts.sortBy) {
52
- fn = null;
53
- }
54
- }
55
-
56
- var tmp = {};
57
- var sortBy = {};
58
-
59
- var build = keys.length === 0 ? fromObj : fromKeys;
60
- build(obj, keys, tmp, sortBy, function (val) {
61
- return getFn(val, prop);
62
- });
63
-
64
- if (fn) {
65
- keys.sort(fn);
66
- }
67
-
68
- var len = keys.length,
69
- i = 0,
70
- j = 0;
71
- var res = {},
72
- prev;
73
- while (len--) {
74
- var key = keys[i++];
75
- if (prev !== key) j = 0;
76
- var k = get(sortBy, key)[j++];
77
- res[k] = tmp[k];
78
- prev = key;
79
- }
80
- return res;
81
- }
82
-
83
- // build up the sorting information from the `obj`
84
- function fromObj(obj, keys, tmp, sortBy, fn) {
85
- for (var key in obj) {
86
- var val = obj[key];
87
- var item = isObject(val) ? fn(val) || key : key;
88
- item = isObject(item)
89
- ? bytewise.encode(JSON.stringify(item)).toString()
90
- : item;
91
- union(sortBy, item, [key]);
92
- keys.push(item);
93
- tmp[key] = val;
94
- }
95
- }
96
-
97
- // build up the sorting information from the supplied keys
98
- function fromKeys(obj, keys, tmp, sortBy) {
99
- var len = keys.length,
100
- i = 0;
101
- while (len--) {
102
- var key = keys[i++];
103
- var val = obj[key];
104
- union(sortBy, key, [key]);
105
- tmp[key] = val;
106
- }
107
- }