From 0606961c858c6148b8f00c13f0ac634167d0c721 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 31 Dec 2019 03:49:48 +0800 Subject: [PATCH] fix: sort `bundledDependencies` as array (#112) --- index.js | 5 +++-- test.js | 32 +++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 234746e3..e6f0f78b 100755 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const globby = require('globby') const onArray = fn => x => (Array.isArray(x) ? fn(x) : x) const uniq = onArray(xs => xs.filter((x, i) => i === xs.indexOf(x))) +const sortArray = onArray(array => [...array].sort()) const isPlainObject = x => x && Object.prototype.toString.call(x) === '[object Object]' const onObject = fn => x => (isPlainObject(x) ? fn(x) : x) @@ -130,8 +131,8 @@ const fields = [ { key: 'dependencies', over: sortObject }, { key: 'devDependencies', over: sortObject }, { key: 'peerDependencies', over: sortObject }, - { key: 'bundledDependencies', over: sortObject }, - { key: 'bundleDependencies', over: sortObject }, + { key: 'bundledDependencies', over: sortArray }, + { key: 'bundleDependencies', over: sortArray }, { key: 'optionalDependencies', over: sortObject }, { key: 'flat' }, { key: 'resolutions', over: sortObject }, diff --git a/test.js b/test.js index a5fcb22a..5471d581 100644 --- a/test.js +++ b/test.js @@ -456,17 +456,39 @@ for (const field of [ 'dependencies', 'devDependencies', 'peerDependencies', - 'bundledDependencies', - 'bundleDependencies', 'optionalDependencies', ]) { testField(field, [ { value: { - 'sort-object-keys': '^1.1.2', - glob: '^7.1.6', + z: '2.0.0', + a: '1.0.0', }, - expect: ['glob', 'sort-object-keys'], + expect: ['a', 'z'], + }, + { + value: ['z', 'a'], + expect: ['z', 'a'], + message: `Should not sort array type of ${field} field.`, + }, + ]) +} + +// bundledDependencies +for (const field of ['bundledDependencies', 'bundleDependencies']) { + testField(field, [ + { + value: ['z', 'a'], + expect: ['a', 'z'], + }, + // should ignore object + { + value: { + z: '2.0.0', + a: '1.0.0', + }, + expect: ['z', 'a'], + message: `Should not sort object type of ${field} field.`, }, ]) }