Personal re-usable ES/TS utilities
npm install @yungezeit/utilsPersonal re-usable ES/TS utilities.
``sh`Using npm
npm i -D @yungezeit/utilsUsing Yarn
yarn add -D @yungezeit/utilsUsing pnpm
pnpm add -D @yungezeit/utilsUsing bun
bun add -D @yungezeit/utils
`ts
import { JSONX } from "@yungezeit/utils";
const json = JSONX.stringify({
someMap: new Map([
["key1", "value-of-key1"],
["key2", "value-of-key2"],
]),
someSet: new Set(["item1", "item2"]),
});
const parsed = JSONX.parse(json);
const someMapItem = parsed.someMap.get("key2"); // "value-of-key2"
const someSetItem = parsed.someSet.has("item2"); // true
`
Adds an optional second argument to Map's get method which can be used to provide a default value if the key does not exist. This default value is not only returned but also set in the map. Providing a default value also marks the return type as non-nullable.
`ts
import { MapX } from "@yungezeit/utils";
type MyMap = MapX
const myMap: MyMap = new MapX([]);
myMap.get("key1", 42); // 42
myMap.has("key1"); // true
`
Notes :
- This does not change the original Map prototype.
- A map value can exist and still be undefined, but the default value logic relies on whether theundefined` as a default value.
second argument were defined or not. This means you can't use