Skip to content

Commit

Permalink
Add support for top-level arrays (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
jamestalmage and sindresorhus authored Oct 13, 2020
1 parent a3d55ab commit 6785f13
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ sortKeys({c: 0, a: 0, b: 0}, {
compare: (a, b) => -a.localeCompare(b)
});
//=> {c: 0, b: 0, a: 0}
sortKeys([{b: 0, a:2}], {deep: true});
//=> [{a: 2, b: 0}]
```
*/
declare function sortKeys<T extends {[key: string]: any}>(
object: T,
options?: sortKeys.Options
): T;

declare function sortKeys<T>(
object: Array<T>,
options?: sortKeys.Options
): Array<T>;

export = sortKeys;
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const isPlainObject = require('is-plain-obj');

module.exports = (object, options = {}) => {
if (!isPlainObject(object)) {
throw new TypeError('Expected a plain object');
if (!isPlainObject(object) && !Array.isArray(object)) {
throw new TypeError('Expected a plain object or array');
}

const {deep} = options;
Expand Down Expand Up @@ -63,5 +63,9 @@ module.exports = (object, options = {}) => {
return result;
};

if (Array.isArray(object)) {
return deep ? deepSortArray(object) : object.slice();
}

return sortKeys(object);
};
3 changes: 3 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ expectType<{a: 0; b: 0; c: 0}>(sortKeys({c: 0, a: 0, b: 0}));
expectType<{a: 0; b: {a: 0; b: 0}}>(
sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true})
);
expectType<Array<{a: 0; b: 0}>>(
sortKeys([{a: 0, b: 0}, {b: 0, a: 0}], {deep: true})
);
expectType<{c: 0; b: 0; a: 0}>(
sortKeys(
{c: 0, a: 0, b: 0},
Expand Down
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ sortKeys({c: 0, a: 0, b: 0}, {
compare: (a, b) => -a.localeCompare(b)
});
//=> {c: 0, b: 0, a: 0}

sortKeys([{b: 0, a:2}], {deep: true});
//=> [{a: 2, b: 0}]
```


Expand All @@ -41,7 +44,7 @@ Returns a new object with sorted keys.

#### object

Type: `object`
Type: `object | Array`

#### options

Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ test('deep arrays', t => {
t.deepEqual(Object.keys(sorted.a[0]), ['a', 'b']);
t.deepEqual(Object.keys(sorted.a[1][0]), ['a', 'b']);
});

test('top-level array', t => {
const array = [{b: 0, a: 0}, {c: 0, d: 0}];
const sorted = sortKeys(array);
t.not(sorted, array, 'should make a copy');
t.is(sorted[0], array[0]);
t.is(sorted[1], array[1]);

const deepSorted = sortKeys(array, {deep: true});
t.not(deepSorted, array);
t.not(deepSorted[0], array[0]);
t.not(deepSorted[1], array[1]);
t.deepEqual(Object.keys(deepSorted[0]), ['a', 'b']);
t.deepEqual(Object.keys(deepSorted[1]), ['c', 'd']);
});

0 comments on commit 6785f13

Please sign in to comment.