Skip to content

Commit

Permalink
feat: support Object.values() (#518)
Browse files Browse the repository at this point in the history
Adds support for `Object.values()`
  • Loading branch information
rorymalcolm authored Apr 13, 2024
1 parent 98effe0 commit 1ca7723
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
11 changes: 11 additions & 0 deletions builtin_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ func builtinObjectKeys(call FunctionCall) Value {
panic(call.runtime.panicTypeError("Object.Keys is nil"))
}

func builtinObjectValues(call FunctionCall) Value {
if obj, values := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(false, func(name string) bool {
values = append(values, obj.get(name))
return true
})
return objectValue(call.runtime.newArrayOf(values))
}
panic(call.runtime.panicTypeError("Object.Values is nil"))
}

func builtinObjectGetOwnPropertyNames(call FunctionCall) Value {
if obj, propertyNames := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(true, func(name string) bool {
Expand Down
38 changes: 38 additions & 0 deletions inline.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,40 @@ func TestObject_keys(t *testing.T) {
})
}

func TestObject_values(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`Object.values({ abc:"first_example", def:"second_example" })`, "first_example,second_example")

test(`
function abc() {
this.abc = "first_example";
this.def = "second_example";
}
Object.values(new abc())
`, "first_example,second_example")

test(`
function def() {
this.ghi = "third_example"
}
def.prototype = new abc();
Object.values(new def());
`, "third_example")

test(`
var arr = [1, 2, 3];
Object.values(arr);
`, "1,2,3")

test(`
var arr = [{"abc": "first_example"}, {"def": "second_example"}];
Object.values(arr);
`, "[object Object],[object Object]")
})
}

func TestObject_getOwnPropertyNames(t *testing.T) {
tt(t, func() {
test, _ := test()
Expand Down
2 changes: 2 additions & 0 deletions tools/gen-jscore/.gen-jscore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ types:
function: 1
- name: keys
function: 1
- name: values
function: 1
- name: getOwnPropertyNames
function: 1
prototype:
Expand Down

0 comments on commit 1ca7723

Please sign in to comment.