Skip to content

Commit 3aab97b

Browse files
Sampson Gaojasongin
authored andcommitted
Add API for getting object's property names (#60)
1 parent 0302651 commit 3aab97b

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

napi-inl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,13 @@ inline void Object::Set(uint32_t index, double numberValue) {
796796
Set(index, static_cast<napi_value>(Number::New(Env(), numberValue)));
797797
}
798798

799+
inline Array Object::GetPropertyNames() {
800+
napi_value result;
801+
napi_status status = napi_get_property_names(_env, _value, &result);
802+
NAPI_THROW_IF_FAILED(_env, status, Array());
803+
return Array(_env, result);
804+
}
805+
799806
inline void Object::DefineProperty(const PropertyDescriptor& property) {
800807
napi_status status = napi_define_properties(_env, _value, 1,
801808
reinterpret_cast<const napi_property_descriptor*>(&property));

napi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ namespace Napi {
523523
double numberValue ///< Property value
524524
);
525525

526+
Array GetPropertyNames(); ///< Get all property names
527+
526528
/// Defines a property on the object.
527529
void DefineProperty(
528530
const PropertyDescriptor& property ///< Descriptor for the property to be defined

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
"pretest": "node-gyp rebuild -C test",
3232
"test": "node test"
3333
},
34-
"version": "0.3.3"
34+
"version": "0.3.4"
3535
}

test/object.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Value TestFunction(const CallbackInfo& info) {
1616
return Boolean::New(info.Env(), true);
1717
}
1818

19+
Array GetPropertyNames(const CallbackInfo& info) {
20+
Object obj = info[0].As<Object>();
21+
Array arr = obj.GetPropertyNames();
22+
return arr;
23+
}
24+
1925
void DefineProperties(const CallbackInfo& info) {
2026
Object obj = info[0].As<Object>();
2127
String nameType = info[1].As<String>();
@@ -87,6 +93,7 @@ void SetProperty(const CallbackInfo& info) {
8793
Object InitObject(Env env) {
8894
Object exports = Object::New(env);
8995

96+
exports["GetPropertyNames"] = Function::New(env, GetPropertyNames);
9097
exports["defineProperties"] = Function::New(env, DefineProperties);
9198
exports["defineValueProperty"] = Function::New(env, DefineValueProperty);
9299
exports["getProperty"] = Function::New(env, GetProperty);

test/object.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ function test(binding) {
8282
assert.strictEqual(obj.test, 1);
8383
}
8484

85+
{
86+
const obj = {'one': 1, 'two': 2, 'three': 3};
87+
var arr = binding.object.GetPropertyNames(obj);
88+
assert.deepStrictEqual(arr, ['one', 'two', 'three'])
89+
}
90+
8591
assert.throws(() => {
8692
binding.object.getProperty(undefined, 'test');
8793
}, /object was expected/);

0 commit comments

Comments
 (0)