check-depends
Advanced tools
Comparing version 0.4.0 to 0.5.0
107
index.js
@@ -8,23 +8,21 @@ var _ = require('lodash'); | ||
* 将字符串转换为正则 | ||
* @param {any} value | ||
* @param {any} test | ||
* @returns {*|RegExp} | ||
*/ | ||
function checkRegExp(value) { | ||
if (typeof value === 'string' && value[0] === '/') { | ||
var matchs = value.match(/^\/(.+)\/([igm]*)$/); | ||
function checkRegExp(test) { | ||
if (typeof test === 'string' && test[0] === '/') { | ||
var matchs = test.match(/^\/(.+)\/([igm]*)$/); | ||
if (matchs) { | ||
value = new RegExp(matchs[1], matchs[2]); | ||
test = new RegExp(matchs[1], matchs[2]); | ||
} | ||
} | ||
return value; | ||
return test; | ||
} | ||
function getRef(value, topData) { | ||
if (typeof value === 'string' && value[0] === ':') { | ||
var key = value.substr(1); | ||
if (topData.hasOwnProperty(key)) { | ||
value = topData[value.substr(1)]; | ||
} | ||
function getRef(test, topData) { | ||
if (typeof test === 'string' && test[0] === ':') { | ||
var key = test.substr(1); | ||
return _.get(topData, key); | ||
} | ||
return value; | ||
return test; | ||
} | ||
@@ -34,3 +32,3 @@ | ||
* 检查值是否存在于数组中,支持正则数组匹配字符串值 | ||
* @param {any} value | ||
* @param {any} val | ||
* @param {Array<any>} array | ||
@@ -40,3 +38,3 @@ * @param {Object} topData | ||
*/ | ||
function inArray(value, array, topData) { | ||
function inArray(val, array, topData) { | ||
for (var i in array) { | ||
@@ -46,6 +44,6 @@ var v = array[i]; | ||
v = checkRegExp(v); | ||
if (typeof value === 'string' && v && v instanceof RegExp) { | ||
if (v.test(value)) return true; | ||
if (typeof val === 'string' && v && v instanceof RegExp) { | ||
if (v.test(val)) return true; | ||
} else { | ||
if (value === v) return true; | ||
if (val === v) return true; | ||
} | ||
@@ -59,15 +57,15 @@ } | ||
* @param {Array<any>|any} array 数组或数据 | ||
* @param {any|RegExp} element | ||
* @param {any|RegExp} test | ||
* @param {Object} topData | ||
* @returns {boolean} | ||
*/ | ||
function findArrayElement(array, element, topData) { | ||
element = getRef(element, topData); | ||
element = checkRegExp(element); | ||
if (element instanceof RegExp) { | ||
function findArrayElement(array, test, topData) { | ||
test = getRef(test, topData); | ||
test = checkRegExp(test); | ||
if (test instanceof RegExp) { | ||
if (typeof array === 'string') { | ||
return element.test(array); | ||
return test.test(array); | ||
} else if (_.isArray(array)) { | ||
return _.find(array, function (el) { | ||
return typeof el === 'string' && element.test(el); | ||
return typeof el === 'string' && test.test(el); | ||
}) | ||
@@ -80,6 +78,6 @@ } else { | ||
if (_.isArray(array)) { | ||
return array.indexOf(element) > -1; | ||
return array.indexOf(test) > -1; | ||
} | ||
return array === element; | ||
return array === test; | ||
} | ||
@@ -112,7 +110,7 @@ | ||
* @param {string} value 值 | ||
* @param {Object} exp 表达式 | ||
* @param {Object} test 表达式 | ||
* @param {Object} topData | ||
*/ | ||
function checkValue(value, exp, topData) { | ||
return _.every(exp, function (val, operator) { | ||
function checkValue(value, test, topData) { | ||
return _.every(test, function (val, operator) { | ||
val = getRef(val, topData); | ||
@@ -172,10 +170,10 @@ if (operator === '$eq') { | ||
if (operator === '$regex') { | ||
if (exp.$options !== undefined && typeof exp.$options !== 'string') { | ||
if (test.$options !== undefined && typeof test.$options !== 'string') { | ||
throw new Error('$options has to be a string'); | ||
} | ||
if (typeof val === 'string') { | ||
val = new RegExp(val, exp.$options); | ||
val = new RegExp(val, test.$options); | ||
} else if (val instanceof RegExp) { | ||
if (exp.$options) { | ||
val.compile(val.source, exp.$options); | ||
if (test.$options) { | ||
val.compile(val.source, test.$options); | ||
} | ||
@@ -191,3 +189,3 @@ } else { | ||
if (operator === '$options') { | ||
if (!exp.$regex) { | ||
if (!test.$regex) { | ||
throw new Error('$options needs a $regex'); | ||
@@ -266,3 +264,3 @@ } | ||
function checkDepends(query, data, topData) { | ||
if (query === undefined) return false; | ||
if (query === undefined || data === undefined) return false; | ||
if (query === null) return false; | ||
@@ -280,32 +278,32 @@ if (query === '') return false; | ||
topData = topData || data; | ||
return _.every(query, function (value, key) { | ||
return _.every(query, function (test, key) { | ||
// 值是引用 | ||
value = getRef(value, topData); | ||
test = getRef(test, topData); | ||
if (key === '$and') { | ||
if (!_.isArray(value)) { | ||
if (!_.isArray(test)) { | ||
throw new Error('$and must be an array'); | ||
} | ||
return _.every(value, function (v) { | ||
return _.every(test, function (v) { | ||
return checkDepends(v, data, topData); | ||
}); | ||
} else if (key === '$or') { | ||
if (!_.isArray(value)) { | ||
if (!_.isArray(test)) { | ||
throw new Error('$or must be an array'); | ||
} | ||
return _.find(value, function (v) { | ||
return _.find(test, function (v) { | ||
return checkDepends(v, data, topData); | ||
}); | ||
} else if (key === '$nor') { | ||
if (!_.isArray(value)) { | ||
if (!_.isArray(test)) { | ||
throw new Error('$nor must be an array'); | ||
} | ||
return !_.find(value, function (v) { | ||
return !_.find(test, function (v) { | ||
return checkDepends(v, data, topData); | ||
}); | ||
} else if (key === '$jsonSchema') { | ||
if (!value || typeof value !== 'object' || _.isArray(value)) { | ||
if (!test || typeof test !== 'object' || _.isArray(test)) { | ||
throw new Error('$jsonSchema must be an object'); | ||
} | ||
return ajv.validate(value, data); | ||
return ajv.validate(test, data); | ||
} else if (key[0] === '$') { | ||
@@ -315,18 +313,19 @@ throw new Error('unsupported top level operator: ' + key); | ||
value = checkRegExp(value); | ||
test = checkRegExp(test); | ||
var value = _.get(data, key); | ||
if (isExpression(value)) { | ||
if (isExpression(test)) { | ||
// 表达式对象 | ||
return checkValue(data[key], value, topData); | ||
return checkValue(value, test, topData); | ||
} | ||
if (_.isArray(data[key]) && !_.isArray(value)) { | ||
return findArrayElement(data[key], value, topData); | ||
if (_.isArray(value) && !_.isArray(test)) { | ||
return findArrayElement(value, test, topData); | ||
} | ||
if (typeof data[key] === 'string' && value instanceof RegExp) { | ||
return value.test(data[key]); | ||
if (typeof value === 'string' && test instanceof RegExp) { | ||
return test.test(value); | ||
} | ||
return _.isEqual(value, data[key]); | ||
return _.isEqual(test, value); | ||
}); | ||
@@ -333,0 +332,0 @@ } |
{ | ||
"name": "check-depends", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Check object depends", | ||
@@ -30,5 +30,5 @@ "keywords": [ | ||
"dependencies": { | ||
"ajv": "^6.5.5", | ||
"ajv": "^6.6.2", | ||
"lodash": "^4.17.11" | ||
} | ||
} |
@@ -19,3 +19,3 @@ # check-depends | ||
obj: {}, | ||
object: { attr: 1 }, | ||
object: { attr: 1, foo: 'bar' }, | ||
regexp: '/Alaska/', | ||
@@ -32,15 +32,18 @@ from: 2016, | ||
checkDepends('name',data); // true | ||
checkDepends('!name',data); // false | ||
checkDepends('others',data); // false | ||
checkDepends({name:'Alaska'},data); // true | ||
checkDepends('name', data); // true | ||
checkDepends('!name', data); // false | ||
checkDepends('others', data); // false | ||
checkDepends({ name: 'Alaska' }, data); // true | ||
checkDepends({ 'name.length': 6 }, data); // true | ||
checkDepends({ name: ':regexp' }, data); // true | ||
checkDepends({from:{ $gt: 2015 }},data); // true | ||
checkDepends({tags:/web/ },data); // true | ||
checkDepends({obj:{} },data); // true | ||
checkDepends({object:{attr:1} },data); // true | ||
checkDepends({numbers:{ $all:[15,20] } },data); // true | ||
checkDepends({ $or: [{name: '/alaska/i'}, { no: true }] },data); // true | ||
checkDepends({ from: { $gt: 2015 } }, data); // true | ||
checkDepends({ tags: /web/ }, data); // true | ||
checkDepends({ obj: {} }, data); // true | ||
checkDepends({ object: { attr: 1, foo: 'bar' } }, data); // true | ||
checkDepends({ 'object.foo': 'bar' }, data); // true | ||
checkDepends({ numbers: { $all: [15, 20] } }, data); // true | ||
checkDepends({ 'tags.length': 2 }, data); // true | ||
checkDepends({ $or: [{ name: '/alaska/i' }, { no: true }] }, data); // true | ||
checkDepends({ $jsonSchema: {} }); //true | ||
checkDepends({ $jsonSchema: { properties: {name:{ type:'number' }}} }); //false | ||
checkDepends({ $jsonSchema: { properties: { name: { type: 'number' } } } }); //false | ||
``` | ||
@@ -82,3 +85,4 @@ | ||
- MongoDB支持JSON Schema draft 4,check-depends 使用AJV验证JSON Schema | ||
- check-depends 中`:`开头的字符串 `:key`,代表data值引用 | ||
- check-depends 中`:`开头的字符串 `:key`,代表data值引用 | ||
- check-depends 支持 string.length 和 array.length 查询 | ||
@@ -85,0 +89,0 @@ 差异举例: |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
105
15807
5
334
1
Updatedajv@^6.6.2