occamsrazor-validator
Advanced tools
Comparing version 9.0.0 to 9.0.1
45
index.js
@@ -1,38 +0,37 @@ | ||
var match = require('occamsrazor-match'); | ||
var and = require('occamsrazor-match/extra/and'); | ||
var combineValidators = require('./lib/combine-validators'); | ||
var ValidationResult = require('./lib/validation-result'); | ||
var setName = require('occamsrazor-match/lib/setName'); | ||
var match = require('occamsrazor-match') | ||
var and = require('occamsrazor-match/extra/and') | ||
var combineValidators = require('./lib/combine-validators') | ||
var ValidationResult = require('./lib/validation-result') | ||
var setName = require('occamsrazor-match/lib/setName') | ||
function buildValidator (baseScore, funcs) { | ||
baseScore = baseScore || 0 | ||
funcs = funcs || [] | ||
var innerValidator = and(funcs) | ||
function buildValidator(baseScore, funcs) { | ||
baseScore = baseScore || 0; | ||
funcs = funcs || []; | ||
var innerValidator = and(funcs); | ||
function validator(obj, callback) { | ||
function validator (obj, callback) { | ||
if (innerValidator(obj, callback)) { | ||
return new ValidationResult(funcs.length + baseScore, obj); | ||
return new ValidationResult(funcs.length + baseScore, obj) | ||
} | ||
return null; | ||
return null | ||
}; | ||
validator.score = function () { | ||
return funcs.length + baseScore; | ||
}; | ||
return funcs.length + baseScore | ||
} | ||
validator.match = function (func) { | ||
return buildValidator(baseScore, funcs.concat(match(func))); | ||
}; | ||
return buildValidator(baseScore, funcs.concat(match(func))) | ||
} | ||
validator.important = function (bump) { | ||
bump = bump || 64; | ||
return buildValidator(baseScore + bump, funcs); | ||
}; | ||
bump = bump || 64 | ||
return buildValidator(baseScore + bump, funcs) | ||
} | ||
return setName(validator, innerValidator.name + ' (score: ' + validator.score() + ')'); | ||
return setName(validator, innerValidator.name + ' (score: ' + validator.score() + ')') | ||
} | ||
buildValidator.combine = combineValidators; | ||
buildValidator.combine = combineValidators | ||
module.exports = buildValidator; | ||
module.exports = buildValidator |
// convert an array in form [1,2,3] into a string "ABC" (easily sortable) | ||
module.exports = function array_to_str(score) { | ||
var i, s = ''; // output string | ||
module.exports = function array_to_str (score) { | ||
var i; var s = '' // output string | ||
for (i = 0; i < score.length; i++) { | ||
s += String.fromCharCode(64 + score[i]); | ||
s += String.fromCharCode(64 + score[i]) | ||
} | ||
return s; | ||
}; | ||
return s | ||
} |
@@ -1,35 +0,35 @@ | ||
var ValidationResult = require('./validation-result'); | ||
var setName = require('occamsrazor-match/lib/setName'); | ||
var ValidationResult = require('./validation-result') | ||
var setName = require('occamsrazor-match/lib/setName') | ||
function addValidatorName(validatorName, callback) { | ||
if (!callback) return; | ||
function addValidatorName (validatorName, callback) { | ||
if (!callback) return | ||
return function (obj) { | ||
obj.validatorName = validatorName; | ||
callback(obj); | ||
}; | ||
obj.validatorName = validatorName | ||
callback(obj) | ||
} | ||
} | ||
module.exports = function combineValidators(validators) { | ||
var validatorName = validators.map(function (v) { return v.name; }).join(', '); | ||
var validatorWrapper = function validatorWrapper(args, callback) { | ||
var i, l, current_score, score = [], validator, decoratedCallback; | ||
decoratedCallback = addValidatorName(validatorName, callback); | ||
module.exports = function combineValidators (validators) { | ||
var validatorName = validators.map(function (v) { return v.name }).join(', ') | ||
var validatorWrapper = function validatorWrapper (args, callback) { | ||
var i; var l; var current_score; var score = []; var validator; var decoratedCallback | ||
decoratedCallback = addValidatorName(validatorName, callback) | ||
if (args.length < validators.length) { | ||
decoratedCallback && decoratedCallback({ path: '', name: 'minArguments:' + args.length, value: args }); | ||
return null; | ||
decoratedCallback && decoratedCallback({ path: '', name: 'minArguments:' + args.length, value: args }) | ||
return null | ||
} | ||
for (i = 0, l = validators.length; i < l; i++) { | ||
validator = validators[i]; | ||
decoratedCallback = addValidatorName(validators[i].name, callback); | ||
current_score = validator(args[i], decoratedCallback); | ||
validator = validators[i] | ||
decoratedCallback = addValidatorName(validators[i].name, callback) | ||
current_score = validator(args[i], decoratedCallback) | ||
if (!current_score) { | ||
return null; | ||
return null | ||
} | ||
score.push(current_score.value()); | ||
score.push(current_score.value()) | ||
} | ||
return new ValidationResult(score, args); | ||
}; | ||
return new ValidationResult(score, args) | ||
} | ||
validatorWrapper.validators = validators; | ||
return setName(validatorWrapper, validatorName); | ||
}; | ||
validatorWrapper.validators = validators | ||
return setName(validatorWrapper, validatorName) | ||
} |
@@ -1,6 +0,6 @@ | ||
var arrayToStr = require('./array-to-str'); | ||
var arrayToStr = require('./array-to-str') | ||
function ValidationResult(score) { | ||
this.score = score; | ||
this._key = undefined; | ||
function ValidationResult (score) { | ||
this.score = score | ||
this._key = undefined | ||
} | ||
@@ -10,12 +10,12 @@ | ||
if (this._key) { | ||
return this._key; | ||
return this._key | ||
} | ||
this._key = arrayToStr(Array.isArray(this.score) ? this.score : [this.score]); | ||
return this._key; | ||
}; | ||
this._key = arrayToStr(Array.isArray(this.score) ? this.score : [this.score]) | ||
return this._key | ||
} | ||
ValidationResult.prototype.value = function () { | ||
return this.score; | ||
}; | ||
return this.score | ||
} | ||
module.exports = ValidationResult; | ||
module.exports = ValidationResult |
{ | ||
"name": "occamsrazor-validator", | ||
"version": "9.0.0", | ||
"version": "9.0.1", | ||
"description": "A duck-typing library", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha tests/**/*.js", | ||
"test": "mocha", | ||
"watch": "npm run test -- -w", | ||
"lint": "./node_modules/.bin/eslint --ext .js ./src ./tests", | ||
"release:major": "./node_modules/.bin/npm-release major", | ||
"release:minor": "./node_modules/.bin/npm-release minor", | ||
"release:patch": "./node_modules/.bin/npm-release patch", | ||
"lint": "eslint index.js ./lib ./test --fix", | ||
"precommit": "npm run lint", | ||
@@ -27,7 +24,11 @@ "prepush": "npm run test" | ||
"devDependencies": { | ||
"chai": "^1.10.0", | ||
"eslint": "^1.10.3", | ||
"husky": "^0.10.2", | ||
"mocha": "^2.1.0", | ||
"npm-release": "^1.0.0" | ||
"chai": "^4.2.0", | ||
"eslint": "^6.7.1", | ||
"eslint-config-standard": "^14.1.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-node": "^10.0.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"husky": "^3.1.0", | ||
"mocha": "^6.2.2" | ||
}, | ||
@@ -34,0 +35,0 @@ "dependencies": { |
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
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
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
495
26307
9
15
1