Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions lib/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
* Licensed under the MIT license.
*/

'use strict';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So really this is about making the code run in strict mode. I'm Okay with it, but it should be reflected in the Pull request description. Would live a newline between the comment and the use strict.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I actually didn't think about the strict mode part, I just sort of added it out of reflex. The optimization thing was my bigger motivation. I'll update the description (and add the whitespace).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sg.


var util = require('util');

var errors = module.exports = require('./errors');

function failCheck(ExceptionConstructor, callee, messageFormat, formatArgs) {
messageFormat = messageFormat || '';
var message = util.format.apply(this, [messageFormat].concat(formatArgs));
var message = util.format.apply(util, [messageFormat].concat(formatArgs));
var error = new ExceptionConstructor(message);
Error.captureStackTrace(error, callee);
throw error;
Expand All @@ -23,40 +25,40 @@ function failStateCheck(callee, message, formatArgs) {
failCheck(errors.IllegalStateError, callee, message, formatArgs);
}

module.exports.checkArgument = function(value, message) {
function checkArgument(value, message) {
if (!value) {
failArgumentCheck(arguments.callee, message,
failArgumentCheck(checkArgument, message,
Array.prototype.slice.call(arguments, 2));
}
};
}

module.exports.checkState = function(value, message) {
function checkState(value, message) {
if (!value) {
failStateCheck(arguments.callee, message,
failStateCheck(checkState, message,
Array.prototype.slice.call(arguments, 2));
}
};
}

module.exports.checkIsDef = function(value, message) {
function checkIsDef(value, message) {
if (value !== undefined) {
return value;
}

failArgumentCheck(arguments.callee, message ||
failArgumentCheck(checkIsDef, message ||
'Expected value to be defined but was undefined.',
Array.prototype.slice.call(arguments, 2));
};
}

module.exports.checkIsDefAndNotNull = function(value, message) {
function checkIsDefAndNotNull(value, message) {
// Note that undefined == null.
if (value != null) {
return value;
}

failArgumentCheck(arguments.callee, message ||
failArgumentCheck(checkIsDefAndNotNull, message ||
'Expected value to be defined and not null but got "' +
typeOf(value) + '".', Array.prototype.slice.call(arguments, 2));
};
}

// Fixed version of the typeOf operator which returns 'null' for null values
// and 'array' for arrays.
Expand All @@ -73,22 +75,28 @@ function typeOf(value) {
}

function typeCheck(expect) {
return function(value, message) {
return function typeChecker(value, message) {
var type = typeOf(value);

if (type == expect) {
return value;
}

failArgumentCheck(arguments.callee, message ||
failArgumentCheck(typeChecker, message ||
'Expected "' + expect + '" but got "' + type + '".',
Array.prototype.slice.call(arguments, 2));
};
}

module.exports.checkIsString = typeCheck('string');
module.exports.checkIsArray = typeCheck('array');
module.exports.checkIsNumber = typeCheck('number');
module.exports.checkIsBoolean = typeCheck('boolean');
module.exports.checkIsFunction = typeCheck('function');
module.exports.checkIsObject = typeCheck('object');
module.exports = {
checkArgument: checkArgument,
checkState: checkState,
checkIsDef: checkIsDef,
checkIsDefAndNotNull: checkIsDefAndNotNull,
checkIsString: typeCheck('string'),
checkIsArray: typeCheck('array'),
checkIsNumber: typeCheck('number'),
checkIsBoolean: typeCheck('boolean'),
checkIsFunction: typeCheck('function'),
checkIsObject: typeCheck('object')
};
3 changes: 2 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2012 Mathieu Turcotte
* Licensed under the MIT license.
*/
'use strict';

var util = require('util');

Expand All @@ -22,4 +23,4 @@ util.inherits(IllegalStateError, Error);
IllegalStateError.prototype.name = 'IllegalStateError';

module.exports.IllegalStateError = IllegalStateError;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a diff here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my editor put a new line at the end of the file. I can revert this.

module.exports.IllegalArgumentError = IllegalArgumentError;
module.exports.IllegalArgumentError = IllegalArgumentError;