diff --git a/lib/function.js b/lib/function.js index dee70d0..9c1d0ec 100644 --- a/lib/function.js +++ b/lib/function.js @@ -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(), @@ -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.matchAll(pattern); + }, + 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) : + 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) : + newValue + ); + }, + search: (target, context, options, runState) => { + const pattern = context.get('pattern'); + return target.search(pattern); + }, }, JSON: { parse: (target, context, options, runState) => {