data-matching
Advanced tools
Comparing version 1.25.0 to 1.26.0
{ | ||
"name": "data-matching", | ||
"version": "1.25.0", | ||
"version": "1.26.0", | ||
"description": "Matches a data object against a reference value Edit", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,5 +5,5 @@ # data-matching | ||
# Syntax details | ||
- Part of strings can be collected using '!{name}' syntax (see https://github.com/MayamaTakeshi/string-matching) | ||
- data can be collected (stored in a dictionary passed to matching function) by using dm.collect('name') | ||
- data that should be absent can be declared with dm.absent | ||
- you can do string matching and substring capture by using gen_matcher from [string-matching](https://www.npmjs.com/package/string-matching) | ||
@@ -16,3 +16,2 @@ # Sample usage | ||
const assert = require('assert') | ||
const s = require('string-matching').gen_matcher; | ||
@@ -46,3 +45,3 @@ var received = { | ||
port: 9, | ||
protocol: s('!{transport_protocol}/MRCP!{mrcp_version}'), | ||
protocol: '!{transport_protocol}/MRCP!{mrcp_version}', | ||
payloads: [dm.collect('mrcp_payload')], | ||
@@ -76,3 +75,2 @@ setup: 'active', | ||
const assert = require('assert') | ||
const s = require('string-matching').gen_matcher; | ||
@@ -90,5 +88,5 @@ | ||
var expected = { | ||
request_uri: s('sip:!{user1}@!{domain1}'), | ||
from_uri: s('<sip:!{user2}@!{domain2}>'), | ||
cseq: s('!{method} 50'), | ||
request_uri: 'sip:!{user1}@!{domain1}', | ||
from_uri: '<sip:!{user2}@!{domain2}>', | ||
cseq: '!{method} 50', | ||
some_absent_key: dm.absent, | ||
@@ -95,0 +93,0 @@ } |
@@ -228,5 +228,16 @@ const _ = require('lodash') | ||
var matchify_strings = (evt) => { | ||
return _deepMap(evt, (x) => { | ||
if(typeof x == 'string' && x.match(re_string_matching_indication)) { | ||
return sm.gen_matcher(x) | ||
} else { | ||
return x | ||
} | ||
}) | ||
} | ||
var partial_match = (expected) => { | ||
var expected2 = matchify_strings(expected) | ||
var f = (received, dict, throw_matching_error, path) => { | ||
return _match(expected, received, dict, false, throw_matching_error, path) | ||
return _match(expected2, received, dict, false, throw_matching_error, path) | ||
} | ||
@@ -239,4 +250,5 @@ f.__original_data__ = expected | ||
var full_match = (expected) => { | ||
var expected2 = matchify_strings(expected) | ||
var f = (received, dict, throw_matching_error, path) => { | ||
return _match(expected, received, dict, true, throw_matching_error, path); | ||
return _match(expected2, received, dict, true, throw_matching_error, path); | ||
} | ||
@@ -249,5 +261,6 @@ f.__original_data__ = expected | ||
var json = (expected, full_match) => { | ||
var expected2 = matchify_strings(expected) | ||
var f = (s, dict, throw_matching_error, path) => { | ||
var received = JSON.parse(s); | ||
return _match(expected, received, dict, full_match, throw_matching_error, path); | ||
return _match(expected2, received, dict, full_match, throw_matching_error, path); | ||
} | ||
@@ -260,2 +273,3 @@ f.__original_data__ = expected | ||
var kv_str = (expected, param_sep, kv_sep, preparse_decoder, postparse_decoder, full_match) => { | ||
var expected2 = matchify_strings(expected) | ||
var f = (s, dict, throw_matching_error, path) => { | ||
@@ -280,3 +294,3 @@ var received = s; | ||
.value(); | ||
return _match(expected, received, dict, full_match, throw_matching_error, path); | ||
return _match(expected2, received, dict, full_match, throw_matching_error, path); | ||
} | ||
@@ -409,2 +423,3 @@ f.__original_data__ = expected | ||
matchify_strings: matchify_strings, | ||
match: _match, | ||
@@ -411,0 +426,0 @@ |
const dm = require('../src/index'); | ||
const MatchingError = dm.MatchingError; | ||
const s = require('string-matching').gen_matcher; | ||
@@ -16,3 +15,3 @@ const THROW_MATCHING_ERROR = true | ||
} | ||
throw "Should have thrown a MatchingError" | ||
throw "Shold have thrown a MatchingError" | ||
} | ||
@@ -583,24 +582,1 @@ | ||
test('check string-matching', () => { | ||
var expected = { | ||
a: s('!{user}@!{domain}:!{port}'), | ||
b: { | ||
B: s('!{schema}://!{location}'), | ||
}, | ||
} | ||
var received = { | ||
a: 'picard@sttng:1701', | ||
b: { | ||
B: 'https://somewhere.com', | ||
}, | ||
} | ||
var d = {} | ||
var res = dm.partial_match(expected)(received, d, !THROW_MATCHING_ERROR, "root") | ||
expect(res).toEqual("object matched") | ||
expect(d.user == 'picard') | ||
expect(d.domain == 'sttng') | ||
expect(d.port == '1701') | ||
expect(d.schema == 'https') | ||
expect(d.location == 'somewhere.com') | ||
}) |
26964
884
104