Skip to content

Commit

Permalink
Fix sorting differences with npm deps (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinzara authored Oct 2, 2023
1 parent 548cad6 commit 61ef435
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/sort-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import sortObject from 'sort-object-keys';
import { PackageJson } from './types';

function order(a: string, b: string) {
return a.localeCompare(b, 'en');
}

export default function sortDependencies<
TKey extends
| 'bundledDependencies'
Expand All @@ -16,5 +12,5 @@ export default function sortDependencies<
>(key: TKey, packageJson: PackageJson) {
const dependencies = packageJson[key];
const keys = Object.keys(dependencies || {}) as Array<keyof typeof dependencies & string>;
return keys.length === 0 ? {} : { [key]: sortObject(dependencies, keys.sort(order)) };
return keys.length === 0 ? {} : { [key]: sortObject(dependencies, keys.sort()) };
}
13 changes: 13 additions & 0 deletions tests/__snapshots__/sort-dependencies.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`It orders dependencies in alphabetical order 1`] = `
"{
\\"dependencies\\": {
\\"a\\": \\"1\\",
\\"foo-bar\\": \\"2\\",
\\"foo_bar\\": \\"3\\",
\\"g\\": \\"4\\"
}
}
"
`;
14 changes: 14 additions & 0 deletions tests/sort-dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { format } from '../src';

test('It orders dependencies in alphabetical order', () => {
const json = {
dependencies: {
foo_bar: "3",
g: "4",
a: "1",
"foo-bar": "2", // the "-" should be before the "_" to match `npm` sorting
}
};

expect(format(json)).toMatchSnapshot();
});

0 comments on commit 61ef435

Please sign in to comment.