不要怂,就是干,撸起袖子干!

Commit 0e09318d by Sascha Depold

generate proper deprecation warning

1 parent ff5db2f1
...@@ -540,7 +540,7 @@ module.exports = (function() { ...@@ -540,7 +540,7 @@ module.exports = (function() {
DAOFactory.prototype.create = function(values, fieldsOrOptions) { DAOFactory.prototype.create = function(values, fieldsOrOptions) {
Utils.validateParameter(values, Object, { optional: true }) Utils.validateParameter(values, Object, { optional: true })
Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true }) Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true, index: 2, method: 'DAOFactory#create' })
if (fieldsOrOptions instanceof Array) { if (fieldsOrOptions instanceof Array) {
fieldsOrOptions = { fields: fieldsOrOptions } fieldsOrOptions = { fields: fieldsOrOptions }
...@@ -643,8 +643,8 @@ module.exports = (function() { ...@@ -643,8 +643,8 @@ module.exports = (function() {
* multiple records * multiple records
*/ */
DAOFactory.prototype.bulkCreate = function(records, fieldsOrOptions, options) { DAOFactory.prototype.bulkCreate = function(records, fieldsOrOptions, options) {
Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true }) Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true, index: 1, method: 'DAOFactory#bulkCreate' })
Utils.validateParameter(options, 'undefined', { deprecated: Object, optional: true }) Utils.validateParameter(options, 'undefined', { deprecated: Object, optional: true, index: 2, method: 'DAOFactory#bulkCreate' })
options = Utils._.extend({ options = Utils._.extend({
validate: false, validate: false,
......
var cJSON = require('circular-json')
var ParameterValidator = module.exports = { var ParameterValidator = module.exports = {
check: function(value, expectation, options) { check: function(value, expectation, options) {
options = Utils._.extend({ options = Utils._.extend({
throwError: true, throwError: true,
deprecated: false, deprecated: false,
deprecationWarning: "Deprecated!", deprecationWarning: generateDeprecationWarning(value, expectation, options),
onDeprecated: function(s){ console.log('DEPRECATION WARNING:', s) }, onDeprecated: function(s) { console.log('DEPRECATION WARNING:', s) },
optional: false index: null,
method: null,
optional: false
}, options || {}) }, options || {})
if (options.optional && ((value === undefined) || (value === null)) ) { if (options.optional && ((value === undefined) || (value === null)) ) {
...@@ -26,6 +30,23 @@ var ParameterValidator = module.exports = { ...@@ -26,6 +30,23 @@ var ParameterValidator = module.exports = {
} }
} }
var generateDeprecationWarning = function(value, expectation, options) {
options = options || {}
if (options.method && options.index) {
return [
'The',
{1:'first',2:'second',3:'third',4:'fourth',5:'fifth'}[options.index],
'parameter of',
options.method,
'should be a',
extractClassName(expectation) + '!'
].join(" ")
} else {
return ["Expected", cJSON.stringify(value), "to be", extractClassName(expectation) + '!'].join(" ")
}
}
var matchesExpectation = function(value, expectation) { var matchesExpectation = function(value, expectation) {
if (typeof expectation === 'string') { if (typeof expectation === 'string') {
return (typeof value === expectation.toString()) return (typeof value === expectation.toString())
...@@ -52,8 +73,18 @@ var validate = function(value, expectation, options) { ...@@ -52,8 +73,18 @@ var validate = function(value, expectation, options) {
return false return false
} else { } else {
var _value = (value === null) ? 'null' : value.toString() var _value = (value === null) ? 'null' : value.toString()
, _expectation = expectation.toString().match(/function ([^\(]+)/)[1] , _expectation = extractClassName(expectation)
throw new Error('The parameter (value: ' + _value + ') is no ' + _expectation + '.') throw new Error('The parameter (value: ' + _value + ') is no ' + _expectation + '.')
} }
} }
var extractClassName = function(o) {
if (typeof o === 'string') {
return o
} else if (!!o) {
return o.toString().match(/function ([^\(]+)/)[1]
} else {
return 'undefined'
}
}
...@@ -46,7 +46,8 @@ ...@@ -46,7 +46,8 @@
"toposort-class": "~0.2.0", "toposort-class": "~0.2.0",
"generic-pool": "2.0.4", "generic-pool": "2.0.4",
"promise": "~3.2.0", "promise": "~3.2.0",
"sql": "~0.31.0" "sql": "~0.31.0",
"circular-json": "~0.1.5"
}, },
"devDependencies": { "devDependencies": {
"sqlite3": "~2.1.12", "sqlite3": "~2.1.12",
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!