Browse Source

initial commit, version 0.1.0

master
Brett Langdon 13 years ago
commit
536f1184fc
6 changed files with 240 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +85
    -0
      README.md
  3. +10
    -0
      binding.gyp
  4. +9
    -0
      lib/index.js
  5. +26
    -0
      package.json
  6. +109
    -0
      src/v8type.cc

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
build

+ 85
- 0
README.md View File

@ -0,0 +1,85 @@
v8type
======
Module that uses V8 itself to check the type of a given object.
## Installing
```bash
npm install v8type
```
## API
### Constants
* `ARRAY` - `'array'`
* `BOOLEAN` - `'boolean'`
* `DATE` - `'date'`
* `FUNCTION - `'function'`
* `NULL` - `'null'`
* `NUMBER` - `'number'`
* `OBJECT` - `'object'`
* `REGEXP` - `'regexp'`
* `STRING` - `'string'`
* `UNDEFINED` - `'undefined'`
### Functions
* `of(object)` - returns the type of `object` as a string defined in the corresponding constants above.
* `is(object, type)` - returns `boolean` if `object` matches the string type `type` (string defined in the corresponding constants above).
## Examples
### of function
```javascript
var v8type = require('v8type');
console.log(v8type.of([1,2,3,4]));
// 'array'
console.log(v8type.of(undefined));
// 'undefined'
console.log(v8type.of(5));
// 'number'
console.log(v8type.of('v8type'));
// 'string'
console.log(v8type.of(new RegExp('v8type')));
// 'regexp'
console.log(v8type.of({}));
// 'object'
console.log(v8type.of(function(){}));
// 'function'
console.log(v8type.of(new Date()));
// 'date'
```
### is function
```javascript
var v8type = require('v8type');
v8type.is([1,2,3], v8type.ARRAY);
// true
v8type.is({}, v8type.NULL);
// false
// Is does not check inheritence
v8type.is([], v8type.OBJECT);
// false
```
## License
```
The MIT License (MIT)
Copyright (c) 2012 Brett Langdon <brett@blangdon.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```

+ 10
- 0
binding.gyp View File

@ -0,0 +1,10 @@
{
"targets": [
{
"target_name": "v8type",
"sources": [
"src/v8type.cc"
]
}
]
}

+ 9
- 0
lib/index.js View File

@ -0,0 +1,9 @@
var v8type = null;
try{
v8type = require('../build/Release/v8type');
}catch(e){
v8type = require('../build/Debug/v8type');
}
module.exports = v8type;

+ 26
- 0
package.json View File

@ -0,0 +1,26 @@
{
"name": "v8type",
"version": "0.1.0",
"description": "Expose V8 C++ type checking to JS",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/brettlangdon/v8type.git"
},
"keywords": [
"type",
"of",
"checking",
"check",
"v8",
"typeof",
"type-checking",
"type-check",
"checker"
],
"author": "Brett Langdon <brett@blangdon.com> (http://brett.is/)",
"license": "MIT"
}

+ 109
- 0
src/v8type.cc View File

@ -0,0 +1,109 @@
#include <node.h>
#include <v8.h>
using namespace v8;
#define ARRAY String::NewSymbol("array")
#define BOOLEAN String::NewSymbol("boolean")
#define DATE String::NewSymbol("date")
#define FUNCTION String::NewSymbol("function")
#define NUMBER String::NewSymbol("number")
#define REGEXP String::NewSymbol("regexp")
#define STRING String::NewSymbol("string")
#define OBJECT String::NewSymbol("object")
#define UNDEFINED String::NewSymbol("undefined")
#define NULL_TYPE String::NewSymbol("null")
Handle<Value> v8type_of(const Arguments& args){
HandleScope scope;
if(args.Length() < 1){
ThrowException(Exception::TypeError(String::New("v8type.of function requires at least 1 argument")));
return scope.Close(Undefined());
}
if(args[0]->IsArray()){
return scope.Close(ARRAY);
} else if(args[0]->IsBoolean()){
return scope.Close(BOOLEAN);
} else if(args[0]->IsDate()){
return scope.Close(DATE);
} else if(args[0]->IsFunction()){
return scope.Close(FUNCTION);
} else if(args[0]->IsNumber()){
return scope.Close(NUMBER);
} else if(args[0]->IsString()){
return scope.Close(STRING);
} else if(args[0]->IsRegExp()){
return scope.Close(REGEXP);
} else if(args[0]->IsUndefined()){
return scope.Close(UNDEFINED);
} else if(args[0]->IsNull()){
return scope.Close(NULL_TYPE);
} else if(args[0]->IsObject()){
return scope.Close(OBJECT);
}
return scope.Close(Undefined());
}
Handle<Value> v8type_is(const Arguments& args){
HandleScope scope;
if(args.Length() < 2){
ThrowException(Exception::TypeError(String::New("v8type.is function requires at least 2 arguments")));
return scope.Close(Undefined());
}
if(!args[1]->IsString()){
ThrowException(Exception::TypeError(String::New("v8type.is second argument must be a string")));
return scope.Close(Undefined());
}
Handle<Context> context = Context::New();
Context::Scope context_scope(context);
Handle<Value> arguments[] = { args[0] };
Handle<Value> type = FunctionTemplate::New(v8type_of)->GetFunction()->Call(context->Global(), 1, arguments);
return scope.Close(Boolean::New(args[1] == type));
}
void init_constants(Handle<Object> target){
target->Set(String::NewSymbol("ARRAY"), ARRAY);
target->Set(String::NewSymbol("BOOLEAN"), BOOLEAN);
target->Set(String::NewSymbol("DATE"), DATE);
target->Set(String::NewSymbol("FUNCTION"), FUNCTION);
target->Set(String::NewSymbol("NUMBER"), NUMBER);
target->Set(String::NewSymbol("REGEXP"), REGEXP);
target->Set(String::NewSymbol("STRING"), STRING);
target->Set(String::NewSymbol("OBJECT"), OBJECT);
target->Set(String::NewSymbol("UNDEFINED"), UNDEFINED);
target->Set(String::NewSymbol("NULL"), NULL_TYPE);
}
void init_v8type_of(Handle<Object> target){
target->Set(String::NewSymbol("of"), FunctionTemplate::New(v8type_of)->GetFunction());
}
void init_v8type_is(Handle<Object> target){
target->Set(String::NewSymbol("is"), FunctionTemplate::New(v8type_is)->GetFunction());
}
void init(Handle<Object> target){
init_constants(target);
init_v8type_of(target);
init_v8type_is(target);
}
NODE_MODULE(v8type, init);

Loading…
Cancel
Save