Skip to content

Commit

Permalink
refactor: sortVariationsByLabel logic when not numbers and redability
Browse files Browse the repository at this point in the history
  • Loading branch information
iago1501 committed Jan 22, 2025
1 parent 9fe3463 commit 88fd354
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- Fully refactor of `sortVariationsByLabel` property

## [3.176.1] - 2024-11-08

## [3.176.0] - 2024-11-06
Expand Down
32 changes: 23 additions & 9 deletions react/components/SKUSelector/components/SKUSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function getShowValueForVariation(
)
}


Check failure on line 49 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`
interface Props {
seeMoreLabel: string
maxItems: number
Expand Down Expand Up @@ -84,6 +85,18 @@ function isSkuAvailable(item?: SelectorProductItem) {
return seller.commertialOffer?.AvailableQuantity > 0
}

function sortOptionsNumerically(options: DisplayOption[]) {
return options.slice().sort((a, b) => parseFloat(a.label) - parseFloat(b.label));

Check failure on line 89 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Replace `.slice().sort((a,·b)·=>·parseFloat(a.label)·-·parseFloat(b.label));` with `⏎····.slice()⏎····.sort((a,·b)·=>·parseFloat(a.label)·-·parseFloat(b.label))`
}

function sortOptionsAlphabetically(options: DisplayOption[]) {
return options.slice().sort((a, b) => {
if (a.label < b.label) return -1;

Check failure on line 94 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `;`
if (a.label > b.label) return 1;

Check failure on line 95 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `;`
return 0;

Check warning on line 96 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Expected blank line before this statement

Check failure on line 96 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `;`
});

Check failure on line 97 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `;`
}

const showItemAsAvailable = ({
possibleItems,
selectedVariations,
Expand Down Expand Up @@ -265,7 +278,7 @@ const variationNameToDisplayVariation =
(variationName: string): DisplayVariation => {
const name = variationName
const { values, originalName } = variations[variationName]
const options = values
let options = values
.map(
parseOptionNameToDisplayOption({
selectedVariations,
Expand All @@ -279,19 +292,20 @@ const variationNameToDisplayVariation =
})
)
.filter(Boolean) as DisplayOption[]

Check failure on line 295 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `······⏎`

if (sortVariationsByLabel) {

Check failure on line 297 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`
const allNumbers = options.every(
(option: any) => !Number.isNaN(option.label)

const areAllNumbers = options.every(
(option: DisplayOption) => !isNaN(Number(option.label))

Check failure on line 300 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected use of 'isNaN'
)

options.sort((a: any, b: any) => {
if (allNumbers) {
return a.label - b.label
}
const sortedOptions = areAllNumbers
? sortOptionsNumerically({...options})

Check failure on line 304 in react/components/SKUSelector/components/SKUSelector.tsx

View workflow job for this annotation

GitHub Actions / Lint

Replace `?·sortOptionsNumerically({...options` with `··?·sortOptionsNumerically({·...options·`
: sortOptionsAlphabetically({...options});

options = {...sortedOptions}

return a.label < b.label ? -1 : a.label > b.label ? 1 : 0
})
}

return { name, originalName, options }
Expand Down

0 comments on commit 88fd354

Please sign in to comment.