Skip to main content

Module filter

Module filter 

Source
Expand description

Converting legacy MapLibre filters into modern expressions.

Before expressions existed, layer filters were written as nested arrays with a bare property name in the operand slot — ["==", "class", "primary"], ["in", "type", "a", "b"], ["all", …]. MapLibre still accepts them, converting each to the equivalent boolean expression (["==", ["get", "class"], "primary"], …) before compiling. This module is a port of maplibre-style-spec’s src/feature_filter/convert.ts (the conversion body) and the isExpressionFilter discriminator from src/feature_filter/index.ts, so the produced expressions match the reference implementation.

Two entry points:

  • is_expression_filter — is this filter already a modern expression (so it needs no conversion), or a legacy filter?
  • convert_legacy_filter — convert a legacy filter to the equivalent modern expression, returned as raw JSON (ready for parse or to be embedded verbatim in a style). An input that already is an expression is returned unchanged.

§Legacy comparison semantics

Legacy comparisons are strictly typed with no implicit conversion: when a property’s runtime type differs from the compared value’s type, the filter simply yields false. The modern ==/</… operators instead type-check (and may error or coerce), so a naive ["==", ["get", k], v] would not reproduce legacy behavior inside an any. Following convert.ts, each any term is guarded with a preflight typeof check (see convert_legacy_filter for the worked example) so a type mismatch short-circuits to false instead of erroring out the whole filter.

Enums§

FilterError
A legacy filter that could not be converted to an expression.

Functions§

convert_legacy_filter
Convert a legacy MapLibre filter to the equivalent modern expression.
is_expression_filter
Whether filter is already a modern expression filter (as opposed to a legacy filter needing conversion). A direct port of isExpressionFilter.