From 600fcc10dba3229e5f69373f6e2b0896032ad4b9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 27 Jul 2023 20:49:06 -0700 Subject: [PATCH] [Refactor] `order`: use `object.groupby` --- package.json | 1 + src/rules/order.js | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e870082e3..c214e5955 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,7 @@ "is-glob": "^4.0.3", "minimatch": "^3.1.2", "object.fromentries": "^2.0.6", + "object.groupby": "^1.0.0", "object.values": "^1.1.6", "resolve": "^1.22.3", "semver": "^6.3.1", diff --git a/src/rules/order.js b/src/rules/order.js index 27c3f4b0f..6f70db263 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -2,6 +2,7 @@ import minimatch from 'minimatch'; import includes from 'array-includes'; +import groupBy from 'object.groupby'; import importType from '../core/importType'; import isStaticRequire from '../core/staticRequire'; @@ -325,13 +326,7 @@ function getSorter(alphabetizeOptions) { } function mutateRanksToAlphabetize(imported, alphabetizeOptions) { - const groupedByRanks = imported.reduce(function (acc, importedItem) { - if (!Array.isArray(acc[importedItem.rank])) { - acc[importedItem.rank] = []; - } - acc[importedItem.rank].push(importedItem); - return acc; - }, {}); + const groupedByRanks = groupBy(imported, (item) => item.rank); const sorterFn = getSorter(alphabetizeOptions);