Last active
February 4, 2020 20:50
-
-
Save kirjavascript/2645b6541595cfb65471d425ddc568d7 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
| const { | |
| str, | |
| char, | |
| digits, | |
| letters, | |
| sequenceOf, | |
| anythingExcept, | |
| anyOfString, | |
| many, | |
| choice, | |
| optionalWhitespace, | |
| between, | |
| } = require('arcsecond'); | |
| const trueValue = str('true').map(() => true); | |
| const falseValue = str('false').map(() => false); | |
| const delimiter = anyOfString(`"'\``); | |
| const stringValue = between(delimiter) (delimiter) ( | |
| many(anythingExcept(delimiter)) | |
| ).map(arr => arr.join('')); | |
| const numberValue = digits.map(Number); | |
| const access = sequenceOf([char('.'), letters]).map(([,tail]) => tail); | |
| const propertyValue = sequenceOf([ | |
| letters, | |
| many(access), | |
| ]).map(([name, accesses]) => [name, ...accesses]); | |
| const value = choice([ | |
| trueValue, | |
| falseValue, | |
| numberValue, | |
| stringValue, | |
| propertyValue, | |
| ]); | |
| const equalityOperator = choice([ | |
| str('=='), | |
| str('!='), | |
| ]); | |
| const zipWhitespace = (arr) => arr.map((p) => | |
| sequenceOf([optionalWhitespace, p, optionalWhitespace]) | |
| .map(([,tail]) => tail) | |
| ); | |
| const equality = sequenceOf(zipWhitespace([ | |
| value, | |
| equalityOperator, | |
| value, | |
| ])); | |
| console.log(equality.run('true == false').result) | |
| [ true, '==', false ] | |
| console.log(equality.run('true == 3').result) | |
| [ true, '==', 3 ] | |
| console.log(equality.run(" 'test123'==3 ").result) | |
| [ 'test123', '==', 3 ] | |
| console.log(equality.run('this.object.name == "hello" ').result) | |
| [ [ 'this', 'object', 'name' ], '==', 'hello' ] | |
| console.log(equality.run("window == false ").result) | |
| [ [ 'window' ], '==', false ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment