diff --git a/index.test.ts b/index.test.ts index a7fa79a..62104a7 100644 --- a/index.test.ts +++ b/index.test.ts @@ -1,3 +1,7 @@ import test from "ava"; +import {map} from "./index"; -test("placeholder test", t => t.true(true)); \ No newline at end of file +test("map", t => { + t.deepEqual(map({a: 1, b: 2}, value => value + 1), {a: 2, b: 3}); + t.deepEqual(map({a: 1, b: 2}, (value, key) => (value * 2) + key), {a: "2a", b: "4b"}); +}); \ No newline at end of file diff --git a/index.ts b/index.ts index d9b5101..05c646d 100644 --- a/index.ts +++ b/index.ts @@ -5,12 +5,23 @@ export function copy(dictionary: ReadonlyDictionary): Dictionary { return {...dictionary}; } +// tslint:disable-next-line:no-unbound-method export const keys: (dictionary: ReadonlyDictionary) => string[] = Object.keys; +// tslint:disable-next-line:no-unbound-method export const values: (dictionary: ReadonlyDictionary) => T[] = Object.values; +// tslint:disable-next-line:no-unbound-method export const entries: (dictionary: ReadonlyDictionary) => Array<[string, T]> = Object.entries; export function empty(dictionary: ReadonlyDictionary): boolean { return keys(dictionary).length === 0; +} + +export function map(dictionary: ReadonlyDictionary, f: (value: T, key: string) => U): Dictionary { + const result: Dictionary = {}; + for (const [key, value] of entries(dictionary)) { + result[key] = f(value, key); + } + return result; } \ No newline at end of file