string-matching
Advanced tools
Comparing version 1.3.0 to 1.4.0
{ | ||
"name": "string-matching", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Checks strings against patterns and collects matched substrings", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
const smp = require('./string_matching_parser') | ||
var _set_key = (step, val, dict) => { | ||
var _set_key = (step, val, dict, throw_matching_errors, path) => { | ||
var v | ||
@@ -24,16 +24,26 @@ if(!step.type || step.type == 'str') { | ||
} | ||
if(isNaN(v)) throw new Error("Invalid value for key '" + step.name + "'") | ||
if(isNaN(v)) throw new Error(`${path}: Invalid value for key '${step.name}'`) | ||
} | ||
if(dict[step.name]) { | ||
if(dict[step.name] != v) throw new Error(step.name + " value cannot be set to " + v + " because it is already set to " + dict[step.name]) | ||
if(dict[step.name] != v) { | ||
if(throw_matching_errors) { | ||
throw new Error(`${path}: '${step.name}' value cannot be set to '${v}' because it is already set to ${dict[step.name]}`) | ||
} else { | ||
return false | ||
} | ||
} | ||
} else { | ||
dict[step.name] = v | ||
} | ||
return true | ||
} | ||
var _match = (steps, received, dict, throw_matching_errors) => { | ||
if(typeof received != 'string') throw new Error('Received element is not string') | ||
var _match = (steps, received, dict, throw_matching_errors, path) => { | ||
if(typeof received != 'string') throw new Error(`${path}: Received element is not string`) | ||
var remainder = received | ||
var collected = []; | ||
@@ -46,3 +56,3 @@ for(var i=0 ; i<steps.length ; i++) { | ||
if(throw_matching_errors) { | ||
throw new Error("Expected substr '" + step.str + "' not found") | ||
throw new Error(`${path}: Expected substr '${step.str}' not found`) | ||
} else { | ||
@@ -57,4 +67,9 @@ return false | ||
collected_str = remainder.substring(0, step.length) | ||
if(collected_str.length < step.length) throw new Error("Not enough chars to be collected in element") | ||
if(collected_str.length < step.length) { | ||
if(throw_matching_errors) { | ||
throw new Error(`${path}: Not enough chars to be collected in element`) | ||
} else { | ||
return false | ||
} | ||
} | ||
remainder = remainder.slice(step.length) | ||
@@ -68,3 +83,3 @@ } else { | ||
// we dont use (pos <= 0) because it is OK to collect empty strings | ||
throw new Error("Expected string collection delimiter '" + next_step.str + "' not found") | ||
throw new Error(`${path}: Expected string collection delimiter '${next_step.str}' not found`) | ||
} else { | ||
@@ -82,15 +97,19 @@ return false | ||
} | ||
_set_key(step, collected_str, dict) | ||
collected.push([step, collected_str]) | ||
} else { | ||
if(throw_matching_errors) { | ||
throw new Error("Invalid match step") | ||
} else { | ||
return false | ||
} | ||
// This indicates bug in our code | ||
throw new Error(`${path}: Invalid match step ${JSON.stringify(step)}`) | ||
} | ||
} | ||
} | ||
collected.forEach(function(a) { | ||
var step = a[0]; | ||
var val = a[1]; | ||
if(!_set_key(step, val, dict, throw_matching_errors, path)) { | ||
return false | ||
} | ||
}); | ||
return true | ||
} | ||
var gen_matcher = (expected, throw_matching_errors) => { | ||
var gen_matcher = (expected) => { | ||
var steps | ||
@@ -103,10 +122,10 @@ try { | ||
} | ||
return (received, dict) => { | ||
return _match(steps, received, dict, throw_matching_errors) | ||
return (received, dict, throw_matching_errors, path) => { | ||
return _match(steps, received, dict, throw_matching_errors, path) | ||
} | ||
} | ||
var match = (expected, received, dict) => { | ||
var match = (expected, received, dict, throw_matching_errors, path) => { | ||
var matcher = gen_matcher(expected) | ||
return matcher(received, dict) | ||
return matcher(received, dict, throw_matching_errors, path) | ||
} | ||
@@ -113,0 +132,0 @@ |
@@ -7,3 +7,3 @@ const sm = require('../src/index'); | ||
var matcher = sm.gen_matcher(`"${alias}" <${proto}:!{user}@!{ip}:!{port:num};tag=!{tag:str:30};phone=!{phone:str:10}>`, false) | ||
var matcher = sm.gen_matcher(`"${alias}" <${proto}:!{user}@!{ip}:!{port:num};tag=!{tag:str:30};phone=!{phone:str:10}>`) | ||
@@ -40,3 +40,3 @@ var user = 'tomjones' | ||
expect( () => { matcher(`name=sulu`, dict) }).toThrow(/already/) | ||
expect( () => { matcher(`name=sulu`, dict, true, 'some_path') }).toThrow(/already/) | ||
}) |
151009
22
1369