UNPKG

tile-stencil

Version:

Load a MapLibre style document and parse it into Javascript functions

50 lines (37 loc) 1.34 kB
import { convertIfColor, buildInterpolator } from "./interpolate.js"; export function autoGetters(properties = {}, defaults) { return Object.entries(defaults).reduce((d, [key, val]) => { d[key] = buildStyleFunc(properties[key], val); return d; }, {}); } function buildStyleFunc(style, defaultVal) { if (style === undefined) { return getConstFunc(defaultVal); } else if (typeof style !== "object" || Array.isArray(style)) { return getConstFunc(style); } else { return getStyleFunc(style); } // NOT IMPLEMENTED: zoom-and-property functions } function getConstFunc(rawVal) { const val = convertIfColor(rawVal); const func = () => val; return Object.assign(func, { type: "constant" }); } function getStyleFunc(style) { const { type, property = "zoom", base = 1, stops } = style; const getArg = (property === "zoom") ? (zoom) => zoom : (zoom, feature) => feature.properties[property]; const getVal = (type === "identity") ? convertIfColor : buildInterpolator(stops, base); if (!getVal) return console.log("style: " + JSON.stringify(style) + "\nERROR in tile-stencil: unsupported style!"); const styleFunc = (zoom, feature) => getVal(getArg(zoom, feature)); return Object.assign(styleFunc, { type: (property === "zoom") ? "zoom" : "property", property, }); }