@guanriyue/decurl/decode
@guanriyue/decurl/decode provides optional decode primitives. They compose common string parsing logic, but they are not required; complex structures can use a custom decode function directly.
Failure Semantics
Decode<TInput, TOutput> is a synchronous function. Returning null or undefined means parsing failed, and pipe stops later steps.
Composition
Composition helpers organize a decode pipeline.
mapItems
mapItems(...steps) runs pipe-like decode logic for every array item. Items that fail to parse are filtered out.
pipe
pipe(...steps) runs multiple decode steps in order. The input type comes from the first step, and the output type comes from the last step.
When a step returns null or undefined, pipe stops later steps and returns undefined.
Normalize
Normalize APIs arrange raw strings into a better shape for later parsing.
trim
trim(input) removes whitespace from both ends of a string.
trim.end
trim.end(input) removes trailing whitespace.
trim.start
trim.start(input) removes leading whitespace.
Shape / Guard
Shape and Guard APIs only check whether a value satisfies a requirement. They do not convert the value. If the requirement is not met, they return undefined.
elementOf
elementOf(values) requires the value to be included in the given array. It uses Object.is for membership checks and does not perform type conversion.
elementOf(enumDefinition)
elementOf(enumDefinition) requires the value to be one of the enum definition values. For numeric enums, usually convert to number first.
shape
shape(regexp) requires the string to match the given regular expression. The output is still the original string.
shape.boolish
shape.boolish requires the string to be 'true' or 'false', and narrows the type to 'true' | 'false'.
shape.date
shape.date requires the string to be YYYY-MM-DD and checks that the date is real.
shape.datetime
shape.datetime requires the string to match a common date-time shape and checks the date and time ranges.
shape.integer
shape.integer requires the string to match a decimal integer shape. It does not convert the string to number.
shape.month
shape.month requires the string to be YYYY-MM and checks the month range.
shape.number
shape.number requires the string to match a decimal number shape. It does not convert the string to number.
startsWith
startsWith(prefix) requires the string to start with the prefix and narrows the type to a template literal type.
where
where(predicate) filters a value with a predicate or type guard. When the predicate returns false, the decode result is undefined.
Convert
Convert APIs transform a value into a new type.
toBoolean
toBoolean(input) converts 'true' / 'false' into boolean. Other inputs return undefined.
toNumber
toNumber(input) converts with Number(input) and requires the result to be a finite number.
Constraint
Constraint APIs continue narrowing the valid range. They only check constraints and do not change the input value.
length
length(size) requires the input length to equal the given value.
length([min, max]) requires the input length to be in the closed interval.
length.max
length.max(value) requires the input length to be less than or equal to the given value.
length.min
length.min(value) requires the input length to be greater than or equal to the given value.
max
max(value) requires a number to be less than or equal to the given value. It does not change the input; if the constraint is not met, it returns undefined.
min
min(value) requires a number to be greater than or equal to the given value. It does not change the input; if the constraint is not met, it returns undefined.
Array
Array APIs handle arrays as a whole.
unique
unique(input) removes duplicates with Object.is and preserves the first occurrence order.
unique.by
unique.by(identity) removes duplicates by the identity result.