-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ádám Barancsuk <[email protected]> Co-authored-by: Ryan Hamley <[email protected]>
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// @flow | ||
|
||
import {array, ValueType, BooleanType} from '../types'; | ||
|
||
import type {Expression} from '../expression'; | ||
import type ParsingContext from '../parsing_context'; | ||
import type EvaluationContext from '../evaluation_context'; | ||
import type {Type} from '../types'; | ||
import type {Value} from '../values'; | ||
|
||
class In implements Expression { | ||
type: Type; | ||
needle: Expression; | ||
haystack: Expression; | ||
|
||
constructor(needle: Expression, haystack: Expression) { | ||
this.type = BooleanType; | ||
this.needle = needle; | ||
this.haystack = haystack; | ||
} | ||
|
||
static parse(args: $ReadOnlyArray<mixed>, context: ParsingContext) { | ||
if (args.length !== 3) | ||
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`); | ||
|
||
const needle = context.parse(args[1], 1, ValueType); | ||
const haystack = context.parse(args[2], 2, array(ValueType)); | ||
|
||
if (!needle || !haystack) return null; | ||
|
||
return new In(needle, haystack); | ||
} | ||
|
||
evaluate(ctx: EvaluationContext) { | ||
const needle = (this.needle.evaluate(ctx): any); | ||
const haystack = ((this.haystack.evaluate(ctx): any): Array<Value>); | ||
|
||
return haystack.indexOf(needle) >= 0; | ||
} | ||
|
||
eachChild(fn: (Expression) => void) { | ||
fn(this.needle); | ||
fn(this.haystack); | ||
} | ||
|
||
possibleOutputs() { | ||
return [true, false]; | ||
} | ||
|
||
serialize() { | ||
return ["in", this.needle.serialize(), this.haystack.serialize()]; | ||
} | ||
} | ||
|
||
export default In; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"expression": [ | ||
"boolean", | ||
["in", ["get", "i"], ["array", ["get", "arr"]]] | ||
], | ||
"inputs": [ | ||
[{}, {"properties": {"i": null, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": 1, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": 9, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": 1, "arr": null}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "boolean" | ||
}, | ||
"outputs": [ | ||
false, | ||
false, | ||
true, | ||
{"error":"Expected value to be of type array, but found null instead."} | ||
], | ||
"serialized": [ | ||
"boolean", | ||
["in", ["get", "i"], ["array", ["get", "arr"]]] | ||
] | ||
} | ||
} |