MCPcopy Create free account
hub / github.com/arktypeio/arktype / normalizeBoundUnit

Function normalizeBoundUnit

ark/schema/refinements/unit.ts:34–59  ·  view source on GitHub ↗
(unit: BoundUnitInput)

Source from the content-addressed store, hash-verified

32 `Bound unit must be a non-empty string or { singular, plural } object (was ${printable(unit)})`
33
34export const normalizeBoundUnit = (unit: BoundUnitInput): BoundUnit => {
35 if (unit === boundUnits.character || unit === boundUnits.item) return unit
36 if (typeof unit === "string") {
37 if (!unit) throwParseError(writeInvalidBoundUnitMessage(unit))
38 return (
39 unit === "character" ? boundUnits.character
40 : unit === "item" ? boundUnits.item
41 : Object.freeze({ singular: unit, plural: `${unit}s` })
42 )
43 }
44 if (
45 typeof unit !== "object" ||
46 unit === null ||
47 typeof unit.singular !== "string" ||
48 !unit.singular ||
49 typeof unit.plural !== "string" ||
50 !unit.plural
51 )
52 throwParseError(writeInvalidBoundUnitMessage(unit))
53 return (
54 unit.singular === "character" && unit.plural === "characters" ?
55 boundUnits.character
56 : unit.singular === "item" && unit.plural === "items" ? boundUnits.item
57 : Object.freeze({ singular: unit.singular, plural: unit.plural })
58 )
59}
60
61export const sameBoundUnit = (
62 l: BoundUnit | undefined,

Callers 2

constraintInputOfFunction · 0.90
parseNodeFunction · 0.90

Calls 2

throwParseErrorFunction · 0.90

Tested by

no test coverage detected