Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to string in includes #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const evalLiteral = node => {
assert.strictEqual(node.type, NODE_LITERAL);

const { value } = node;
const typeNames = [TYPE_NUMBER, TYPE_BOOLEAN, TYPE_STRING];
const typeNames = [TYPE_NUMBER, TYPE_BOOLEAN, TYPE_STRING, TYPE_OBJECT];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove TYPE_OBJECT from typeNames since it would be an error if a node of type NODE_LITERAL has an object as value.

assert(value === null || typeNames.includes(typeof value), `Expected one of ${typeNames.map(t => `'${t}'`).join(', ')} or null as argument`);

return value;
Expand Down
49 changes: 43 additions & 6 deletions lib/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,19 @@ const builtIns = {

concat: new AbstractFunction(),
endsWith: new AbstractFunction(),
includes: new AbstractFunction(),
includes: new GenericFunction({ type: NODE_BUILTIN_METHOD_IMPL, targetClass: 'String', name: 'includes' }, [{ name: 'searchElement' }, { name: 'fromIndex' }]),
indexOf: new AbstractFunction(),
lastIndexOf: new AbstractFunction(),
localeCompare: new AbstractFunction(),
match: new AbstractFunction(),
matchAll: new AbstractFunction(),
match: new GenericFunction({ type: NODE_BUILTIN_METHOD_IMPL, targetClass: 'String', name: 'match' }, [{ name: 'pattern' }]),
matchAll: new GenericFunction({ type: NODE_BUILTIN_METHOD_IMPL, targetClass: 'String', name: 'matchAll' }, [{ name: 'pattern' }]),
normalize: new AbstractFunction(),
padEnd: new AbstractFunction(),
padStart: new AbstractFunction(),
repeat: new AbstractFunction(),
replace: new AbstractFunction(),
replaceAll: new AbstractFunction(),
search: new AbstractFunction(),
replace: new GenericFunction({ type: NODE_BUILTIN_METHOD_IMPL, targetClass: 'String', name: 'replace' }, [{ name: 'searchValue' }, { name: 'newValue' }]),
replaceAll: new GenericFunction({ type: NODE_BUILTIN_METHOD_IMPL, targetClass: 'String', name: 'replaceAll' }, [{ name: 'searchValue' }, { name: 'newValue' }]),
search: new GenericFunction({ type: NODE_BUILTIN_METHOD_IMPL, targetClass: 'String', name: 'search' }, [{ name: 'pattern' }]),
slice: new AbstractFunction(),
split: new AbstractFunction(),
startsWith: new AbstractFunction(),
Expand Down Expand Up @@ -318,6 +318,43 @@ const builtInsImpls = {
valueOf: (target, context, options, runState) => {
return target.valueOf();
},
includes: (target, context, options, runState) => {
const elem = context.get('searchElement');
const from = context.get('fromIndex');
return target.includes(elem, from);
},
match: (target, context, options, runState) => {
const pattern = context.get('pattern');
return target.match(pattern);
},
matchAll: (target, context, options, runState) => {
const pattern = context.get('pattern');
return target.match(pattern);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call matchAll instead.

},
replace: (target, context, options, runState) => {
const searchValue = context.get('searchValue');
const newValue = context.get('newValue');
return target.replace(
searchValue,
newValue instanceof AbstractFunction ?
(x) => newValue.exec(x, context, options, runState) :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exec method requires the first argument to be an array of parameters. Please fix the code to pass in all the arguments to the replacer function.

newValue
)
},
replaceAll: (target, context, options, runState) => {
const searchValue = context.get('searchValue');
const newValue = context.get('newValue');
return target.replaceAll(
searchValue,
newValue instanceof AbstractFunction ?
(x) => newValue.exec(x, context, options, runState) :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exec method requires the first argument to be an array of parameters. Please fix the code to pass in all the arguments to the replacer function.

newValue
);
},
search: (target, context, options, runState) => {
const pattern = context.get('pattern');
return target.search(pattern);
},
},
JSON: {
parse: (target, context, options, runState) => {
Expand Down