Skip to content

Commit 777a1b2

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

File tree

16 files changed

+17
-20
lines changed

16 files changed

+17
-20
lines changed

eslint/i18n/custom-rules/i18n-order.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default {
3939
});
4040

4141
const translationKeys = Object.keys(keysToNode);
42-
const sortedTranslationKeys = [...translationKeys].sort((a, b) => {
42+
const sortedTranslationKeys = translationKeys.toSorted((a, b) => {
4343
const aPrefix = getPrefix(a);
4444
const bPrefix = getPrefix(b);
4545
// Make sure we sort by prefix before sorting by the rest of the key

prettier-rules/sort-lit-get-properties.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function parse(text, options) {
7070
* @returns {Array<ObjectPropertiesType>}
7171
*/
7272
function sortProperties(props) {
73-
return [...props].sort((pA, pB) => {
73+
return props.toSorted((pA, pB) => {
7474
// These conditions will always be checked on `...super.properties`, so there's no need to check value
7575
if (t.isSpreadElement(pA)) {
7676
return -1;

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-logs-app-runtime/cc-logs-app-runtime.smart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class DeploymentsManager {
783783
return d2.creationDate.getTime() - d1.creationDate.getTime();
784784
};
785785

786-
const deployments = Array.from(this._deploymentsMap.values()).sort(sortFn);
786+
const deployments = Array.from(this._deploymentsMap.values()).toSorted(sortFn);
787787
return deployments[0];
788788
}
789789

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';

0 commit comments

Comments
 (0)