agreed-core
Advanced tools
Comparing version 2.2.1 to 2.3.0
'use strict'; | ||
const hasTemplate = require('../template/hasTemplate').hasTemplate; | ||
const { isRestArrayTemplate, hasTemplate } = require('../template/hasTemplate'); | ||
@@ -13,5 +13,11 @@ module.exports = function isInclude(small, large) { | ||
if (typeof small != 'object') { | ||
if (hasTemplate(small) && large != null) { | ||
if (hasTemplate(small) && large !== undefined) { | ||
return true; | ||
} | ||
// {:foo.1-last} is rest array template | ||
if (isRestArrayTemplate(small)) { | ||
return true; | ||
} | ||
return small == large; | ||
@@ -18,0 +24,0 @@ } |
@@ -0,3 +1,3 @@ | ||
'use strict'; | ||
const constants = require('./constants'); | ||
const merge = require('deepmerge'); | ||
const hasTemplate = require('./hasTemplate').hasTemplate; | ||
@@ -8,19 +8,19 @@ | ||
if (typeof hasFormatObj === 'object') { | ||
const keys = Object.keys(hasFormatObj); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const keysWithTemplates = Object.keys(hasFormatObj).filter((key) => { | ||
if (hasTemplate(hasFormatObj[key])) { | ||
const formatName = hasFormatObj[key].replace(constants.TEMPLATE_REGEXP, '$2'); | ||
const names = formatName.split('.'); | ||
if (names.length > 1 && typeof hasValueObj[key] === 'object') { | ||
result = merge(result, walkTree(names, hasValueObj[key])); | ||
} else if (names.length > 1) { | ||
result = merge(result, walkTree(names, hasValueObj)); | ||
} else { | ||
result[formatName] = hasValueObj[key]; | ||
if (names.length > 1) { | ||
names.forEach((name) => { | ||
if (hasValueObj[name]) { | ||
result[name] = hasValueObj[name]; | ||
} | ||
}); | ||
return; | ||
} | ||
result[formatName] = hasValueObj[key]; | ||
} else if (hasFormatObj[key] && typeof hasFormatObj[key] === 'object') { | ||
return bind(hasFormatObj[key], hasValueObj[key], result); | ||
bind(hasFormatObj[key], hasValueObj[key], result); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -30,19 +30,1 @@ | ||
}; | ||
// keys: [ a, b, c ] | ||
// values: { a: { b: { c: 123, d: 223 } } } | ||
// return { a: { b: { c: 123 } } } | ||
function walkTree(keys, values) { | ||
const key = keys[0]; | ||
const result = {}; | ||
if (values[key] && keys.length >= 2) { | ||
result[key] = walkTree(keys.slice(1), values[key], result[key]); | ||
return result; | ||
} else if (keys.length >= 2) { | ||
result[key] = walkTree(keys.slice(1), values, result[key]); | ||
return result; | ||
} else { | ||
result[key] = values[key]; | ||
return result; | ||
} | ||
} |
'use strict'; | ||
const OPERATIONAL_STRINGS = 'randomInt'; | ||
const OPERATIONAL_STRINGS = 'randomInt|parseInt'; | ||
const REGEXP_STRING = `{(${OPERATIONAL_STRINGS})*:([\\w|\\.|:|\\[|\\]|-]+)\\}`; | ||
const BRACKETS_STRING = '\\[:([\\w]+)\\]'; | ||
const REST_ARRAY_STRING = '{:.+\\d\-last}'; | ||
const REST_ARRAY_STRING = '{:.+\\d+\-last}'; | ||
@@ -8,0 +8,0 @@ const TEMPLATE_REGEXP_GLOBAL = new RegExp(REGEXP_STRING, 'g'); |
'use strict' | ||
const randomInt = require('./randomInt'); | ||
const parseInt = require('./parseInt'); | ||
function getRandom(min = 0, max = Number.MAX_SAFE_INTEGER) { | ||
if (min > max) { | ||
max = min; | ||
min = 0; | ||
} | ||
return Math.trunc(Math.random() * (max - min) + min); | ||
} | ||
function randomInt(value) { | ||
if (typeof value === 'number') { | ||
return getRandom(value); | ||
} | ||
const matched = value.match(/(\d+)-(\d+)/); | ||
if (!matched) { | ||
return getRandom(); | ||
} | ||
const min = matched[1]; | ||
const max = matched[2]; | ||
return getRandom(min, max); | ||
} | ||
module.exports = (operation, ...values) => { | ||
@@ -30,3 +12,8 @@ if (operation === 'randomInt') { | ||
return result; | ||
} else if (operation === 'parseInt') { | ||
const [ value ] = values; | ||
const result = parseInt(value); | ||
return result; | ||
} | ||
}; |
{ | ||
"name": "agreed-core", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "agreed is a mock server and test client, agreed will be helper for Consumer Driven Contract", | ||
@@ -31,3 +31,2 @@ "main": "index.js", | ||
"dependencies": { | ||
"deepmerge": "^1.4.4", | ||
"json5": "^0.5.0", | ||
@@ -34,0 +33,0 @@ "jsonschema": "^1.1.0", |
@@ -23,8 +23,12 @@ 'use strict'; | ||
}; | ||
const req = http.request(options, (res) => { | ||
const assert = new AssertStream(); | ||
assert.expect({ messages: [ { message: null }, { message: 'test' } ] }); | ||
res.pipe(process.stdout); | ||
const req = http.request(options, mustCall((res) => { | ||
let data = ''; | ||
res.on('data', (d) => data += d); | ||
res.on('end', mustCall(() => { | ||
const actual = JSON.parse(data); | ||
const expected = { messages: [ { message: null }, { message: 'test' } ] }; | ||
assert.deepStrictEqual(actual, expected); | ||
})); | ||
server.close(); | ||
}).on('error', console.error); | ||
})).on('error', console.error); | ||
@@ -31,0 +35,0 @@ req.end(); |
@@ -22,25 +22,11 @@ 'use strict'; | ||
test('bind: { key: { foo: "{:value.foo}" }, { key: { foo: { value: { foo: 12345 } } } } => { value: { foo: 12345 } }', () => { | ||
const result = bind({ key: { foo: "{:value.foo}" }}, { key: { foo: { value: { foo: 12345 } } } }); | ||
assert.deepEqual(result, { value: { foo: 12345 } }); | ||
test('bind: { key: { foo: "{:value.foo}" }, { key: { value: { foo: 12345 } } } => { value: { foo: 12345 } }', () => { | ||
const result = bind({ key: { foo: "{:value.foo}" }}, { key: { value: { foo: 12345 } } }); | ||
assert.deepEqual(result, {value: { foo: 12345 } }); | ||
}); | ||
test('bind: { key: { time: { start: "{:time.start}", end: "{:time.end}" } } }, { key: { time: { start: 12345, end: 3456 } } } => { time: { start: 12345, end: 3456 } }', () => { | ||
const result = bind({ | ||
key: { | ||
time: { | ||
start: "{:time.start}", | ||
end: "{:time.end}", | ||
} | ||
} | ||
}, { | ||
key: { | ||
time: { | ||
start: 12345, | ||
end: 3456 | ||
} | ||
} | ||
}); | ||
assert.deepEqual(result, { time: { start: 12345, end: 3456 } }); | ||
test('bind: { key: { foo: "{:value.foo.bar.0.baz}" }, { key: { value: { foo: { bar: [ { baz : 12345 } ] } } } } => { value: { foo: { bar: [ { baz: 12345 } ] } } }', () => { | ||
const result = bind({ key: { foo: "{:value.foo.bar.0.baz}" } }, { key: { value: { foo: { bar: [ { baz : 12345 } ] } } } }); | ||
const expect = { value: { foo: { bar: [ { baz: 12345 } ] } } }; | ||
assert.deepStrictEqual(result, expect); | ||
}); | ||
@@ -26,1 +26,5 @@ 'use strict'; | ||
test('format: {parseInt:id}', () => { | ||
const result = format({ id: '{parseInt:id}' }, { id: '10000' }); | ||
assert.deepStrictEqual(result.id, 10000); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85283
4
65
2662
11
- Removeddeepmerge@^1.4.4
- Removeddeepmerge@1.5.2(transitive)