Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/object_group_by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Object.groupBy(items, f): a dictionary of arrays, each item in the array of the key f(item, index) gives, keys in
// the order they first come (TSLANG 229).
function main() {
const g = Object.groupBy([1, 2, 3, 4, 5], (x) => x % 2 == 0 ? "even" : "odd");
assert(g["even"].length == 2 && g["odd"].length == 3, "groupBy counts");
assert(g["odd"][2] == 5, "groupBy order");

const words = ["apple", "avocado", "banana"];
const byLetter = Object.groupBy(words, (w, i) => w[0]);
let keys = "";
for (const k in byLetter) keys += k + byLetter[k].length;
assert(keys == "a2b1", "groupBy keys in order");

const byIndex = Object.groupBy(words, (w, i) => i < 2 ? "first" : "rest");
assert(byIndex["first"].join(",") == "apple,avocado", "groupBy index");

// an iterable that is not an array: spread through its [Symbol.iterator]() ([...set] was "can't estimate
// element of array")
const set = new Set<number>([1, 2, 3]);
const spread = [...set];
assert(spread.length == 3 && spread[2] == 3, "spread of a Set");
const bySize = Object.groupBy(set, (n) => n > 1 ? "big" : "small");
assert(bySize["big"].length == 2 && bySize["small"][0] == 1, "groupBy of a Set");
const m = new Map<string, number>([["x", 1], ["y", 2]]);
const entries = [...m];
assert(entries.length == 2 && entries[1][0] == "y", "spread of a Map");
console.log("ALL DONE");
}
12 changes: 12 additions & 0 deletions tests/object_keys_of_object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// not own: Object.keys of a value of type `object` is read at run time, as of an `any`, which -mm=own refuses
// Object.keys of a value of type `object`: its keys are known at run time only (TSLANG 229). It was "its keys are
// not known".
class C { x = 1; y = "q"; }
function main() {
const o: object = { a: 1, b: "x" };
const keys = Object.keys(o);
assert(keys.length == 2 && keys[0] == "a" && keys[1] == "b", "keys of an object literal as object");
const c: object = new C();
assert(Object.keys(c).length == 2, "keys of a class instance as object");
console.log("ALL DONE");
}
Loading