From 88773a4ae56ce75e038d86d7b55bda8cb66e30fc Mon Sep 17 00:00:00 2001 From: Ayman Farhat Date: Tue, 12 May 2020 15:32:34 +0400 Subject: [PATCH 1/2] Switches to exec instead of match for regex pattern evaluation to support matching patterns for number values --- index.js | 4 ++-- package-lock.json | 2 +- test/test_objectron.js | 52 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d6b8800..2920a31 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ const match = (payload, pattern, callback) => { * with the result object */ if (value instanceof RegExp) { - const matcher = payload[key].match(value) || [] + const matcher = value.exec(payload[key]) || [] if (matcher.length > 0) { result.groups = { ...result.groups, ...matcher.groups } currentNode[key] = payload[key] @@ -49,7 +49,7 @@ const match = (payload, pattern, callback) => { * Level N depth RegExp handling */ if (element instanceof RegExp) { - const matcher = payload[key][index].match(element) || [] + const matcher = element.exec(payload[key][index]) || [] if (matcher.length > 0) { result.groups = { ...result.groups, ...matcher.groups } currentNode[key] = payload[key] diff --git a/package-lock.json b/package-lock.json index 139fad1..54c6630 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@menadevs/objectron", - "version": "0.1.10", + "version": "0.1.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/test_objectron.js b/test/test_objectron.js index cf97963..8edb6ca 100644 --- a/test/test_objectron.js +++ b/test/test_objectron.js @@ -389,6 +389,42 @@ suite('Objectron Core Tests', () => { assert.deepEqual(result, expected) }) + test('Match number type values against regex pattern', () => { + const payload = { + age: 15, + shoeSize: 44.5, + temperature: { + type: 'celcius', + degree: -20 + } + }; + + const result = match(payload, { + age: /\d*/, + shoeSize: /(\d+\.?\d*|\d*\.?\d+)/, + temperature: { + degree: /\-\d*/ + } + }); + + const expected = { + match: true, + total: 3, + matches: { + age: 15, + shoeSize: 44.5, + temperature: { + degree: -20 + } + }, + groups: {} + } + + + assert.isTrue(result.match); + assert.deepEqual(result, expected); + }); + test('Callback fired on match', () => { let called = false @@ -414,7 +450,8 @@ suite('Objectron Core Tests', () => { fields: [ { type: 'plain_text', - text: 'going home?' + text: 'going home?', + rating: 5.5 }, { type: 'markdown', @@ -432,7 +469,8 @@ suite('Objectron Core Tests', () => { fields: [ { type: 'plain_text', - text: /(?\S+) (?.+)?/ + text: /(?\S+) (?.+)?/, + rating: /(\d+\.?\d*|\d*\.?\d+)/ } ] } @@ -440,13 +478,19 @@ suite('Objectron Core Tests', () => { const expected = { match: true, - total: 7, + total: 8, matches: { api: 13, ids: [130, 45, 12], components: { type: 'section', - fields: [{ type: 'plain_text', text: 'going home?' }] + fields: [ + { + type: 'plain_text', + text: 'going home?', + rating: 5.5 + } + ] } }, groups: { verb: 'going', what: 'home?' } From b99195f52480934c17316756911ef957d12fff6e Mon Sep 17 00:00:00 2001 From: Bassem Dghaidi Date: Tue, 12 May 2020 13:54:07 +0200 Subject: [PATCH 2/2] Release 0.1.12 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dc67a6c..690ca7f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@menadevs/objectron", - "version": "0.1.11", + "version": "0.1.12", "description": "Compares a set of match rules contained with an object to determine if the latter conforms to the matching rules", "main": "index.js", "devDependencies": {