Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Apr 29, 2019
1 parent 39a81a8 commit c5c02fe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -38,7 +38,7 @@ Returns a new object with sorted keys.

#### input

Type: `Object`
Type: `object`

#### options

Expand All @@ -50,7 +50,7 @@ Recursively sort keys.

##### compare

Type: `Function`
Type: `function`

[Compare function.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)

Expand Down
28 changes: 14 additions & 14 deletions test.js
@@ -1,27 +1,27 @@
import test from 'ava';
import m from '.';
import sortKeys from '.';

test('sort the keys of an object', t => {
t.deepEqual(m({c: 0, a: 0, b: 0}), {a: 0, b: 0, c: 0});
t.deepEqual(sortKeys({c: 0, a: 0, b: 0}), {a: 0, b: 0, c: 0});
});

test('custom compare function', t => {
const compare = (a, b) => a.localeCompare(b);
t.deepEqual(m({c: 0, a: 0, b: 0}, {compare}), {c: 0, b: 0, a: 0});
t.deepEqual(sortKeys({c: 0, a: 0, b: 0}, {compare}), {c: 0, b: 0, a: 0});
});

test('deep option', t => {
t.deepEqual(m({c: {c: 0, a: 0, b: 0}, a: 0, b: 0}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}});
t.deepEqual(sortKeys({c: {c: 0, a: 0, b: 0}, a: 0, b: 0}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}});

t.notThrows(() => {
const obj = {a: 0};
obj.circular = obj;
m(obj, {deep: true});
sortKeys(obj, {deep: true});
});

const obj = {z: 0};
obj.circular = obj;
const sortedObj = m(obj, {deep: true});
const sortedObj = sortKeys(obj, {deep: true});

t.is(sortedObj, sortedObj.circular);
t.deepEqual(Object.keys(sortedObj), ['circular', 'z']);
Expand All @@ -37,19 +37,19 @@ test('deep option', t => {
obj4.a[0].c = obj3.a[0];

t.notThrows(() => {
m(obj1, {deep: true});
m(obj2, {deep: true});
m(obj3, {deep: true});
m(obj4, {deep: true});
sortKeys(obj1, {deep: true});
sortKeys(obj2, {deep: true});
sortKeys(obj3, {deep: true});
sortKeys(obj4, {deep: true});
});

const sorted = m(obj1, {deep: true});
const deepSorted = m(obj3, {deep: true});
const sorted = sortKeys(obj1, {deep: true});
const deepSorted = sortKeys(obj3, {deep: true});

t.is(sorted, sorted.a.c);
t.deepEqual(deepSorted.a[0], deepSorted.a[0].a.c);
t.deepEqual(Object.keys(sorted), ['a', 'b']);
t.deepEqual(Object.keys(deepSorted.a[0]), ['a', 'b']);
t.deepEqual(m({c: {c: 0, a: 0, b: 0}, a: 0, b: 0, z: [9, 8, 7, 6, 5]}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}, z: [9, 8, 7, 6, 5]});
t.deepEqual(Object.keys(m({a: [{b: 0, a: 0}]}, {deep: true}).a[0]), ['a', 'b']);
t.deepEqual(sortKeys({c: {c: 0, a: 0, b: 0}, a: 0, b: 0, z: [9, 8, 7, 6, 5]}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}, z: [9, 8, 7, 6, 5]});
t.deepEqual(Object.keys(sortKeys({a: [{b: 0, a: 0}]}, {deep: true}).a[0]), ['a', 'b']);
});

0 comments on commit c5c02fe

Please sign in to comment.