From 86362e26e01921979bbc12e02f1cdae6a255933d Mon Sep 17 00:00:00 2001 From: Mark Probst Date: Fri, 14 Jun 2024 05:04:58 -0400 Subject: [PATCH] 2.3.0: Add `mapRecord` and `mapRecordFilterUndefined` --- package.json | 4 ++-- src/index.ts | 21 +++++++++++++++++++++ tsconfig.json | 4 ++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0b6c5a1..4a2f284 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@glideapps/ts-necessities", - "version": "2.2.4", + "version": "2.3.0", "description": "Small utilities to make life with TypeScript easier", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -24,6 +24,6 @@ "eslint": "^7.18.0", "eslint-plugin-import": "^2.22.1", "typedoc": "^0.25.13", - "typescript": "^5.4.4" + "typescript": "^5.4.5" } } diff --git a/src/index.ts b/src/index.ts index 14392d8..127542b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -168,6 +168,27 @@ export function filterUndefined(arr: Iterable): T[] { return result; } +export function mapRecord(r: Record, f: (v: T, n: string) => U): Record { + const result: Record = {}; + for (const [name, value] of Object.entries(r)) { + result[name] = f(value, name); + } + return result; +} + +export function mapRecordFilterUndefined( + r: Record, + f: (v: T, n: string) => U | undefined +): Record { + const result: Record = {}; + for (const [name, value] of Object.entries(r)) { + const o = f(value, name); + if (o === undefined) continue; + result[name] = o; + } + return result; +} + /** * Returns a string representation of `e`, which is supposed to be an * exception. diff --git a/tsconfig.json b/tsconfig.json index 9cf2fee..3ecf01d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,9 +12,9 @@ "downlevelIteration": true, "noFallthroughCasesInSwitch": true, "noImplicitReturns": true, - "target": "es6", + "target": "esnext", "outDir": "dist", - "lib": ["es6", "dom"], + "lib": ["es2017", "dom"], "baseUrl": "./src", "typeRoots": ["./node_modules/@types"] },