Skip to content

Commit faada8c

Browse files
committed
feat: add NodeArray.has
1 parent ade2578 commit faada8c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/node_values.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,32 @@ pub const NodeArray = struct {
205205
};
206206
}
207207

208+
/// Gets the number of items in the array.
208209
pub fn len(self: NodeArray.Self) !u32 {
209210
var v: u32 = undefined;
210211
try s2e(c.napi_get_array_length(self.napi_env, self.napi_value, &v));
211212
return v;
212213
}
213214

214-
/// Gets a NodeValue indicating wether the object has a property with the specified name.
215+
/// Gets the NodeValue at the specified index.
215216
pub fn get(self: NodeArray.Self, index: u32) !NodeValue {
216217
var v: c.napi_value = undefined;
217218
try s2e(c.napi_get_element(self.napi_env, self.napi_value, index, &v));
218219
return NodeValue{ .napi_env = self.napi_env, .napi_value = v };
219220
}
220221

221-
/// Set the specified NodeValue at the specified index of the NodeArray.
222+
/// Sets the specified NodeValue at the specified index.
222223
pub fn set(self: NodeArray.Self, index: u32, value: NodeValue) !NodeValue {
223224
try s2e(c.napi_set_element(self.napi_env, self.napi_value, index, value.napi_value));
224225
return value;
225226
}
226227

227-
// get/set [], push, splice, etc.
228+
/// Gets whether the array has an element at the specified index.
229+
pub fn has(self: NodeArray.Self, index: u32) !bool {
230+
var v: bool = undefined;
231+
try s2e(c.napi_has_element(self.napi_env, self.napi_value, index, &v));
232+
return v;
233+
}
228234
};
229235

230236
/// Represents a JS function.

tests/node-array.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ describe("NodeArray", () => {
2020
});
2121
});
2222

23+
describe("has", () => {
24+
it("should return true for existing element", () => {
25+
expect(addon.nodeArray.has([123, 456], 1)).toEqual(true);
26+
});
27+
it("should return false for invalid index ", () => {
28+
expect(addon.nodeArray.has([], 1)).toEqual(false);
29+
});
30+
});
31+
2332
describe("set", () => {
2433
it("should set value at index", () => {
2534
const arr = [1, 2, 3] as any[];

tests/zig/node_values/NodeArrayTests.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ pub fn get(arr: NodeArray, index: u32) !NodeValue {
2424
pub fn set(arr: NodeArray, index: u32, v: NodeValue) !NodeValue {
2525
return try arr.set(index, v);
2626
}
27+
28+
pub fn has(arr: NodeArray, index: u32) !bool {
29+
return try arr.has(index);
30+
}

0 commit comments

Comments
 (0)