Skip to content

Commit b1c4cb9

Browse files
rolftimmermansmhdawson
authored andcommitted
Add API for deletion of object properties.
PR-URL: #151 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 9535d82 commit b1c4cb9

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

napi-inl.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,28 @@ inline void Object::Set(const std::string& utf8name, double numberValue) {
757757
Set(utf8name.c_str(), Number::New(Env(), numberValue));
758758
}
759759

760+
inline bool Object::Delete(napi_value key) {
761+
bool result;
762+
napi_status status = napi_delete_property(_env, _value, key, &result);
763+
NAPI_THROW_IF_FAILED(_env, status, false);
764+
return result;
765+
}
766+
767+
inline bool Object::Delete(Value key) {
768+
bool result;
769+
napi_status status = napi_delete_property(_env, _value, key, &result);
770+
NAPI_THROW_IF_FAILED(_env, status, false);
771+
return result;
772+
}
773+
774+
inline bool Object::Delete(const char* utf8name) {
775+
return Delete(String::New(_env, utf8name));
776+
}
777+
778+
inline bool Object::Delete(const std::string& utf8name) {
779+
return Delete(String::New(_env, utf8name));
780+
}
781+
760782
inline bool Object::Has(uint32_t index) const {
761783
bool result;
762784
napi_status status = napi_has_element(_env, _value, index, &result);
@@ -797,6 +819,13 @@ inline void Object::Set(uint32_t index, double numberValue) {
797819
Set(index, static_cast<napi_value>(Number::New(Env(), numberValue)));
798820
}
799821

822+
inline bool Object::Delete(uint32_t index) {
823+
bool result;
824+
napi_status status = napi_delete_element(_env, _value, index, &result);
825+
NAPI_THROW_IF_FAILED(_env, status, false);
826+
return result;
827+
}
828+
800829
inline Array Object::GetPropertyNames() {
801830
napi_value result;
802831
napi_status status = napi_get_property_names(_env, _value, &result);
@@ -1657,7 +1686,7 @@ inline Reference<T>& Reference<T>::operator =(Reference<T>&& other) {
16571686
}
16581687

16591688
template <typename T>
1660-
inline Reference<T>::Reference(const Reference<T>& other)
1689+
inline Reference<T>::Reference(const Reference<T>& other)
16611690
: _env(other._env), _ref(nullptr), _suppressDestruct(false) {
16621691
HandleScope scope(_env);
16631692

napi.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,26 @@ namespace Napi {
491491
double numberValue ///< Property value
492492
);
493493

494+
/// Delete property.
495+
bool Delete(
496+
napi_value key ///< Property key primitive
497+
);
498+
499+
/// Delete property.
500+
bool Delete(
501+
Value key ///< Property key
502+
);
503+
504+
/// Delete property.
505+
bool Delete(
506+
const char* utf8name ///< UTF-8 encoded null-terminated property name
507+
);
508+
509+
/// Delete property.
510+
bool Delete(
511+
const std::string& utf8name ///< UTF-8 encoded property name
512+
);
513+
494514
/// Checks whether an indexed property is present.
495515
bool Has(
496516
uint32_t index ///< Property / element index
@@ -537,6 +557,11 @@ namespace Napi {
537557
double numberValue ///< Property value
538558
);
539559

560+
/// Deletes an indexed property or array element.
561+
bool Delete(
562+
uint32_t index ///< Property / element index
563+
);
564+
540565
Array GetPropertyNames(); ///< Get all property names
541566

542567
/// Defines a property on the object.

test/object.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ void SetProperty(const CallbackInfo& info) {
102102
obj.Set(name, value);
103103
}
104104

105+
Value DeleteProperty(const CallbackInfo& info) {
106+
Object obj = info[0].As<Object>();
107+
Name name = info[1].As<Name>();
108+
return Boolean::New(info.Env(), obj.Delete(name));
109+
}
110+
105111
Object InitObject(Env env) {
106112
Object exports = Object::New(env);
107113

@@ -110,6 +116,7 @@ Object InitObject(Env env) {
110116
exports["defineValueProperty"] = Function::New(env, DefineValueProperty);
111117
exports["getProperty"] = Function::New(env, GetProperty);
112118
exports["setProperty"] = Function::New(env, SetProperty);
119+
exports["deleteProperty"] = Function::New(env, DeleteProperty);
113120

114121
return exports;
115122
}

test/object.js

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

85+
{
86+
const obj = { one: 1, two: 2 };
87+
Object.defineProperty(obj, "three", {configurable: false, value: 3});
88+
assert.strictEqual(binding.object.deleteProperty(obj, 'one'), true);
89+
assert.strictEqual(binding.object.deleteProperty(obj, 'missing'), true);
90+
91+
/* Returns true for all cases except when the property is an own non-
92+
configurable property, in which case, false is returned in non-strict mode. */
93+
assert.strictEqual(binding.object.deleteProperty(obj, 'three'), false);
94+
assert.deepStrictEqual(obj, { two: 2 });
95+
}
96+
8597
{
8698
const obj = {'one': 1, 'two': 2, 'three': 3};
8799
var arr = binding.object.GetPropertyNames(obj);
@@ -94,4 +106,7 @@ function test(binding) {
94106
assert.throws(() => {
95107
binding.object.setProperty(undefined, 'test', 1);
96108
}, /object was expected/);
109+
assert.throws(() => {
110+
binding.object.deleteProperty(undefined, 'test');
111+
}, /object was expected/);
97112
}

0 commit comments

Comments
 (0)