Skip to content

Commit

Permalink
2.3.0: Add mapRecord and mapRecordFilterUndefined
Browse files Browse the repository at this point in the history
  • Loading branch information
schani committed Jun 14, 2024
1 parent 4c3d12f commit 86362e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ export function filterUndefined<T>(arr: Iterable<T | undefined>): T[] {
return result;
}

export function mapRecord<T, U>(r: Record<string, T>, f: (v: T, n: string) => U): Record<string, U> {
const result: Record<string, U> = {};
for (const [name, value] of Object.entries(r)) {
result[name] = f(value, name);
}
return result;
}

export function mapRecordFilterUndefined<T, U>(
r: Record<string, T>,
f: (v: T, n: string) => U | undefined
): Record<string, U> {
const result: Record<string, U> = {};
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.
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
},
Expand Down

0 comments on commit 86362e2

Please sign in to comment.