lazy-assert
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "lazy-assert", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "An way of doing assertion for lazy people ...", | ||
@@ -15,3 +15,4 @@ "main": "src/index.js", | ||
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha src/**/__test/*.test.js", | ||
"test-lazy": "./node_modules/.bin/mocha src/**/__test/*.test.js" | ||
"test-lazy": "./node_modules/.bin/mocha src/**/__test/*.test.js", | ||
"open-cover": "open ./coverage/lcov-report/index.html" | ||
}, | ||
@@ -18,0 +19,0 @@ "author": "Liang", |
@@ -0,1 +1,3 @@ | ||
![travis build](https://travis-ci.org/be-fe/lazy-assert.svg?branch=master) | ||
# 什么是 lazy-assert | ||
@@ -2,0 +4,0 @@ |
@@ -9,19 +9,27 @@ var lazy = require('../index'); | ||
it('Test simple value', function () { | ||
lazy.peek('string-value', 'a string'); | ||
lazy.peek('number-value', 123); | ||
lazy.peek('float-number-value', -0.123); | ||
lazy.peek('boolean-value', true); | ||
lazy.peek('null-value', null); | ||
lazy.peek('undefined-value', undefined); | ||
lazy.peek('1-simple-value', { | ||
'string': 'string', | ||
'number': 123, | ||
'float': -0.123, | ||
'NaN': Number('not-a-number'), | ||
'Infinity': Infinity, | ||
'-Infinity': -1 * Infinity, | ||
'boolean': false, | ||
'null': null, | ||
'undefined': undefined, | ||
}); | ||
}); | ||
it('Test complex value', function () { | ||
lazy.peek('function-value', function () { | ||
lazy.peek('2-complex-value', { | ||
'functon': function () {}, | ||
'array': [1, 2, 3], | ||
'object': {hello: 'world'}, | ||
'regexp': /abc/ | ||
}); | ||
lazy.peek('array-value', [1, 2, 3]); | ||
lazy.peek('object-value', {hello: 'world'}); | ||
}); | ||
it('Test complex value : deep object', function () { | ||
lazy.peek('complex-object', { | ||
it('Test complex value : really deep object', function () { | ||
lazy.peek('3-complex-deep-object', { | ||
a: {b: {c: {d: {e: {f: {g: 0}}}}}}, | ||
hello: [1, 2, 3, { | ||
@@ -37,4 +45,4 @@ world: '!' | ||
it('Test complex value : really deep object', function () { | ||
lazy.peek('complex-deep-object', { | ||
it('Test complex value : really deep object, with depth = 6', function () { | ||
lazy.peek('4-complex-deep-object-6', { | ||
a: {b: {c: {d: {e: {f: {g: 0}}}}}}, | ||
@@ -48,7 +56,7 @@ hello: [1, 2, 3, { | ||
aString: 'a string' | ||
}); | ||
}, 6); | ||
}); | ||
it('Test complex value : really deep object, with depth = 6', function () { | ||
lazy.peek('complex-deep-object-6', { | ||
it('Test complex value : really deep object, with depth = -1', function () { | ||
lazy.peek('5-complex-deep-object-full', { | ||
a: {b: {c: {d: {e: {f: {g: 0}}}}}}, | ||
@@ -62,8 +70,7 @@ hello: [1, 2, 3, { | ||
aString: 'a string' | ||
}, 6); | ||
}, -1); | ||
}); | ||
it('Test complex value : really deep object, with depth = -1', function () { | ||
lazy.peek('complex-deep-object-full', { | ||
a: {b: {c: {d: {e: {f: {g: 0}}}}}}, | ||
it('Test complex value : deep object', function () { | ||
lazy.peek('6-complex-object', { | ||
hello: [1, 2, 3, { | ||
@@ -76,4 +83,4 @@ world: '!' | ||
aString: 'a string' | ||
}, -1); | ||
}); | ||
}); | ||
}); |
@@ -5,5 +5,70 @@ var utils = require('./utils'); | ||
load: function (lazyAssert) { | ||
var inner = { | ||
patternRegExp: function (target, pattern) { | ||
if (typeof target === 'string' | ||
|| typeof target === 'number') { | ||
if (pattern.test(String(target))) { | ||
return {result: 'ok', pattern: pattern.toString()}; | ||
} | ||
else { | ||
return {fail: target, pattern: pattern.toString()}; | ||
} | ||
} | ||
else { | ||
return {fail: target, pattern: pattern.toString()}; | ||
} | ||
}, | ||
// @start-def: lazyAssert: {} | ||
utils.extend(lazyAssert, { | ||
patternFunction: function (target, key, pattern) { | ||
var check = pattern(target, key); | ||
if (check) { | ||
return 'ok'; | ||
} | ||
else { | ||
return {fail: target}; | ||
} | ||
}, | ||
patternArray: function (array, pattern) { | ||
var result = []; | ||
for (var i = 0; i < array.length; i++) { | ||
if (pattern instanceof RegExp) { | ||
result.push(inner.patternRegExp(array[i], pattern)); | ||
} | ||
else if (typeof pattern === 'function') { | ||
result.push(inner.patternFunction(array[i], i, pattern)); | ||
} | ||
else { | ||
// 只能是 object 了 | ||
result.push(inner.patternObject(array[i], pattern)); | ||
} | ||
} | ||
return result; | ||
}, | ||
patternObject: function (object, pattern) { | ||
var result = {}; | ||
if (typeof object !== 'object') { | ||
return {fail: object, message: 'non-object target is not supported for object pattern'}; | ||
} | ||
for (var key in pattern) { | ||
if (pattern.hasOwnProperty(key)) { | ||
if (typeof subPattern === 'function') { | ||
// match function | ||
result[key] = inner.patternFunction(object[key], key, pattern); | ||
} | ||
else { | ||
// regexp | ||
result[key] = inner.patternRegExp(object[key], pattern[key]); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
}; | ||
var lazyUtils = { | ||
pick: function (value, configArray) { | ||
@@ -121,5 +186,131 @@ if (value instanceof Array) { | ||
} | ||
}, | ||
/** | ||
* | ||
* @jsdef: target, pattern => result | ||
* | ||
* /// | ||
* 场景: | ||
* | ||
* target, pattern | ||
* | ||
* {key: any}, {key: func | regexp} | ||
* 每一个对应的 pattern.key 会用于匹配 target.key | ||
* | ||
* {key: any}, func | ||
* func 会用于匹配所有的 target 里的属性 | ||
* | ||
* {}, regexp | ||
* 不支持 | ||
* | ||
* [{key}], func | ||
* func 会用于匹配所有的 target {key} | ||
* | ||
* [{key}], regexp | ||
* 不支持 | ||
* | ||
* [{key}], {key} | ||
* key 对应地检查 | ||
* | ||
* [other], {} | ||
* 不支持 | ||
* | ||
* [other], func | regexp | ||
* pattern 匹配 target 每一个 item | ||
* | ||
* other, {} | ||
* 不支持 | ||
* | ||
* other, func | regexp | ||
* pattern 匹配 target | ||
* /// | ||
* target: {} | [{}] | [other] | other | ||
* other: not a {} nor a [] | ||
* | ||
* /// | ||
* 如果是 func, 则必须返回 true, 才认为是正确的, | ||
* | ||
* 如果是 regexp 则 .test() 必须为 true | ||
* 且如果检查对象非string, 或number, 则直接报错 | ||
* | ||
* 如果检验值为 false, 则还需要把出错的值打印出来 | ||
* /// | ||
* pattern: object | func | regexp | ||
* object: {key: func | regexp} | ||
* func: val, key => boolean | ||
* regexp: RegExp | ||
* | ||
* result: string | ||
*/ | ||
pattern: function (target, pattern, key) { | ||
if (target instanceof Array) { | ||
return inner.patternArray(target, pattern); | ||
} | ||
else if (typeof pattern === 'function') { | ||
return inner.patternFunction(target, key, pattern); | ||
} | ||
else if (pattern instanceof RegExp) { | ||
return inner.patternRegExp(target, pattern); | ||
} | ||
else if (typeof pattern === 'object') { | ||
return inner.patternObject(target, pattern); | ||
} | ||
}, | ||
fullPattern: function (complexTarget, patternSet) { | ||
if ( | ||
!(complexTarget instanceof Array && patternSet instanceof Array) | ||
&& !(typeof complexTarget === 'object' && typeof patternSet === 'object' && !(complexTarget instanceof Array) && !(patternSet instanceof Array)) | ||
) { | ||
return 'fail: target & pattern set mismatch'; | ||
} | ||
else if (patternSet instanceof Array) { | ||
var result = []; | ||
for (var i = 0; i < patternSet.length; i++) { | ||
if (i >= complexTarget.length) { | ||
result[i] = 'fail: target not found'; | ||
} | ||
else { | ||
result[i] = lazyUtils.pattern(complexTarget[i], patternSet[i], i); | ||
} | ||
} | ||
if (i < complexTarget.length) { | ||
result[i] = 'fail: target has more items than pattern'; | ||
} | ||
return result; | ||
} | ||
else { | ||
var result = {}; | ||
var allTargetKeys = {}; | ||
for (var key in complexTarget) { | ||
allTargetKeys[key] = 1; | ||
} | ||
for (var key in patternSet) { | ||
if (key in complexTarget) { | ||
result[key] = lazyUtils.pattern(complexTarget[key], patternSet[key], key); | ||
delete allTargetKeys[key]; | ||
} | ||
else { | ||
result[key] = 'fail: target not found'; | ||
} | ||
} | ||
for (var key in allTargetKeys) { | ||
result[key] = 'fail: pattern not found'; | ||
} | ||
return result; | ||
} | ||
} | ||
}); | ||
}; | ||
// @start-def: lazyAssert: {} | ||
utils.extend(lazyAssert, lazyUtils); | ||
} | ||
}; |
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
379518
2122
129
140