From b0f4eda81949990c55ceb03ff25560206f0701b4 Mon Sep 17 00:00:00 2001 From: Alison <10372732+alisonrag@users.noreply.github.com> Date: Tue, 3 Sep 2024 13:34:42 -0300 Subject: [PATCH 1/5] feat: add support to string in includes --- lib/function.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/function.js b/lib/function.js index dee70d0..98daf26 100644 --- a/lib/function.js +++ b/lib/function.js @@ -154,7 +154,7 @@ 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(), @@ -318,6 +318,11 @@ 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); + }, }, JSON: { parse: (target, context, options, runState) => { From 3118a83cb2d49f4ecc19beb93456b7403901e916 Mon Sep 17 00:00:00 2001 From: Alison <10372732+alisonrag@users.noreply.github.com> Date: Sat, 7 Sep 2024 15:19:39 -0300 Subject: [PATCH 2/5] feat: add more functions --- lib/function.js | 43 ++++++++++++++++++++++++++++++++++++++----- lib/utils.js | 12 ++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 lib/utils.js diff --git a/lib/function.js b/lib/function.js index 98daf26..14337a6 100644 --- a/lib/function.js +++ b/lib/function.js @@ -1,6 +1,7 @@ const assert = require('assert'); const { evalExpressionNode, evalBlockStatement, evalBuiltInMethodImpl, NODE_BLOCK_STATEMENT, NODE_BUILTIN_METHOD_IMPL } = require('./eval'); const { Context } = require('./context'); +const { regExpFromString } = require('./utils'); class AbstractFunction { exec() { @@ -158,15 +159,15 @@ const builtIns = { 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(), @@ -323,6 +324,38 @@ const builtInsImpls = { const from = context.get('fromIndex'); return target.includes(elem, from); }, + match: (target, context, options, runState) => { + const pattern = context.get('pattern'); + return target.match(regExpFromString(pattern)); + }, + matchAll: (target, context, options, runState) => { + const pattern = context.get('pattern'); + return target.match(regExpFromString(pattern)); + }, + replace: (target, context, options, runState) => { + const searchValue = context.get('searchValue'); + const newValue = context.get('newValue'); + return target.replace( + regExpFromString(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( + regExpFromString(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(regExpFromString(pattern)); + }, }, JSON: { parse: (target, context, options, runState) => { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..3bd33b5 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,12 @@ +export function regExpFromString(q) { + let flags = q.replace(/.*\/([gimuy]*)$/, "$1"); + if (flags === q) flags = ""; + let pattern = flags + ? q.replace(new RegExp("^/(.*?)/" + flags + "$"), "$1") + : q; + try { + return new RegExp(pattern, flags); + } catch (e) { + return null; + } +} From eccebdcd21702db4157ce1de5ca6182437f2405c Mon Sep 17 00:00:00 2001 From: Alison <10372732+alisonrag@users.noreply.github.com> Date: Sat, 7 Sep 2024 15:24:55 -0300 Subject: [PATCH 3/5] fix export --- lib/utils.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 3bd33b5..03547e2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,4 +1,4 @@ -export function regExpFromString(q) { +function regExpFromString(q) { let flags = q.replace(/.*\/([gimuy]*)$/, "$1"); if (flags === q) flags = ""; let pattern = flags @@ -10,3 +10,7 @@ export function regExpFromString(q) { return null; } } + +module.exports = { + regExpFromString +}; From e2c29133546d88f8bca6f2633a89312a1af33a4d Mon Sep 17 00:00:00 2001 From: Alison <10372732+alisonrag@users.noreply.github.com> Date: Sat, 7 Sep 2024 15:49:58 -0300 Subject: [PATCH 4/5] chore: remove function --- lib/eval.js | 2 +- lib/function.js | 11 +++++------ lib/utils.js | 16 ---------------- 3 files changed, 6 insertions(+), 23 deletions(-) delete mode 100644 lib/utils.js diff --git a/lib/eval.js b/lib/eval.js index fb5bbb7..39fd4f7 100644 --- a/lib/eval.js +++ b/lib/eval.js @@ -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]; assert(value === null || typeNames.includes(typeof value), `Expected one of ${typeNames.map(t => `'${t}'`).join(', ')} or null as argument`); return value; diff --git a/lib/function.js b/lib/function.js index 14337a6..0ea30e5 100644 --- a/lib/function.js +++ b/lib/function.js @@ -1,7 +1,6 @@ const assert = require('assert'); const { evalExpressionNode, evalBlockStatement, evalBuiltInMethodImpl, NODE_BLOCK_STATEMENT, NODE_BUILTIN_METHOD_IMPL } = require('./eval'); const { Context } = require('./context'); -const { regExpFromString } = require('./utils'); class AbstractFunction { exec() { @@ -326,17 +325,17 @@ const builtInsImpls = { }, match: (target, context, options, runState) => { const pattern = context.get('pattern'); - return target.match(regExpFromString(pattern)); + return target.match(pattern); }, matchAll: (target, context, options, runState) => { const pattern = context.get('pattern'); - return target.match(regExpFromString(pattern)); + return target.match(pattern); }, replace: (target, context, options, runState) => { const searchValue = context.get('searchValue'); const newValue = context.get('newValue'); return target.replace( - regExpFromString(searchValue), + searchValue, newValue instanceof AbstractFunction ? (x) => newValue.exec(x, context, options, runState) : newValue @@ -346,7 +345,7 @@ const builtInsImpls = { const searchValue = context.get('searchValue'); const newValue = context.get('newValue'); return target.replaceAll( - regExpFromString(searchValue), + searchValue, newValue instanceof AbstractFunction ? (x) => newValue.exec(x, context, options, runState) : newValue @@ -354,7 +353,7 @@ const builtInsImpls = { }, search: (target, context, options, runState) => { const pattern = context.get('pattern'); - return target.search(regExpFromString(pattern)); + return target.search(pattern); }, }, JSON: { diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index 03547e2..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,16 +0,0 @@ -function regExpFromString(q) { - let flags = q.replace(/.*\/([gimuy]*)$/, "$1"); - if (flags === q) flags = ""; - let pattern = flags - ? q.replace(new RegExp("^/(.*?)/" + flags + "$"), "$1") - : q; - try { - return new RegExp(pattern, flags); - } catch (e) { - return null; - } -} - -module.exports = { - regExpFromString -}; From bc65d055ba7838d33df625ee3d54e2bd9a4df279 Mon Sep 17 00:00:00 2001 From: Alison <10372732+alisonrag@users.noreply.github.com> Date: Thu, 24 Apr 2025 12:47:47 -0300 Subject: [PATCH 5/5] fix: updates functions to work as desired --- lib/eval.js | 2 +- lib/function.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/eval.js b/lib/eval.js index 39fd4f7..fb5bbb7 100644 --- a/lib/eval.js +++ b/lib/eval.js @@ -143,7 +143,7 @@ const evalLiteral = node => { assert.strictEqual(node.type, NODE_LITERAL); const { value } = node; - const typeNames = [TYPE_NUMBER, TYPE_BOOLEAN, TYPE_STRING, TYPE_OBJECT]; + const typeNames = [TYPE_NUMBER, TYPE_BOOLEAN, TYPE_STRING]; assert(value === null || typeNames.includes(typeof value), `Expected one of ${typeNames.map(t => `'${t}'`).join(', ')} or null as argument`); return value; diff --git a/lib/function.js b/lib/function.js index 0ea30e5..9c1d0ec 100644 --- a/lib/function.js +++ b/lib/function.js @@ -329,7 +329,7 @@ const builtInsImpls = { }, matchAll: (target, context, options, runState) => { const pattern = context.get('pattern'); - return target.match(pattern); + return target.matchAll(pattern); }, replace: (target, context, options, runState) => { const searchValue = context.get('searchValue'); @@ -337,7 +337,7 @@ const builtInsImpls = { return target.replace( searchValue, newValue instanceof AbstractFunction ? - (x) => newValue.exec(x, context, options, runState) : + (x) => newValue.exec([x], context, options, runState) : newValue ) }, @@ -347,7 +347,7 @@ const builtInsImpls = { return target.replaceAll( searchValue, newValue instanceof AbstractFunction ? - (x) => newValue.exec(x, context, options, runState) : + (x) => newValue.exec([x], context, options, runState) : newValue ); },