Skip to content

Commit 771ff95

Browse files
committed
add .has() method
1 parent ed2d4aa commit 771ff95

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Insert a new key/value pair in the hashmap. The key must be a string, and the va
4040

4141
Lookup a value from its key. Will return undefined if the key does not exist.
4242

43+
### `has ( key )`
44+
45+
Check if key exists. Will return false if the key does not exist; otherwise true.
46+
4347
### `remove ( key )`
4448

4549
Remove a key/value pair by its key. If the key does not exist, no action will be performed and it will return false. If a pair is removed, then it will return true.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hashtable",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Native HashTable and ES6 compatible Map for Node.js",
55
"main": "./index.js",
66
"keywords": [

src/hashtable.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void HashTable::init(Handle<Object> exports) {
1616
auto ht_prototype = ht_constructor->PrototypeTemplate();
1717
ht_prototype->Set("put", FunctionTemplate::New(Put)->GetFunction());
1818
ht_prototype->Set("get", FunctionTemplate::New(Get)->GetFunction());
19+
ht_prototype->Set("has", FunctionTemplate::New(Has)->GetFunction());
1920
ht_prototype->Set("keys", FunctionTemplate::New(Keys)->GetFunction());
2021
ht_prototype->Set("remove", FunctionTemplate::New(Remove)->GetFunction());
2122
ht_prototype->Set("clear", FunctionTemplate::New(Clear)->GetFunction());
@@ -28,6 +29,7 @@ void HashTable::init(Handle<Object> exports) {
2829
auto map_prototype = map_constructor->PrototypeTemplate();
2930
map_prototype->Set("set", FunctionTemplate::New(Put)->GetFunction());
3031
map_prototype->Set("get", FunctionTemplate::New(Get)->GetFunction());
32+
map_prototype->Set("has", FunctionTemplate::New(Has)->GetFunction());
3133
map_prototype->Set("keys", FunctionTemplate::New(MapKeys)->GetFunction());
3234
map_prototype->Set("values", FunctionTemplate::New(MapValues)->GetFunction());
3335
map_prototype->Set("entries", FunctionTemplate::New(MapEntries)->GetFunction());
@@ -94,6 +96,28 @@ Handle<Value> HashTable::Get(const Arguments& args) {
9496
return scope.Close(value);
9597
}
9698

99+
Handle<Value> HashTable::Has(const Arguments& args) {
100+
HandleScope scope;
101+
102+
if (args.Length() < 1 || !args[0]->IsString()) {
103+
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
104+
return scope.Close(Undefined());
105+
}
106+
107+
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
108+
109+
Local<Value> key = Local<Value>(args[0]);
110+
String::Utf8Value keyStr(key);
111+
112+
MapType::const_iterator itr = obj->map.find(std::string(*keyStr));
113+
114+
if(itr == obj->map.end()) {
115+
return scope.Close(Boolean::New(false)); //return undefined
116+
}
117+
118+
return scope.Close(Boolean::New(true));
119+
}
120+
97121
Handle<Value> HashTable::Put(const Arguments& args) {
98122
HandleScope scope;
99123

src/hashtable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class HashTable : public node::ObjectWrap {
3030
// hashTable.get(key) : value
3131
static v8::Handle<v8::Value> Get(const v8::Arguments &args);
3232

33+
// hashTable.has(key) : boolean
34+
static v8::Handle<v8::Value> Has(const v8::Arguments &args);
35+
3336
// hashTable.put(key, value) : undefined
3437
static v8::Handle<v8::Value> Put(const v8::Arguments &args);
3538

0 commit comments

Comments
 (0)