string-matching
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "string-matching", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Checks strings against patterns and collects matched substrings", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,4 +1,5 @@ | ||
const MatchingError = require ('./matching_error') | ||
const smp = require('./string_matching_parser') | ||
var _set_key = (step, val, dict, throw_matching_errors, path) => { | ||
var _set_key = (step, val, dict, throw_matching_error, path) => { | ||
var v | ||
@@ -29,4 +30,4 @@ if(!step.type || step.type == 'str') { | ||
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]}`) | ||
if(throw_matching_error) { | ||
throw new MatchingError(`value cannot be set to '${v}' because it is already set to ${dict[step.name]}`, path) | ||
} else { | ||
@@ -43,4 +44,7 @@ return false | ||
var _match = (steps, received, dict, throw_matching_errors, path) => { | ||
if(typeof received != 'string') throw new Error(`${path}: Received element is not string`) | ||
var _match = (steps, received, dict, throw_matching_error, path) => { | ||
if(typeof received != 'string') { | ||
if(throw_matching_error) throw new MatchingError("Received element is not string", path) | ||
return false | ||
} | ||
@@ -56,7 +60,4 @@ var remainder = received | ||
if(remainder.substr(0, step.str.length) != step.str){ | ||
if(throw_matching_errors) { | ||
throw new Error(`${path}: Expected substr '${step.str}' not found`) | ||
} else { | ||
return false | ||
} | ||
if(throw_matching_error) throw new MatchingError(`Expected substr '${step.str}' not found`, path) | ||
return false | ||
} | ||
@@ -69,7 +70,4 @@ remainder = remainder.slice(step.str.length) | ||
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 | ||
} | ||
if(throw_matching_error) throw new MatchingError("Not enough chars to be collected in element", path) | ||
return false | ||
} | ||
@@ -82,8 +80,5 @@ remainder = remainder.slice(step.length) | ||
if(pos < 0) { | ||
if(throw_matching_errors) { | ||
// we dont use (pos <= 0) because it is OK to collect empty strings | ||
throw new Error(`${path}: Expected string collection delimiter '${next_step.str}' not found`) | ||
} else { | ||
return false | ||
} | ||
// we dont use (pos <= 0) because it is OK to collect empty strings | ||
if(throw_matching_error) throw new MatchingError(`Expected string collection delimiter '${next_step.str}' not found`, path) | ||
return false | ||
} | ||
@@ -100,4 +95,3 @@ collected_str = remainder.substring(0, pos) | ||
} else { | ||
// This indicates bug in our code | ||
throw new Error(`${path}: Invalid match step ${JSON.stringify(step)}`) | ||
throw new Error(`${path}: Invalid match step ${JSON.stringify(step)}. This might be bug in our code.`) | ||
} | ||
@@ -108,3 +102,3 @@ } | ||
var val = a[1]; | ||
if(!_set_key(step, val, dict, throw_matching_errors, path)) { | ||
if(!_set_key(step, val, dict, throw_matching_error, path)) { | ||
return false | ||
@@ -124,10 +118,10 @@ } | ||
} | ||
return (received, dict, throw_matching_errors, path) => { | ||
return _match(steps, received, dict, throw_matching_errors, path) | ||
return (received, dict, throw_matching_error, path) => { | ||
return _match(steps, received, dict, throw_matching_error, path) | ||
} | ||
} | ||
var match = (expected, received, dict, throw_matching_errors, path) => { | ||
var match = (expected, received, dict, throw_matching_error, path) => { | ||
var matcher = gen_matcher(expected) | ||
return matcher(received, dict, throw_matching_errors, path) | ||
return matcher(received, dict, throw_matching_error, path) | ||
} | ||
@@ -138,3 +132,4 @@ | ||
match: match, | ||
MatchingError: MatchingError, | ||
} | ||
151466
23
1377