{"version":3,"file":"convert.mjs","names":[],"sources":["../../src/feature_filter/convert.ts"],"sourcesContent":["import {isExpressionFilter} from './index';\n\nimport type {\n    ExpressionFilterSpecification,\n    ExpressionInputType,\n    ExpressionSpecification,\n    FilterSpecification,\n    LegacyFilterSpecification\n} from '../types.g';\n\ntype ExpectedTypes = {[_: string]: ExpressionInputType};\n\n/*\n * Convert the given filter to an expression, storing the expected types for\n * any feature properties referenced in expectedTypes.\n *\n * These expected types are needed in order to construct preflight type checks\n * needed for handling 'any' filters. A preflight type check is necessary in\n * order to mimic legacy filters' semantics around expected type mismatches.\n * For example, consider the legacy filter:\n *\n *     [\"any\", [\"all\", [\">\", \"y\", 0], [\">\", \"y\", 0]], [\">\", \"x\", 0]]\n *\n * Naively, we might convert this to the expression:\n *\n *     [\"any\", [\"all\", [\">\", [\"get\", \"y\"], 0], [\">\", [\"get\", \"z\"], 0]], [\">\", [\"get\", \"x\"], 0]]\n *\n * But if we tried to evaluate this against, say `{x: 1, y: null, z: 0}`, the\n * [\">\", [\"get\", \"y\"], 0] would cause an evaluation error, leading to the\n * entire filter returning false. Legacy filter semantics, though, ask for\n * [\">\", \"y\", 0] to simply return `false` when `y` is of the wrong type,\n * allowing the subsequent terms of the outer \"any\" expression to be evaluated\n * (resulting, in this case, in a `true` value, because x > 0).\n *\n * We account for this by inserting a preflight type-checking expression before\n * each \"any\" term, allowing us to avoid evaluating the actual converted filter\n * if any type mismatches would cause it to produce an evaluation error:\n *\n *     [\"any\",\n *       [\"case\",\n *         [\"all\", [\"==\", [\"typeof\", [\"get\", \"y\"]], \"number\"], [\"==\", [\"typeof\", [\"get\", \"z\"], \"number]],\n *         [\"all\", [\">\", [\"get\", \"y\"], 0], [\">\", [\"get\", \"z\"], 0]],\n *         false\n *       ],\n *       [\"case\",\n *         [\"==\", [\"typeof\", [\"get\", \"x\"], \"number\"]],\n *         [\">\", [\"get\", \"x\"], 0],\n *         false\n *       ]\n *     ]\n *\n * An alternative, possibly more direct approach would be to use type checks\n * in the conversion of each comparison operator, so that the converted version\n * of each individual ==, >=, etc. would mimic the legacy filter semantics. The\n * downside of this approach is that it can lead to many more type checks than\n * would otherwise be necessary: outside the context of an \"any\" expression,\n * bailing out due to a runtime type error (expression semantics) and returning\n * false (legacy filter semantics) are equivalent: they cause the filter to\n * produce a `false` result.\n */\nexport function convertFilter(\n    filter: FilterSpecification,\n    expectedTypes: ExpectedTypes = {}\n): ExpressionFilterSpecification {\n    if (isExpressionFilter(filter)) return filter;\n    if (!filter) return true;\n\n    const legacyFilter = filter as LegacyFilterSpecification;\n    const legacyOp = legacyFilter[0];\n    if (filter.length <= 1) return legacyOp !== 'any';\n\n    switch (legacyOp) {\n        case '==':\n        case '!=':\n        case '<':\n        case '>':\n        case '<=':\n        case '>=': {\n            const [, property, value] = filter;\n            return convertComparisonOp(property as string, value, legacyOp, expectedTypes);\n        }\n        case 'any': {\n            const [, ...conditions] = legacyFilter;\n            const children = conditions.map((f: LegacyFilterSpecification) => {\n                const types = {};\n                const child = convertFilter(f, types);\n                const typechecks = runtimeTypeChecks(types);\n                return typechecks === true\n                    ? child\n                    : (['case', typechecks, child, false] as ExpressionSpecification);\n            });\n            return ['any', ...children];\n        }\n        case 'all': {\n            const [, ...conditions] = legacyFilter;\n            const children = conditions.map((f) => convertFilter(f, expectedTypes));\n            return children.length > 1 ? ['all', ...children] : children[0];\n        }\n        case 'none': {\n            const [, ...conditions] = legacyFilter;\n            return ['!', convertFilter(['any', ...conditions], {})];\n        }\n        case 'in': {\n            const [, property, ...values] = legacyFilter;\n            return convertInOp(property, values);\n        }\n        case '!in': {\n            const [, property, ...values] = legacyFilter;\n            return convertInOp(property, values, true);\n        }\n        case 'has':\n            return convertHasOp(legacyFilter[1]);\n        case '!has':\n            return ['!', convertHasOp(legacyFilter[1])];\n        default:\n            return true;\n    }\n}\n\n// Given a set of feature properties and an expected type for each one,\n// construct an boolean expression that tests whether each property has the\n// right type.\n// E.g.: for {name: 'string', population: 'number'}, return\n// [ 'all',\n//   ['==', ['typeof', ['get', 'name'], 'string']],\n//   ['==', ['typeof', ['get', 'population'], 'number]]\n// ]\nfunction runtimeTypeChecks(expectedTypes: ExpectedTypes): ExpressionFilterSpecification {\n    const conditions = [];\n    for (const property in expectedTypes) {\n        const get = property === '$id' ? ['id'] : ['get', property];\n        conditions.push(['==', ['typeof', get], expectedTypes[property]]);\n    }\n    if (conditions.length === 0) return true;\n    if (conditions.length === 1) return conditions[0];\n    return ['all', ...conditions];\n}\n\nfunction convertComparisonOp(\n    property: string,\n    value: any,\n    op: string,\n    expectedTypes?: ExpectedTypes | null\n): ExpressionFilterSpecification {\n    let get;\n    if (property === '$type') {\n        return [op, ['geometry-type'], value] as ExpressionFilterSpecification;\n    } else if (property === '$id') {\n        get = ['id'];\n    } else {\n        get = ['get', property];\n    }\n\n    if (expectedTypes && value !== null) {\n        const type = typeof value as any;\n        expectedTypes[property] = type;\n    }\n\n    if (op === '==' && property !== '$id' && value === null) {\n        return [\n            'all',\n            ['has', property], // missing property != null for legacy filters\n            ['==', get, null]\n        ];\n    } else if (op === '!=' && property !== '$id' && value === null) {\n        return [\n            'any',\n            ['!', ['has', property]], // missing property != null for legacy filters\n            ['!=', get, null]\n        ];\n    }\n\n    return [op, get, value] as ExpressionFilterSpecification;\n}\n\nfunction convertInOp(\n    property: string,\n    values: Array<any>,\n    negate = false\n): ExpressionFilterSpecification {\n    if (values.length === 0) return negate;\n\n    let get: ExpressionSpecification;\n    if (property === '$type') {\n        get = ['geometry-type'];\n    } else if (property === '$id') {\n        get = ['id'];\n    } else {\n        get = ['get', property];\n    }\n\n    // Determine if the list of values to be searched is homogenously typed.\n    // If so (and if the type is string or number), then we can use a\n    // [match, input, [...values], true, false] construction rather than a\n    // bunch of `==` tests.\n    let uniformTypes = true;\n    const type = typeof values[0];\n    for (const value of values) {\n        if (typeof value !== type) {\n            uniformTypes = false;\n            break;\n        }\n    }\n\n    if (uniformTypes && (type === 'string' || type === 'number')) {\n        // Match expressions must have unique values.\n        const uniqueValues = values.sort().filter((v, i) => i === 0 || values[i - 1] !== v);\n        return ['match', get, uniqueValues, !negate, negate];\n    }\n\n    if (negate) {\n        return ['all', ...values.map((v) => ['!=', get, v] as ExpressionSpecification)];\n    } else {\n        return ['any', ...values.map((v) => ['==', get, v] as ExpressionSpecification)];\n    }\n}\n\nfunction convertHasOp(property: string): ExpressionFilterSpecification {\n    if (property === '$type') {\n        return true;\n    } else if (property === '$id') {\n        return ['!=', ['id'], null];\n    } else {\n        return ['has', property];\n    }\n}\n"],"mappings":";;AA4DA,SAAgB,cACZ,QACA,gBAA+B,CAAC,GACH;CAC7B,IAAI,mBAAmB,MAAM,GAAG,OAAO;CACvC,IAAI,CAAC,QAAQ,OAAO;CAEpB,MAAM,eAAe;CACrB,MAAM,WAAW,aAAa;CAC9B,IAAI,OAAO,UAAU,GAAG,OAAO,aAAa;CAE5C,QAAQ,UAAR;EACI,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,MAAM;GACP,MAAM,GAAG,UAAU,SAAS;GAC5B,OAAO,oBAAoB,UAAoB,OAAO,UAAU,aAAa;EACjF;EACA,KAAK,OAAO;GACR,MAAM,GAAG,GAAG,cAAc;GAS1B,OAAO,CAAC,OAAO,GARE,WAAW,KAAK,MAAiC;IAC9D,MAAM,QAAQ,CAAC;IACf,MAAM,QAAQ,cAAc,GAAG,KAAK;IACpC,MAAM,aAAa,kBAAkB,KAAK;IAC1C,OAAO,eAAe,OAChB,QACC;KAAC;KAAQ;KAAY;KAAO;IAAK;GAC5C,CACyB,CAAC;EAC9B;EACA,KAAK,OAAO;GACR,MAAM,GAAG,GAAG,cAAc;GAC1B,MAAM,WAAW,WAAW,KAAK,MAAM,cAAc,GAAG,aAAa,CAAC;GACtE,OAAO,SAAS,SAAS,IAAI,CAAC,OAAO,GAAG,QAAQ,IAAI,SAAS;EACjE;EACA,KAAK,QAAQ;GACT,MAAM,GAAG,GAAG,cAAc;GAC1B,OAAO,C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