Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A fix and an improvement.

* Infinities aren't ints.
* Report failures more sensibly.
  • Loading branch information...
commit f8e73e25629c2b0ee0e8eda758526158feea7aa9 1 parent 732ca8f
Dan Bornstein authored
Showing with 134 additions and 11 deletions.
  1. +133 −10 lib/type.js
  2. +1 −1  package.json
View
143 lib/type.js
@@ -15,15 +15,27 @@ var assert = require("assert");
/*
- * Exported bindings
+ * Variable definitions
*/
+/** extended type name */
+var ARRAY = "array";
+
/** type name */
var BOOLEAN = "boolean";
+/** extended type name */
+var BUFFER = "buffer";
+
/** type name */
var FUNCTION = "function";
+/** extended type name */
+var INT = "int";
+
+/** extended type name */
+var NULL = "null";
+
/** type name */
var NUMBER = "number";
@@ -33,12 +45,90 @@ var OBJECT = "object";
/** type name */
var STRING = "string";
+/** extended type name */
+var UINT = "uint";
+
/** type name */
var UNDEFINED = "undefined";
+
+/*
+ * Helper functions
+ */
+
+function extendedTypeOf(value) {
+ var type = typeof value;
+
+ switch (type) {
+ case BOOLEAN:
+ case FUNCTION:
+ case STRING:
+ case UNDEFINED: {
+ return type;
+ }
+ case NUMBER: {
+ if (isInt(value)) {
+ return (value > 0) ? UINT : INT;
+ } else {
+ return NUMBER;
+ }
+ }
+ case OBJECT: {
+ if (value === null) {
+ return NULL;
+ } else if (Array.isArray(value)) {
+ return ARRAY;
+ } else if (Buffer.isBuffer(value)) {
+ return BUFFER;
+ } else {
+ return OBJECT;
+ }
+ }
+ }
+}
+
+/**
+ * Somewhat more helpful failure message.
+ */
+function failType(value, expectedTypeName) {
+ var gotType = extendedTypeOf(value);
+ var message = "Expected " + expectedTypeName + "; got " + gotType;
+
+ switch (gotType) {
+ case BOOLEAN:
+ case INT:
+ case NUMBER:
+ case UINT: {
+ message += " (" + value + ")";
+ break;
+ }
+ case FUNCTION: {
+ if (value.name) {
+ message += " (" + value.name + ")";
+ }
+ break;
+ }
+ }
+
+ assert.fail(gotType, expectedTypeName, message, "!==");
+}
+
+
+
+/*
+ * Exported bindings
+ */
+
+// For symmetry.
+var isArray = Array.isArray;
+
// For symmetry.
var isBuffer = Buffer.isBuffer;
+function isBoolean(x) {
+ return (x === true) || (x === false);
+}
+
function isDefined(x) {
return x !== undefined;
}
@@ -56,7 +146,7 @@ function isNumber(x) {
}
function isInt(x) {
- if (!isNumber(x) || (x !== Math.floor(x))) {
+ if (!isNumber(x) || (x !== Math.floor(x)) || !isFinite(x)) {
return false;
}
@@ -80,36 +170,65 @@ function isFunction(x) {
return (typeof x) === FUNCTION;
}
+function assertArray(x) {
+ if (!isArray(x)) {
+ failType(x, ARRAY);
+ }
+}
+
+function assertBoolean(x) {
+ if (!isBoolean(x)) {
+ failType(x, BOOLEAN);
+ }
+}
+
function assertBuffer(x) {
- assert(isBuffer(x));
+ if (!isBuffer(x)) {
+ failType(x, BUFFER);
+ }
}
function assertDefined(x) {
- assert(isDefined(x));
+ if (!isDefined(x)) {
+ assert.fail("undefined", "(anything else)",
+ "Expected defined value", "!==");
+ }
}
function assertUndefined(x) {
- assert(isUndefined(x));
+ if (!isUndefined(x)) {
+ failType(x, UNDEFINED);
+ }
}
function assertInt(x) {
- assert(isInt(x));
+ if (!isInt(x)) {
+ failType(x, INT);
+ }
}
function assertNumber(x) {
- assert(isNumber(x));
+ if (!isNumber(x)) {
+ failType(x, NUMBER);
+ }
}
function assertString(x) {
- assert(isString(x));
+ if (!isString(x)) {
+ failType(x, STRING);
+ }
}
function assertUint(x) {
- assert(isUint(x));
+ if (!isUint(x)) {
+ failType(x, UINT);
+ }
}
function assertFunction(x) {
- assert(isFunction(x));
+ if (!isFunction(x)) {
+ failType(x, FUNCTION);
+ }
}
module.exports = {
@@ -120,6 +239,8 @@ module.exports = {
STRING: STRING,
UNDEFINED: UNDEFINED,
+ assertArray: assertArray,
+ assertBoolean: assertBoolean,
assertBuffer: assertBuffer,
assertDefined: assertDefined,
assertFunction: assertFunction,
@@ -128,6 +249,8 @@ module.exports = {
assertString: assertString,
assertUint: assertUint,
assertUndefined: assertUndefined,
+ isArray: isArray,
+ isBoolean: isBoolean,
isBuffer: isBuffer,
isDefined: isDefined,
isInt: isInt,
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "bidar",
- "version": "0.1.5",
+ "version": "0.1.6",
"keywords":
["object", "serialization", "data", "graph"],
"description":
Please sign in to comment.
Something went wrong with that request. Please try again.