Comparing version 0.0.3 to 0.0.4
105
jstype.js
@@ -1,2 +0,2 @@ | ||
(function (root, name) { | ||
(function(root, name) { | ||
var node = typeof exports === 'object', | ||
@@ -9,4 +9,3 @@ mod = node ? module : { | ||
define(name, module.exports); | ||
} | ||
else if (!node) { | ||
} else if (!node) { | ||
root[name] = mod.exports; | ||
@@ -16,30 +15,7 @@ } | ||
function factory(module, exports) { | ||
/****************************************************************************** | ||
* Type [object] | ||
* | ||
* an enumeration of native Object Class constructors mapped to their string | ||
* equivalents, e.g. Object => 'object', String => 'string', etc. used | ||
* internally but exposed for general usefulness in specifying types | ||
* | ||
* usage example: | ||
* var stringType = Type[String]; | ||
* | ||
***********************************/ | ||
var Type = exports.Type = {}; | ||
Type[Object] = 'object'; | ||
Type[String] = 'string'; | ||
Type[Array] = 'array'; | ||
Type[Date] = 'date'; | ||
Type[Number] = 'number'; | ||
Type[RegExp] = 'regexp'; | ||
Type[Function] = 'function'; | ||
Type[Boolean] = 'boolean'; | ||
var customTypes = []; | ||
var customConstructors = []; | ||
var reservedTypes = ["undefined", "null", "infinity", "nan", "date", "regexp", "array", "integer", "float", "string", "function", "boolean", "object", "arguments"]; | ||
var reservedTypes = ["number", "undefined", "null", "infinity", "nan", "date", "regexp", "array", "integer", "float", "string", "function", "boolean", "object", "arguments"]; | ||
var extendedTypes = ["nan", "infinity", "integer", "float"]; | ||
/****************************************************************************** | ||
* type [function] | ||
* | ||
* a function that returns the type of an object similarly to 'typeof' but with | ||
@@ -74,9 +50,6 @@ * the extended type differentiation proposed for EcmaScript 6 (Harmony). This | ||
* | ||
* arguments: | ||
* value - [any] JavaScript value to get the type of | ||
* extended - [boolean] whether to distinguish between numeric values, and/or | ||
* custom defined types | ||
* numericBase - [boolean] if extended is true, specifies whether to return | ||
* extended numeric types | ||
* returns: [string] of one of one of the following types | ||
* @param value any type of JavaScript value to get the type of | ||
* @param {Boolean} extended whether to distinguish between numeric values and custom defined types | ||
* @param {Boolean} numericBase if extended is true, specifies whether to return extended numeric types | ||
* @returns {String} of one of one of the following types | ||
* - undefined | ||
@@ -96,13 +69,13 @@ * - null | ||
* | ||
* usage example: | ||
* var obj = { foo: 'bar' }, | ||
* objType = type(obj); // 'object' | ||
* type(7.1, true); // 'float' | ||
* function MyClass() { } | ||
* defineType('myclass', MyClass); | ||
* var instance = new MyClass(); | ||
* type(instance); // 'myclass' | ||
***********************************/ | ||
* @example | ||
* var type = jsType.type; | ||
* type({ foo: 'bar' }); // 'object' | ||
* type(7.1, true); // 'float' | ||
* function MyClass() { } | ||
* defineType('myclass', MyClass); | ||
* var instance = new MyClass(); | ||
* type(instance); // 'myclass' | ||
*/ | ||
exports.type = function type(value, extended, baseNumeric) { | ||
function type(value, extended, baseNumeric) { | ||
if (value === 'undefined') return 'undefined'; | ||
@@ -129,12 +102,11 @@ if (value === null) return 'null'; | ||
} | ||
exports.type = type; | ||
/**** | ||
* defineType | ||
* | ||
* arguments: | ||
* type [ string ] - named type for the constructor instances | ||
* constructor [ function ] - an object constructor | ||
* | ||
***/ | ||
exports.define = function define(typeName, constructor) { | ||
/** | ||
* define a custom type | ||
* @param {String} type named type for the constructor instances | ||
* @param {Function} constructor an object constructor | ||
*/ | ||
function define(typeName, constructor) { | ||
if (typeof typeName === 'function') { | ||
@@ -148,5 +120,28 @@ constructor = typeName; | ||
customTypes.push(typeName); | ||
extendedTypes.push(typeName); | ||
customConstructors.push(constructor); | ||
}; | ||
var methodName = 'is' + typeName.charAt(0).toUpperCase() + typeName.substr(1); | ||
exports[methodName] = getMethod(typeName); | ||
} | ||
exports.define = define; | ||
function getMethod(curType) { | ||
if (extendedTypes.indexOf(curType) === -1) { | ||
return function(value) { | ||
return type(value) === curType; | ||
}; | ||
} else { | ||
return function (value) { | ||
return type(value, true) === curType; | ||
}; | ||
} | ||
} | ||
for (var i = 0, l = reservedTypes.length; i < l; i++) { | ||
var curType = reservedTypes[i]; | ||
var methodName = 'is' + curType.charAt(0).toUpperCase() + curType.substr(1); | ||
exports[methodName] = getMethod(curType); | ||
} | ||
} | ||
})(this, 'jsType'); |
{ | ||
"name": "jstype", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Extended and extensible javascript type checking", | ||
@@ -20,4 +20,7 @@ "main": "jstype.js", | ||
], | ||
"author": "Nathan Cartwright", | ||
"license": "MIT" | ||
"author": { | ||
"name": "Nathan Cartwright" | ||
}, | ||
"license": "MIT", | ||
"readmeFilename": "README.md" | ||
} |
@@ -7,6 +7,7 @@ JsType | ||
### installation (Node.js) | ||
### installation | ||
npm install jstype | ||
or load it in the browser with a script tag, or as an AMD module | ||
jstype can also be used in the browser either by loading it via a script tag or as an AMD module. | ||
@@ -16,3 +17,3 @@ | ||
#####jsType.type | ||
#####type | ||
@@ -58,5 +59,17 @@ ######usage: | ||
##### is[Type] | ||
#####jsType.define | ||
###### usage: | ||
jsType.is{Type}(value) | ||
Examples: | ||
jsType.isNumber(27); // true | ||
jsType.isInteger(27); // true | ||
jsType.isString('foo'); // true | ||
jsType.isString(27); // false | ||
#####define | ||
######usage: | ||
@@ -81,5 +94,5 @@ | ||
### License | ||
MIT License | ||
MIT-Style License | ||
Copyright (c) 2013 Mashdraggin | ||
Copyright (c) 2013 Nathan Cartwright | ||
@@ -86,0 +99,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
@@ -7,21 +7,8 @@ "use strict"; | ||
test("jsType module", function (test) { | ||
var Type = jsType.Type; | ||
test.test("Type", function (test) { | ||
test.ok(jsType.Type); | ||
test.equal(jsType.Type[Object], 'object'); | ||
test.equal(jsType.Type[Array], 'array'); | ||
test.equal(jsType.Type[Date], 'date'); | ||
test.equal(jsType.Type[Function], 'function'); | ||
test.equal(jsType.Type[RegExp], 'regexp'); | ||
test.equal(jsType.Type[Boolean], 'boolean'); | ||
test.equal(jsType.Type[String], 'string'); | ||
test.equal(jsType.Type[Number], 'number'); | ||
test.end(); | ||
}); | ||
test("jsType module", function(test) { | ||
test.test("type", function (test) { | ||
test.test("type", function(test) { | ||
var type = jsType.type; | ||
test.ok(type); | ||
test.test("base types", function (test) { | ||
test.test("base types", function(test) { | ||
test.equal(type(undefined), 'undefined'); | ||
@@ -32,3 +19,3 @@ test.equal(type(null), 'null'); | ||
test.equal(type(new Date('1-1-2011')), 'date'); | ||
test.equal(type(function test() { }), 'function'); | ||
test.equal(type(function test() {}), 'function'); | ||
test.equal(type(true), 'boolean'); | ||
@@ -44,7 +31,9 @@ test.equal(type(false), 'boolean'); | ||
test.equal(type({}), 'object'); | ||
test.equal(type({ foo: 'bar' }), 'object'); | ||
test.equal(type({ | ||
foo: 'bar' | ||
}), 'object'); | ||
test.end(); | ||
}); | ||
test.test("extended numeric types", function (test) { | ||
test.test("extended numeric types", function(test) { | ||
test.equal(type(1, true), 'integer'); | ||
@@ -58,7 +47,9 @@ test.equal(type(1.1, true), 'float'); | ||
test.test("custom defined types", function (test) { | ||
test.test("custom defined types", function(test) { | ||
test.ok(jsType.define); | ||
// test custom defined types | ||
function Custom1() { }; | ||
function Custom2() { }; | ||
function Custom1() {} | ||
function Custom2() {} | ||
jsType.define(Custom1); | ||
@@ -72,3 +63,3 @@ jsType.define('custom2', Custom2); | ||
test.test("baseNumeric argument with extended types", function (test) { | ||
test.test("baseNumeric argument with extended types", function(test) { | ||
test.equal(type(1, true, true), 'number'); | ||
@@ -84,2 +75,15 @@ test.equal(type(instance1, true, true), 'Custom1'); | ||
test.test("built-in isType methods", function(test) { | ||
test.ok(jsType.isNumber(1)); | ||
test.ok(jsType.isString('foo')); | ||
test.ok(jsType.isObject({ | ||
foo: 'bar' | ||
})); | ||
test.ok(jsType.isArray([1, 2])); | ||
test.ok(jsType.isDate(new Date())); | ||
test.ok(jsType.isRegexp(/./g)); | ||
test.ok(jsType.isFunction(function () {})); | ||
test.end(); | ||
}); | ||
test.end(); | ||
@@ -91,3 +95,2 @@ | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
112
17311
205