Comparing version 0.1.5 to 0.1.6
143
lib/type.js
@@ -18,11 +18,23 @@ // Copyright 2012 The Obvious Corporation. | ||
/* | ||
* 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 */ | ||
@@ -37,8 +49,86 @@ var NUMBER = "number"; | ||
/** 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) { | ||
@@ -61,3 +151,3 @@ return x !== undefined; | ||
function isInt(x) { | ||
if (!isNumber(x) || (x !== Math.floor(x))) { | ||
if (!isNumber(x) || (x !== Math.floor(x)) || !isFinite(x)) { | ||
return false; | ||
@@ -86,32 +176,61 @@ } | ||
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); | ||
} | ||
} | ||
@@ -127,2 +246,4 @@ | ||
assertArray: assertArray, | ||
assertBoolean: assertBoolean, | ||
assertBuffer: assertBuffer, | ||
@@ -136,2 +257,4 @@ assertDefined: assertDefined, | ||
assertUndefined: assertUndefined, | ||
isArray: isArray, | ||
isBoolean: isBoolean, | ||
isBuffer: isBuffer, | ||
@@ -138,0 +261,0 @@ isDefined: isDefined, |
{ | ||
"name": "bidar", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"keywords": | ||
@@ -5,0 +5,0 @@ ["object", "serialization", "data", "graph"], |
@@ -40,3 +40,3 @@ Bidar: Binary Data Representation. | ||
in the purely-local case, and you shouldn't necessarily have to | ||
change your code if you decide to interpose a serialization (e.g., a | ||
change your code if you decide to interpose serialization (e.g., a | ||
machine boundary). I have no love of `undefined` myself and wish I | ||
@@ -242,2 +242,5 @@ could do away with it, but I'm not about to try to do so wishfully. | ||
* Dynamic properties (e.g. `{ get foo() {...} }` ) will | ||
cause `serialize()` to throw an exception. | ||
* If there are extra properties on Buffer objects, they get silently | ||
@@ -244,0 +247,0 @@ ignored. E.g.: On serialization, `foo` will get dropped with this |
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
63268
1413
282