Comparing version 0.5.3 to 0.5.4
400
lib/typ.js
@@ -104,3 +104,3 @@ // Copyright 2012 The Obvious Corporation. | ||
function baseToString(value) { | ||
return objectToString.call(value); | ||
return objectToString.call(value); | ||
} | ||
@@ -110,38 +110,38 @@ | ||
function extendedTypeOf(value) { | ||
var type = typeof 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 (isArray(value)) { | ||
return ARRAY; | ||
} else if (isBuffer(value)) { | ||
return BUFFER; | ||
} else if (isDate(value)) { | ||
return DATE; | ||
} else if (isError(value)) { | ||
return ERROR; | ||
} else if (isRegExp(value)) { | ||
return REGEXP; | ||
} else if (isMap(value)) { | ||
return MAP; | ||
} else { | ||
return OBJECT; | ||
} | ||
} | ||
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 (isArray(value)) { | ||
return ARRAY; | ||
} else if (isBuffer(value)) { | ||
return BUFFER; | ||
} else if (isDate(value)) { | ||
return DATE; | ||
} else if (isError(value)) { | ||
return ERROR; | ||
} else if (isRegExp(value)) { | ||
return REGEXP; | ||
} else if (isMap(value)) { | ||
return MAP; | ||
} else { | ||
return OBJECT; | ||
} | ||
} | ||
} | ||
} | ||
@@ -153,30 +153,30 @@ | ||
function failType(value, expectedTypeName) { | ||
var gotType = extendedTypeOf(value); | ||
var message = "Expected " + expectedTypeName + "; got " + gotType; | ||
var gotType = extendedTypeOf(value); | ||
var message = "Expected " + expectedTypeName + "; got " + gotType; | ||
switch (gotType) { | ||
case BOOLEAN: | ||
case DATE: | ||
case INT: | ||
case NUMBER: | ||
case REGEXP: | ||
case UINT: { | ||
message += " (" + value + ")"; | ||
break; | ||
} | ||
case ERROR: { | ||
if (value.message) { | ||
message += " (" + value.message + ")"; | ||
} | ||
break; | ||
} | ||
case FUNCTION: { | ||
if (value.name) { | ||
message += " (" + value.name + ")"; | ||
} | ||
break; | ||
} | ||
switch (gotType) { | ||
case BOOLEAN: | ||
case DATE: | ||
case INT: | ||
case NUMBER: | ||
case REGEXP: | ||
case UINT: { | ||
message += " (" + value + ")"; | ||
break; | ||
} | ||
case ERROR: { | ||
if (value.message) { | ||
message += " (" + value.message + ")"; | ||
} | ||
break; | ||
} | ||
case FUNCTION: { | ||
if (value.name) { | ||
message += " (" + value.name + ")"; | ||
} | ||
break; | ||
} | ||
} | ||
assert.fail(gotType, expectedTypeName, message, "!=="); | ||
assert.fail(gotType, expectedTypeName, message, "!=="); | ||
} | ||
@@ -194,3 +194,3 @@ | ||
function hasDefaultPrototype(obj) { | ||
return Object.getPrototypeOf(obj) === OBJECT_PROTOTYPE; | ||
return Object.getPrototypeOf(obj) === OBJECT_PROTOTYPE; | ||
} | ||
@@ -202,3 +202,3 @@ | ||
function hasOwnProperty(obj, name) { | ||
return HAS_OWN_PROPERTY_FUNC.call(obj, name); | ||
return HAS_OWN_PROPERTY_FUNC.call(obj, name); | ||
} | ||
@@ -210,3 +210,3 @@ | ||
function isBoolean(x) { | ||
return (x === true) || (x === false); | ||
return (x === true) || (x === false); | ||
} | ||
@@ -218,40 +218,40 @@ | ||
function isDate(x) { | ||
return isObject(x) && | ||
(Object.getPrototypeOf(x) === DATE_PROTOTYPE) && | ||
(baseToString(x) === '[object Date]'); | ||
return isObject(x) && | ||
(Object.getPrototypeOf(x) === DATE_PROTOTYPE) && | ||
(baseToString(x) === '[object Date]'); | ||
} | ||
function isDefined(x) { | ||
// `(void 0)` is a particularly safe way to say `undefined`. | ||
return x !== (void 0); | ||
// `(void 0)` is a particularly safe way to say `undefined`. | ||
return x !== (void 0); | ||
} | ||
function isUndefined(x) { | ||
return x === (void 0); | ||
return x === (void 0); | ||
} | ||
function isError(x) { | ||
return isObject(x) && | ||
(baseToString(x) === '[object Error]') && | ||
hasErrorProto(x); | ||
return isObject(x) && | ||
(baseToString(x) === '[object Error]') && | ||
hasErrorProto(x); | ||
function hasErrorProto(obj) { | ||
while (obj && (obj !== OBJECT_PROTOTYPE)) { | ||
if (obj === ERROR_PROTOTYPE) { | ||
return true; | ||
} | ||
obj = Object.getPrototypeOf(obj); | ||
} | ||
return false; | ||
function hasErrorProto(obj) { | ||
while (obj && (obj !== OBJECT_PROTOTYPE)) { | ||
if (obj === ERROR_PROTOTYPE) { | ||
return true; | ||
} | ||
obj = Object.getPrototypeOf(obj); | ||
} | ||
return false; | ||
} | ||
} | ||
function isRegExp(x) { | ||
return isObject(x) && | ||
(Object.getPrototypeOf(x) === REGEXP_PROTOTYPE) && | ||
(baseToString(x) === '[object RegExp]'); | ||
return isObject(x) && | ||
(Object.getPrototypeOf(x) === REGEXP_PROTOTYPE) && | ||
(baseToString(x) === '[object RegExp]'); | ||
} | ||
function isString(x) { | ||
return (typeof x) === STRING; | ||
return (typeof x) === STRING; | ||
} | ||
@@ -264,192 +264,192 @@ | ||
function isMap(x) { | ||
if (!isObject(x)) { | ||
return false; | ||
} | ||
if (!isObject(x)) { | ||
return false; | ||
} | ||
if (Object.getPrototypeOf(x) !== OBJECT_PROTOTYPE) { | ||
return false; | ||
} | ||
if (Object.getPrototypeOf(x) !== OBJECT_PROTOTYPE) { | ||
return false; | ||
} | ||
var keys = Object.keys(x); | ||
for (var i = 0; i < keys.length; i++) { | ||
var props = Object.getOwnPropertyDescriptor(x, keys[i]); | ||
if (props.get || props.set) { | ||
return false; | ||
} | ||
var keys = Object.keys(x); | ||
for (var i = 0; i < keys.length; i++) { | ||
var props = Object.getOwnPropertyDescriptor(x, keys[i]); | ||
if (props.get || props.set) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
return true; | ||
} | ||
function isNumber(x) { | ||
return (typeof x) === NUMBER; | ||
return (typeof x) === NUMBER; | ||
} | ||
function isObject(x) { | ||
var type = typeof x; | ||
return ((type === OBJECT) || (type === FUNCTION)) && (x !== null); | ||
var type = typeof x; | ||
return ((type === OBJECT) || (type === FUNCTION)) && (x !== null); | ||
} | ||
function isInt(x) { | ||
if (!isNumber(x) || (x !== Math.floor(x)) || !isFinite(x)) { | ||
return false; | ||
} | ||
if (!isNumber(x) || (x !== Math.floor(x)) || !isFinite(x)) { | ||
return false; | ||
} | ||
if (x !== 0) { | ||
return true; | ||
} | ||
if (x !== 0) { | ||
return true; | ||
} | ||
// Zero is special. We don't count "-0" as an int, but you can't | ||
// just test that with === because === doesn't distinguish positive | ||
// and negative zeroes. However, we can use it as a divisor to see | ||
// its effect. | ||
// Zero is special. We don't count "-0" as an int, but you can't | ||
// just test that with === because === doesn't distinguish positive | ||
// and negative zeroes. However, we can use it as a divisor to see | ||
// its effect. | ||
return (1/x) === Infinity; | ||
return (1/x) === Infinity; | ||
} | ||
function isUint(x) { | ||
return isInt(x) && (x >= 0); | ||
return isInt(x) && (x >= 0); | ||
} | ||
function isFunction(x) { | ||
return (typeof x) === FUNCTION; | ||
return (typeof x) === FUNCTION; | ||
} | ||
function assertArray(x) { | ||
if (!isArray(x)) { | ||
failType(x, ARRAY); | ||
} | ||
if (!isArray(x)) { | ||
failType(x, ARRAY); | ||
} | ||
} | ||
function assertBoolean(x) { | ||
if (!isBoolean(x)) { | ||
failType(x, BOOLEAN); | ||
} | ||
if (!isBoolean(x)) { | ||
failType(x, BOOLEAN); | ||
} | ||
} | ||
function assertBuffer(x) { | ||
if (!isBuffer(x)) { | ||
failType(x, BUFFER); | ||
} | ||
if (!isBuffer(x)) { | ||
failType(x, BUFFER); | ||
} | ||
} | ||
function assertDate(x) { | ||
if (!isDate(x)) { | ||
failType(x, DATE); | ||
} | ||
if (!isDate(x)) { | ||
failType(x, DATE); | ||
} | ||
} | ||
function assertDefined(x) { | ||
if (!isDefined(x)) { | ||
assert.fail("undefined", "(anything else)", | ||
"Expected defined value", "!=="); | ||
} | ||
if (!isDefined(x)) { | ||
assert.fail("undefined", "(anything else)", | ||
"Expected defined value", "!=="); | ||
} | ||
} | ||
function assertUndefined(x) { | ||
if (!isUndefined(x)) { | ||
failType(x, UNDEFINED); | ||
} | ||
if (!isUndefined(x)) { | ||
failType(x, UNDEFINED); | ||
} | ||
} | ||
function assertError(x) { | ||
if (!isError(x)) { | ||
failType(x, ERROR); | ||
} | ||
if (!isError(x)) { | ||
failType(x, ERROR); | ||
} | ||
} | ||
function assertInt(x) { | ||
if (!isInt(x)) { | ||
failType(x, INT); | ||
} | ||
if (!isInt(x)) { | ||
failType(x, INT); | ||
} | ||
} | ||
function assertNumber(x) { | ||
if (!isNumber(x)) { | ||
failType(x, NUMBER); | ||
} | ||
if (!isNumber(x)) { | ||
failType(x, NUMBER); | ||
} | ||
} | ||
function assertMap(x) { | ||
if (!isMap(x)) { | ||
failType(x, MAP); | ||
} | ||
if (!isMap(x)) { | ||
failType(x, MAP); | ||
} | ||
} | ||
function assertObject(x) { | ||
if (!isObject(x)) { | ||
failType(x, OBJECT); | ||
} | ||
if (!isObject(x)) { | ||
failType(x, OBJECT); | ||
} | ||
} | ||
function assertRegExp(x) { | ||
if (!isRegExp(x)) { | ||
failType(x, REGEXP); | ||
} | ||
if (!isRegExp(x)) { | ||
failType(x, REGEXP); | ||
} | ||
} | ||
function assertString(x) { | ||
if (!isString(x)) { | ||
failType(x, STRING); | ||
} | ||
if (!isString(x)) { | ||
failType(x, STRING); | ||
} | ||
} | ||
function assertUint(x) { | ||
if (!isUint(x)) { | ||
failType(x, UINT); | ||
} | ||
if (!isUint(x)) { | ||
failType(x, UINT); | ||
} | ||
} | ||
function assertFunction(x) { | ||
if (!isFunction(x)) { | ||
failType(x, FUNCTION); | ||
} | ||
if (!isFunction(x)) { | ||
failType(x, FUNCTION); | ||
} | ||
} | ||
module.exports = { | ||
BOOLEAN: BOOLEAN, | ||
FUNCTION: FUNCTION, | ||
NUMBER: NUMBER, | ||
OBJECT: OBJECT, | ||
STRING: STRING, | ||
UNDEFINED: UNDEFINED, | ||
BOOLEAN: BOOLEAN, | ||
FUNCTION: FUNCTION, | ||
NUMBER: NUMBER, | ||
OBJECT: OBJECT, | ||
STRING: STRING, | ||
UNDEFINED: UNDEFINED, | ||
ARRAY_PROTOTYPE: ARRAY_PROTOTYPE, | ||
FUNCTION_PROTOTYPE: FUNCTION_PROTOTYPE, | ||
OBJECT_PROTOTYPE: OBJECT_PROTOTYPE, | ||
ARRAY_PROTOTYPE: ARRAY_PROTOTYPE, | ||
FUNCTION_PROTOTYPE: FUNCTION_PROTOTYPE, | ||
OBJECT_PROTOTYPE: OBJECT_PROTOTYPE, | ||
assertArray: assertArray, | ||
assertBoolean: assertBoolean, | ||
assertBuffer: assertBuffer, | ||
assertDate: assertDate, | ||
assertDefined: assertDefined, | ||
assertError: assertError, | ||
assertFunction: assertFunction, | ||
assertInt: assertInt, | ||
assertMap: assertMap, | ||
assertNumber: assertNumber, | ||
assertObject: assertObject, | ||
assertRegExp: assertRegExp, | ||
assertString: assertString, | ||
assertUint: assertUint, | ||
assertUndefined: assertUndefined, | ||
assertArray: assertArray, | ||
assertBoolean: assertBoolean, | ||
assertBuffer: assertBuffer, | ||
assertDate: assertDate, | ||
assertDefined: assertDefined, | ||
assertError: assertError, | ||
assertFunction: assertFunction, | ||
assertInt: assertInt, | ||
assertMap: assertMap, | ||
assertNumber: assertNumber, | ||
assertObject: assertObject, | ||
assertRegExp: assertRegExp, | ||
assertString: assertString, | ||
assertUint: assertUint, | ||
assertUndefined: assertUndefined, | ||
hasDefaultPrototype: hasDefaultPrototype, | ||
hasOwnProperty: hasOwnProperty, | ||
hasDefaultPrototype: hasDefaultPrototype, | ||
hasOwnProperty: hasOwnProperty, | ||
isArray: isArray, | ||
isBoolean: isBoolean, | ||
isBuffer: isBuffer, | ||
isDate: isDate, | ||
isDefined: isDefined, | ||
isError: isError, | ||
isInt: isInt, | ||
isFunction: isFunction, | ||
isMap: isMap, | ||
isNumber: isNumber, | ||
isObject: isObject, | ||
isRegExp: isRegExp, | ||
isString: isString, | ||
isUint: isUint, | ||
isUndefined: isUndefined | ||
isArray: isArray, | ||
isBoolean: isBoolean, | ||
isBuffer: isBuffer, | ||
isDate: isDate, | ||
isDefined: isDefined, | ||
isError: isError, | ||
isInt: isInt, | ||
isFunction: isFunction, | ||
isMap: isMap, | ||
isNumber: isNumber, | ||
isObject: isObject, | ||
isRegExp: isRegExp, | ||
isString: isString, | ||
isUint: isUint, | ||
isUndefined: isUndefined | ||
}; |
{ | ||
"name": "typ", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"keywords": | ||
@@ -5,0 +5,0 @@ ["type", "assert", "predicate"], |
@@ -1,8 +0,11 @@ | ||
type: Type predicates and assertions for Node | ||
============================================= | ||
typ: Type predicates and assertions for Node | ||
============================================ | ||
This Node module is meant to provide a unified place to ask and | ||
assert about all the built-in JavaScript and Node types. | ||
assert about all the built-in JavaScript and core Node types. | ||
Further documentation coming soon. | ||
"Typ" is German for "type". Also, as of this writing, both "type" and | ||
"types" were taken in the npm module registry. The name was picked to | ||
be both memorable and short, the latter in order to encourage it to be | ||
used liberally. | ||
@@ -89,3 +92,6 @@ | ||
type `name`, the predicate is called `isName()` and the assertion is | ||
called `assertName()`. | ||
called `assertName()`. A predicate simply returns a boolean indicating | ||
whether or not its value argument is of the appropriate type, and an | ||
assertion does nothing other than throw a descriptive message if its | ||
value argument is not of the expected type. | ||
@@ -98,3 +104,3 @@ The following run-down indicates the meaning of the various types, as | ||
### array: isArray, assertArray | ||
### array: isArray(value), assertArray(value) | ||
@@ -106,3 +112,3 @@ Implies: object | ||
### boolean: isBoolean, assertBoolean | ||
### boolean: isBoolean(value), assertBoolean(value) | ||
@@ -112,3 +118,3 @@ The only two booleans are `true` and `false`. Notably, `Boolean` objects | ||
### buffer: isBuffer, assertBuffer | ||
### buffer: isBuffer(value), assertBuffer(value) | ||
@@ -121,3 +127,3 @@ Implies: object | ||
### date: isDate, assertDate | ||
### date: isDate(value), assertDate(value) | ||
@@ -129,7 +135,7 @@ Implies: object | ||
### defined: isDefined, assertDefined | ||
### defined: isDefined(value), assertDefined(value) | ||
All values other than `undefined` are `defined`. | ||
### error: isError, assertError | ||
### error: isError(value), assertError(value) | ||
@@ -142,3 +148,3 @@ Implies: object | ||
### function: isFunction, assertFunction | ||
### function: isFunction(value), assertFunction(value) | ||
@@ -151,3 +157,3 @@ Implies: object | ||
### int: isInt, assertInt | ||
### int: isInt(value), assertInt(value) | ||
@@ -169,3 +175,3 @@ Implies: number | ||
### map: isMap, assertMap | ||
### map: isMap(value), assertMap(value) | ||
@@ -180,3 +186,3 @@ Implies: object | ||
### number: isNumber, assertNumber | ||
### number: isNumber(value), assertNumber(value) | ||
@@ -193,3 +199,3 @@ A number is, well, a numeric value. Numbers are what result from | ||
### object: isObject, assertObject | ||
### object: isObject(value), assertObject(value) | ||
@@ -200,3 +206,3 @@ An object is an arbitrary mapping of string keys to values. They | ||
### regexp: isRegExp, assertRegExp | ||
### regexp: isRegExp(value), assertRegExp(value) | ||
@@ -209,3 +215,3 @@ Implies: object | ||
### string: isString, assertString | ||
### string: isString(value), assertString(value) | ||
@@ -218,3 +224,3 @@ A string is an ordered sequence of characters. They can be created | ||
### uint: isUint, assertUint | ||
### uint: isUint(value), assertUint(value) | ||
@@ -227,7 +233,9 @@ Implies: int, number | ||
### undefined: isUndefined, assertUndefined | ||
### undefined: isUndefined(value), assertUndefined(value) | ||
The only value that is undefined is `undefined`. | ||
Notably, `null` is defined, *not* undefined. | ||
Miscellaneous Functions | ||
@@ -262,3 +270,3 @@ ----------------------- | ||
Questions, comments, bug reports, and pull requests are all welcome. | ||
Submit them at [the project on GitHub](https://github.com/Obvious/leb/). | ||
Submit them at [the project on GitHub](https://github.com/Obvious/typ/). | ||
@@ -277,2 +285,5 @@ Bug reports that include steps-to-reproduce (including code) are the | ||
Thanks to [Jeremy Stanley](https://github.com/azulus) | ||
and [Dan Pupius](https://github.com/dpup) | ||
for suggestions. | ||
@@ -279,0 +290,0 @@ License |
@@ -68,5 +68,11 @@ // Copyright 2012 The Obvious Corporation. | ||
p: [typ.isFunction, typ.isObject, typ.isDefined] }, | ||
{ v: new Function("return 5"), | ||
p: [typ.isFunction, typ.isObject, typ.isDefined] }, | ||
// maps | ||
{ v: {}, p: [typ.isMap, typ.isObject, typ.isDefined] }, | ||
{ v: { a: 10, b: 20 }, p: [typ.isMap, typ.isObject, typ.isDefined] }, | ||
// non-map objects | ||
{ v: new events.EventEmitter(), p: [typ.isObject, typ.isDefined] }, | ||
{ v: { get a() { return 5; } }, p: [typ.isObject, typ.isDefined] }, | ||
{ v: { set b(n) { } }, p: [typ.isObject, typ.isDefined] }, | ||
// numbers | ||
@@ -83,11 +89,14 @@ { v: 0, p: [typ.isInt, typ.isUint, typ.isNumber, typ.isDefined] }, | ||
{ v: NaN, p: [typ.isNumber, typ.isDefined] }, // somewhat ironic | ||
{ v: -1e-1000, p: [typ.isNumber, typ.isDefined] }, | ||
{ v: -1e-1000, p: [typ.isNumber, typ.isDefined] }, // !isInt() | ||
// regexps | ||
{ v: /frobozz/, p: [typ.isRegExp, typ.isObject, typ.isDefined] }, | ||
{ v: new RegExp("fizmo"), p: [typ.isRegExp, typ.isObject, typ.isDefined] }, | ||
// strings | ||
{ v: "", p: [typ.isString, typ.isDefined] }, | ||
{ v: "blort", p: [typ.isString, typ.isDefined] }, | ||
// notable edge cases | ||
{ v: new Number(5), p: [typ.isObject, typ.isDefined] }, // !isNumber() | ||
{ v: new String("fuzz"), p: [typ.isObject, typ.isDefined] }, // !isString() | ||
{ v: new Boolean(true), p: [typ.isObject, typ.isDefined] }, // !isBoolean() | ||
// other | ||
{ v: new events.EventEmitter(), p: [typ.isObject, typ.isDefined] }, | ||
{ v: { get a() { return 5; } }, p: [typ.isObject, typ.isDefined] }, | ||
{ v: null, p: [typ.isDefined] }, | ||
@@ -94,0 +103,0 @@ { v: undefined, p: [typ.isUndefined] } |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
33611
534
281
1