From 863ae8dcdfe0fce3639ee793f89b1e0535c79816 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 29 Nov 2026 15:55:51 +0000 Subject: [PATCH] Tests for Object.groupBy, [...set] / [...map], Object.keys of an object (TSLANG 229) object_group_by.ts (all models): groupBy of arrays (numbers, strings, by index) and of a Set; spreads of a Set and a Map. object_keys_of_object.ts (not own): Object.keys of a value of type `object`. They need the compiler of ASDAlexander77/TypeScriptCompiler#1066. Co-Authored-By: Claude Opus 5.5 --- tests/object_group_by.ts | 28 ++++++++++++++++++++++++++++ tests/object_keys_of_object.ts | 12 ++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/object_group_by.ts create mode 100644 tests/object_keys_of_object.ts diff --git a/tests/object_group_by.ts b/tests/object_group_by.ts new file mode 100644 index 0000000..4ba48e0 --- /dev/null +++ b/tests/object_group_by.ts @@ -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([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([["x", 1], ["y", 2]]); + const entries = [...m]; + assert(entries.length == 2 && entries[1][0] == "y", "spread of a Map"); + console.log("ALL DONE"); +} diff --git a/tests/object_keys_of_object.ts b/tests/object_keys_of_object.ts new file mode 100644 index 0000000..dc94c25 --- /dev/null +++ b/tests/object_keys_of_object.ts @@ -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"); +}