Skip to content

Commit 7825d33

Browse files
committed
chore: simplify weight normalization
1 parent 00045fe commit 7825d33

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

src/generators/web/utils/pages.mjs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ export const DEFAULT_PAGE_WEIGHT = -1;
66
/**
77
* Normalizes a page weight value to a finite number, or falls back to default.
88
*
9-
* @param {unknown} weight
9+
* @param {number|string} weight
1010
* @returns {number}
1111
*/
1212
export const normalizePageWeight = weight => {
13-
if (typeof weight === 'number' && Number.isFinite(weight)) {
14-
return weight;
13+
let candidate = NaN;
14+
15+
if (typeof weight === 'number') {
16+
candidate = weight;
1517
}
1618

1719
if (typeof weight === 'string') {
1820
const trimmedWeight = weight.trim();
1921

20-
if (trimmedWeight.length > 0) {
21-
const parsedWeight = Number(trimmedWeight);
22-
23-
if (Number.isFinite(parsedWeight)) {
24-
return parsedWeight;
25-
}
22+
if (trimmedWeight !== '') {
23+
candidate = Number(trimmedWeight);
2624
}
2725
}
2826

29-
return DEFAULT_PAGE_WEIGHT;
27+
return Number.isFinite(candidate) ? candidate : DEFAULT_PAGE_WEIGHT;
3028
};
3129

3230
/**
@@ -43,7 +41,7 @@ export const compareSidebarPageWeight = (a, b) => {
4341
const aHasWeight = a.weight !== DEFAULT_PAGE_WEIGHT;
4442
const bHasWeight = b.weight !== DEFAULT_PAGE_WEIGHT;
4543

46-
if (aHasWeight && bHasWeight && a.weight !== b.weight) {
44+
if (aHasWeight && bHasWeight) {
4745
return a.weight - b.weight;
4846
}
4947

0 commit comments

Comments
 (0)