aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2023-11-07 17:26:27 +0100
committerGitHub <[email protected]>2023-11-07 16:26:27 +0000
commit8542ef30bd4eda3d9f04db2c4a79abf0369d57c3 (patch)
tree94f768aa2c1c48faedddb745843f6d0b34b19988 /src/internal
parent9498203190e37a96114ddc8e861b02ccd94e517b (diff)
downloadfaker-8542ef30bd4eda3d9f04db2c4a79abf0369d57c3.tar.xz
faker-8542ef30bd4eda3d9f04db2c4a79abf0369d57c3.zip
infra(unicorn): no-array-reduce (#2479)
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/group-by.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/internal/group-by.ts b/src/internal/group-by.ts
new file mode 100644
index 00000000..ff3242c9
--- /dev/null
+++ b/src/internal/group-by.ts
@@ -0,0 +1,25 @@
+/**
+ * Groups the values by the key function.
+ *
+ * @internal
+ *
+ * @param values The values to group.
+ * @param keyFunction The function to get the key from the value.
+ */
+export function groupBy<TValue>(
+ values: ReadonlyArray<TValue>,
+ keyFunction: (value: TValue) => string | number
+): Record<string, TValue[]> {
+ const result: Record<string, TValue[]> = {};
+
+ for (const value of values) {
+ const key = keyFunction(value);
+ if (result[key] === undefined) {
+ result[key] = [];
+ }
+
+ result[key].push(value);
+ }
+
+ return result;
+}