-
-
Save ozten/0a2e982044de8830f105 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
macro _match_cond { | |
rule {$o($field)} => { | |
(typeof $o.$field !== 'undefined') | |
} | |
rule {$o($field $rest...)} => { | |
_match_cond $o($field) && _match_cond $o($rest...) | |
} | |
} | |
macro _match_var { | |
rule {$o($field)} => { | |
var $field = $o.$field; | |
} | |
rule {$o($field $rest...)} => { | |
_match_var $o($field) | |
_match_var $o($rest...) | |
} | |
} | |
macro match { | |
rule {$tomatch:expr { | |
$({ | |
$field: ident(, )... | |
} => { | |
$body... | |
})...} | |
} => { | |
var obj = $tomatch; | |
$( | |
if (_match_cond obj($field...)) { | |
_match_var obj($field...) | |
$body... | |
})... | |
} | |
} | |
match { id: "myid", foo: 42 } { | |
{ id, foo } => { | |
log(id); | |
log(foo + 24); | |
} { | |
id | |
} => { | |
log(id) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment