forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibJS: Add Object.{keys,values,entries}()
- Loading branch information
1 parent
36a5e0b
commit 683a069
Showing
7 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
load("test-common.js"); | ||
|
||
try { | ||
assert(Object.entries.length === 1); | ||
assert(Object.entries(true).length === 0); | ||
assert(Object.entries(45).length === 0); | ||
assert(Object.entries(-998).length === 0); | ||
assert(Object.entries("abcd").length === 4); | ||
assert(Object.entries([1, 2, 3]).length === 3); | ||
assert(Object.entries({ a: 1, b: 2, c: 3 }).length === 3); | ||
|
||
assertThrowsError(() => { | ||
Object.entries(null); | ||
}, { | ||
error: TypeError, | ||
message: "ToObject on null or undefined.", | ||
}); | ||
|
||
assertThrowsError(() => { | ||
Object.entries(undefined); | ||
}, { | ||
error: TypeError, | ||
message: "ToObject on null or undefined.", | ||
}); | ||
|
||
let entries = Object.entries({ foo: 1, bar: 2, baz: 3 }); | ||
assert( | ||
entries.length === 3 && entries[0].length === 2 && | ||
entries[1].length === 2 && entries[2].length === 2 && | ||
entries[0][0] === "foo" && entries[0][1] === 1 && | ||
entries[1][0] === "bar" && entries[1][1] === 2 && | ||
entries[2][0] === "baz" && entries[2][1] === 3 | ||
); | ||
|
||
entries = Object.entries(["a", "b", "c"]); | ||
assert( | ||
entries.length === 3 && entries[0].length === 2 && | ||
entries[1].length === 2 && entries[2].length === 2 && | ||
entries[0][0] === "0" && entries[0][1] === "a" && | ||
entries[1][0] === "1" && entries[1][1] === "b" && | ||
entries[2][0] === "2" && entries[2][1] === "c" | ||
); | ||
|
||
let obj = { foo: 1 }; | ||
Object.defineProperty(obj, "getFoo", { | ||
value: function() { return this.foo; }, | ||
}); | ||
let entries = Object.entries(obj); | ||
assert(entries.length === 1 && entries[0][0] === "foo" && entries[0][1] === 1); | ||
|
||
console.log("PASS"); | ||
} catch (e) { | ||
console.log("FAIL: " + e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
load("test-common.js"); | ||
|
||
try { | ||
assert(Object.keys.length === 1); | ||
assert(Object.keys(true).length === 0); | ||
assert(Object.keys(45).length === 0); | ||
assert(Object.keys(-998).length === 0); | ||
assert(Object.keys("abcd").length === 4); | ||
assert(Object.keys([1, 2, 3]).length === 3); | ||
assert(Object.keys({ a: 1, b: 2, c: 3 }).length === 3); | ||
|
||
assertThrowsError(() => { | ||
Object.keys(null); | ||
}, { | ||
error: TypeError, | ||
message: "ToObject on null or undefined.", | ||
}); | ||
|
||
assertThrowsError(() => { | ||
Object.keys(undefined); | ||
}, { | ||
error: TypeError, | ||
message: "ToObject on null or undefined.", | ||
}); | ||
|
||
let keys = Object.keys({ foo: 1, bar: 2, baz: 3 }); | ||
assert(keys[0] === "foo" && keys[1] === "bar" && keys[2] === "baz"); | ||
|
||
keys = Object.keys(["a", "b", "c"]); | ||
assert(keys[0] === "0" && keys[1] === "1" && keys[2] === "2"); | ||
|
||
let obj = { foo: 1 }; | ||
Object.defineProperty(obj, 'getFoo', { | ||
value: function() { return this.foo; }, | ||
}); | ||
keys = Object.keys(obj); | ||
assert(keys.length === 1 && keys[0] === 'foo'); | ||
|
||
console.log("PASS"); | ||
} catch (e) { | ||
console.log("FAIL: " + e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
load("test-common.js"); | ||
|
||
try { | ||
assert(Object.values.length === 1); | ||
assert(Object.values(true).length === 0); | ||
assert(Object.values(45).length === 0); | ||
assert(Object.values(-998).length === 0); | ||
assert(Object.values("abcd").length === 4); | ||
assert(Object.values([1, 2, 3]).length === 3); | ||
assert(Object.values({ a: 1, b: 2, c: 3 }).length === 3); | ||
|
||
assertThrowsError(() => { | ||
Object.values(null); | ||
}, { | ||
error: TypeError, | ||
message: "ToObject on null or undefined.", | ||
}); | ||
|
||
assertThrowsError(() => { | ||
Object.values(undefined); | ||
}, { | ||
error: TypeError, | ||
message: "ToObject on null or undefined.", | ||
}); | ||
|
||
let values = Object.values({ foo: 1, bar: 2, baz: 3 }); | ||
assert(values[0] === 1 && values[1] === 2 && values[2] === 3); | ||
|
||
values = Object.values(["a", "b", "c"]); | ||
assert(values[0] === "a" && values[1] === "b" && values[2] === "c"); | ||
|
||
let obj = { foo: 1 }; | ||
Object.defineProperty(obj, 'getFoo', { | ||
value: function() { return this.foo; }, | ||
}); | ||
let values = Object.values(obj); | ||
assert(values.length === 1 && values[0] === 1); | ||
|
||
console.log("PASS"); | ||
} catch (e) { | ||
console.log("FAIL: " + e); | ||
} |