Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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