-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
552 additions
and
176 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
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
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,102 @@ | ||
/// Evaluation rules used in expressions like `$[?(@.foo > 2)]`. | ||
/// Allows users to implement custom evaluation rules to emulate behavior | ||
/// in other programming languages, like JavaScript. | ||
abstract class Algebra { | ||
/// A set of rules with strictly typed operations. | ||
/// Throws [TypeError] when the operation is not applicable to operand types. | ||
static const strict = _Strict(); | ||
|
||
/// A relaxed set of rules allowing some operations on not fully compatible types. | ||
/// E.g. `1 < "3"` would return false instead of throwing a [TypeError]. | ||
static const relaxed = _Relaxed(); | ||
|
||
/// True if [a] equals [b]. | ||
bool eq(dynamic a, dynamic b); | ||
|
||
/// True if [a] is not equal to [b]. | ||
bool ne(dynamic a, dynamic b); | ||
|
||
/// True if [a] is strictly less than [b]. | ||
bool lt(dynamic a, dynamic b); | ||
|
||
/// True if [a] is less or equal to [b]. | ||
bool le(dynamic a, dynamic b); | ||
|
||
/// True if [a] is strictly greater than [b]. | ||
bool gt(dynamic a, dynamic b); | ||
|
||
/// True if [a] is greater or equal to [b]. | ||
bool ge(dynamic a, dynamic b); | ||
|
||
/// True if both [a] and [b] are truthy. | ||
bool and(dynamic a, dynamic b); | ||
|
||
/// True if either [a] or [b] are truthy. | ||
bool or(dynamic a, dynamic b); | ||
|
||
/// Casts the [val] to bool. | ||
bool isTruthy(dynamic val); | ||
} | ||
|
||
class _Strict implements Algebra { | ||
const _Strict(); | ||
|
||
@override | ||
bool isTruthy(dynamic val) => val; | ||
|
||
@override | ||
bool eq(a, b) => a == b; | ||
|
||
@override | ||
bool ge(a, b) => a >= b; | ||
|
||
@override | ||
bool gt(a, b) => a > b; | ||
|
||
@override | ||
bool le(a, b) => a <= b; | ||
|
||
@override | ||
bool lt(a, b) => a < b; | ||
|
||
@override | ||
bool ne(a, b) => a != b; | ||
|
||
@override | ||
bool and(dynamic a, dynamic b) => isTruthy(a) && isTruthy(b); | ||
|
||
@override | ||
bool or(dynamic a, dynamic b) => isTruthy(a) || isTruthy(b); | ||
} | ||
|
||
class _Relaxed extends _Strict { | ||
const _Relaxed(); | ||
|
||
@override | ||
bool isTruthy(dynamic val) => | ||
val == true || | ||
val is List || | ||
val is Map || | ||
(val is num && val != 0) || | ||
(val is String && val.isNotEmpty); | ||
|
||
@override | ||
bool ge(a, b) => | ||
(a is String && b is String && a.compareTo(b) >= 0) || | ||
(a is num && b is num && a >= b); | ||
|
||
@override | ||
bool gt(a, b) => | ||
(a is String && b is String && a.compareTo(b) > 0) || | ||
(a is num && b is num && a > b); | ||
|
||
@override | ||
bool le(a, b) => | ||
(a is String && b is String && a.compareTo(b) <= 0) || | ||
(a is num && b is num && a <= b); | ||
|
||
@override | ||
bool lt(a, b) => | ||
(a is String && b is String && a.compareTo(b) < 0) || | ||
(a is num && b is num && a < b); | ||
} |
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
Oops, something went wrong.