@webfeet/microsyntaxes
v0.1.0
Published
Functions implementing the HTML common microsyntaxes
Readme
@webfeet/microsyntaxes
Implements the HTML common microsyntaxes.
TODO
Currently, only enumerated attributes and numbers are implemented, no dates and times, colors, space- or comma-separated tokens, or references.
API
All parsing functions take a single string or null argument, and return that value parsed according to the specific rules.
enumerated(options)(value)
This function is a factory returning a parser function implementing the rules for determining the state of an enumerated attribute.
The factory functions takes an object as argument with options as properties:
keywords: mandatory; the list of canonical keywords (those values that will be returned by the parser function)aliases: optional; an object associating aliases to their canonical keyword equivalentmissing: optional; the missing value default (must be a canonical keyword), returned when the parser function is called withnullinvalid: optional; the invalid value default (must be a canonical keyword), returned when the parser function is called with an unknown value
The parser function (returned by the factory function) takes a single string or null argument and returns one of the canonical keywords (or null if missing or invalid are null).
For example, the shape attribute of <area> elements could be implemented as:
const parseShape = enumerated({
keywords: ["circle", "default", "poly", "rect"],
aliases: {
circ: "circle",
polygon: "poly",
rectangle: "rect",
},
missing: "rect",
invalid: "rect",
});
parseShape(null); // → "rect"
parseShape("circle"); // → "circ"parseInteger(value)
This function implements the rules for parsing integers. It takes a single string or null argument and returns a number value, or null if the argument cannot be parsed.
parseNonNegativeInteger(value)
This function implements the rules for parsing non-negative integers. It takes a single string or null argument and returns a number value, or null if the argument cannot be parsed.
parseDouble(value)
This function implements the rules for parsing floating-point number values. It takes a single string or null argument and returns a number value, or null if the argument cannot be parsed.
