Skip to content

Commit bbf14f7

Browse files
committed
refactor: use toSorted() instead of [...arr].sort()
Replace copy-before-sort pattern with ES2023 toSorted() in 12 files
1 parent fed4c7d commit bbf14f7

File tree

12 files changed

+13
-16
lines changed

12 files changed

+13
-16
lines changed

src/components/cc-addon-features/cc-addon-features.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,11 @@ export class CcAddonFeatures extends LitElement {
102102
* @private
103103
*/
104104
_sortFeatures(features) {
105-
const sortedArray = features.slice(0);
106-
sortedArray.sort((a, b) => {
105+
return features.toSorted((a, b) => {
107106
const aIndex = SORT_FEATURES.indexOf(a.name.toLowerCase()) + 1 || SORT_FEATURES.length + 1;
108107
const bIndex = SORT_FEATURES.indexOf(b.name.toLowerCase()) + 1 || SORT_FEATURES.length + 1;
109108
return String(aIndex).localeCompare(String(bIndex), undefined, { numeric: true });
110109
});
111-
return sortedArray;
112110
}
113111

114112
render() {

src/components/cc-domain-management/cc-domain-management.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ export class CcDomainManagement extends LitElement {
349349
}
350350

351351
if (changedProperties.has('domainListState') && this.domainListState.type === 'loaded') {
352-
// Make sure we use a copy before using .sort() which modifies the array in place
353-
this._sortedDomains = [...this.domainListState.domains].sort(sortDomains).map((domainState) => {
352+
this._sortedDomains = this.domainListState.domains.toSorted(sortDomains).map((domainState) => {
354353
const { hostname } = domainState;
355354

356355
/** @type {FormattedDomainInfo} */

src/components/cc-email-list/cc-email-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class CcEmailList extends LitElement {
245245
* @return {TemplateResult}
246246
*/
247247
_renderSecondarySection(secondaryAddressStates) {
248-
const addresses = [...secondaryAddressStates].sort(sortBy('address'));
248+
const addresses = secondaryAddressStates.toSorted(sortBy('address'));
249249
const isOneRowMarkingPrimary = addresses.some((item) => item.type === 'marking-as-primary');
250250

251251
return html`

src/components/cc-env-var-form/cc-env-var-form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export class CcEnvVarForm extends LitElement {
223223
this._currentVariables = null;
224224
this._editorsState = { type: 'loaded', validationMode: this.state.validationMode, variables: [] };
225225
} else {
226-
const sortedVariables = [...variables].sort((a, b) => a.name.localeCompare(b.name));
226+
const sortedVariables = variables.toSorted((a, b) => a.name.localeCompare(b.name));
227227
this._currentVariables = sortedVariables;
228228
this._editorsState = { type: 'loaded', validationMode: this.state.validationMode, variables: sortedVariables };
229229
}

src/components/cc-pricing-product/cc-pricing-product.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export class CcPricingProduct extends LitElement {
249249
*/
250250
_renderProductPlans(productName, productPlans, productFeatures) {
251251
// this component is not rerendering very often so we consider we can afford to sort plans and filter the features here.
252-
const sortedPlans = [...productPlans].sort((a, b) => a.price - b.price);
252+
const sortedPlans = productPlans.toSorted((a, b) => a.price - b.price);
253253
const filteredProductFeatures = productFeatures.filter(
254254
(feature) => AVAILABLE_FEATURES.includes(feature.code) || feature.name != null,
255255
);

src/components/cc-ssh-key-list/cc-ssh-key-list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ export class CcSshKeyList extends LitElement {
261261

262262
/**
263263
* @param {"personal"|"github"|"skeleton"} type
264-
* @param {SshKeyState[]|GithubSshKeyState[]} keys
264+
* @param {Array<SshKeyState|GithubSshKeyState>} keys
265265
* @return {TemplateResult}
266266
*/
267267
_renderKeyList(type, keys) {
268-
const sortedKeys = [...keys].sort(sortBy('name'));
268+
const sortedKeys = keys.toSorted(sortBy('name'));
269269
const skeleton = type === 'skeleton';
270270
return html`
271271
<div class="key-list">

src/components/cc-token-api-list/cc-token-api-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class CcTokenApiList extends LitElement {
117117

118118
const isEmpty = this.state.type === 'loaded' && this.state.apiTokens.length === 0;
119119

120-
const sortedTokens = this.state.type === 'loaded' ? [...this.state.apiTokens].sort(this._sortTokens) : [];
120+
const sortedTokens = this.state.type === 'loaded' ? this.state.apiTokens.toSorted(this._sortTokens) : [];
121121
const isOneTokenRevoking = sortedTokens.some((token) => token.type === 'revoking');
122122

123123
const isResettingPassword = this.state.type === 'resetting-password';

src/components/cc-token-oauth-list/cc-token-oauth-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class CcTokenOauthList extends LitElement {
8484
this.state.type === 'revoking-all' || (!isEmpty && this.state.type !== 'loading');
8585
const sortedOauthTokens =
8686
this.state.type === 'loaded' || this.state.type === 'revoking-all'
87-
? [...this.state.oauthTokens].sort(
87+
? this.state.oauthTokens.toSorted(
8888
(tokenA, tokenB) => tokenB.creationDate.getTime() - tokenA.creationDate.getTime(),
8989
)
9090
: [];

src/components/cc-visual-tests-report-menu/cc-visual-tests-report-menu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class CcVisualTestsReportMenu extends LitElement {
6767
* @returns {VisualTestResult[]}
6868
*/
6969
_sortTestResults(results) {
70-
return [...results].sort((a, b) => {
70+
return results.toSorted((a, b) => {
7171
const componentCompare = a.componentTagName.localeCompare(b.componentTagName);
7272
if (componentCompare !== 0) {
7373
return componentCompare;

src/components/cc-visual-tests-report/cc-visual-tests-report.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class CcVisualTestsReport extends LitElement {
6565
* @returns {VisualTestResult[]}
6666
*/
6767
_sortTestResults(testResults) {
68-
return [...testResults].sort((a, b) => {
68+
return testResults.toSorted((a, b) => {
6969
const componentCompare = a.componentTagName.localeCompare(b.componentTagName);
7070
if (componentCompare !== 0) {
7171
return componentCompare;

0 commit comments

Comments
 (0)