-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,305 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./content-type/_index.mjs"; | ||
export * from "./parse-quality-values.mjs"; | ||
export * from "./singleton.mjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/architectura/src/utility/parse-quality-values.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { splitOnce } from "@vitruvius-labs/toolbox"; | ||
|
||
/** | ||
* Parses a string of quality values. | ||
* | ||
* @remarks | ||
* - Order values by weight in descending order. | ||
* - Preserve order of values with the same weight. | ||
* - Default weight is 1. | ||
*/ | ||
function parseQualityValues(input: string): Array<string> | ||
{ | ||
interface QualityValueInterface | ||
{ | ||
value: string; | ||
weight: number; | ||
index: number; | ||
} | ||
|
||
if (input === "") | ||
{ | ||
return []; | ||
} | ||
|
||
return input | ||
.split(",") | ||
.map( | ||
(serialized_value: string, index: number): QualityValueInterface => | ||
{ | ||
const TRIMMED_VALUE: string = serialized_value.trim(); | ||
|
||
const [VALUE, WEIGHT]: [string, string] = splitOnce(";q=", TRIMMED_VALUE); | ||
|
||
return { | ||
value: VALUE, | ||
weight: WEIGHT === "" ? 1 : parseFloat(WEIGHT), | ||
index: index, | ||
}; | ||
} | ||
) | ||
.sort( | ||
(a: QualityValueInterface, z: QualityValueInterface): number => | ||
{ | ||
if (a.weight === z.weight) | ||
{ | ||
return a.index - z.index; | ||
} | ||
|
||
return z.weight - a.weight; | ||
} | ||
) | ||
.map( | ||
(quality_value: QualityValueInterface): string => | ||
{ | ||
return quality_value.value; | ||
} | ||
); | ||
} | ||
|
||
export { parseQualityValues }; |
Oops, something went wrong.