Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit a7abf0662f57875d72a0ae2cddb7445104049dad @tj tj committed Aug 30, 2012
Showing with 119 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +14 −0 Makefile
  3. +34 −0 Readme.md
  4. +10 −0 component.json
  5. +30 −0 index.js
  6. +28 −0 test/type.js
3 .gitignore
@@ -0,0 +1,3 @@
+components
+build
+node_modules
14 Makefile
@@ -0,0 +1,14 @@
+
+test:
+ @node test/type
+
+build: components index.js
+ @component build
+
+components:
+ @Component install
+
+clean:
+ rm -fr build components template.js
+
+.PHONY: clean test
34 Readme.md
@@ -0,0 +1,34 @@
+
+# type
+
+ Type assertions aka less-broken `typeof`.
+
+## Example
+
+```js
+var type = require('type');
+
+var obj = new Date;
+if (type(obj) == 'date') ...
+```
+
+## API
+
+```js
+type(new Date) == 'date'
+type({}) == 'object'
+type(null) == 'null'
+type(undefined) == 'undefined'
+type("hey") == 'string'
+type(true) == 'boolean'
+type(false) == 'boolean'
+type(12) == 'number'
+type(type) == 'function'
+type(/asdf/) == 'regexp'
+type((function(){ return arguments })()) == 'arguments'
+type([]) == 'array'
+```
+
+## License
+
+ MIT
10 component.json
@@ -0,0 +1,10 @@
+{
+ "name": "type",
+ "description": "Cross-browser type assertions (less broken typeof)",
+ "version": "0.0.1",
+ "keywords": ["typeof", "type", "utility"],
+ "dependencies": {},
+ "scripts": [
+ "index.js"
+ ]
+}
30 index.js
@@ -0,0 +1,30 @@
+
+/**
+ * toString ref.
+ */
+
+var toString = Object.prototype.toString;
+
+/**
+ * Return the type of `val`.
+ *
+ * @param {Mixed} val
+ * @return {String}
+ * @api public
+ */
+
+module.exports = function(val){
+ switch (toString.call(val)) {
+ case '[object Function]': return 'function';
+ case '[object Date]': return 'date';
+ case '[object RegExp]': return 'regexp';
+ case '[object Arguments]': return 'arguments';
+ case '[object Array]': return 'array';
+ }
+
+ if (val === null) return 'null';
+ if (val === undefined) return 'undefined';
+ if (val === Object(val)) return 'object';
+
+ return typeof val;
+};
28 test/type.js
@@ -0,0 +1,28 @@
+
+var type = require('..');
+
+function assert(expected, val) {
+ if (expected != type(val)) {
+ throw new Error('expected "' + expected + '" for ' + val + ' but got "' + type(val) + '"');
+ }
+}
+
+function Foo(){}
+
+assert('object', {});
+assert('object', new Foo);
+assert('object', new Boolean(true));
+assert('object', new Number(123));
+assert('object', new String('whoop'));
+assert('number', 12);
+assert('string', "test");
+assert('date', new Date);
+assert('boolean', true);
+assert('boolean', false);
+assert('null', null);
+assert('undefined', undefined);
+assert('array', []);
+assert('regexp', /asdf/);
+assert('regexp', new RegExp('weee'));
+assert('function', function(){});
+assert('arguments', (function(){ return arguments })());

0 comments on commit a7abf06

Please sign in to comment.
Something went wrong with that request. Please try again.