About this project

getorset-anything is a small npm package that wraps a common pattern: looking up a key in a Map or plain object, and creating an initial value if the key is absent. The README explains the motivation with an example where a Map holds arrays and the caller must check for undefined, create an empty array, set it, and then push. The package provides mapGetOrSet and objGetOrSet functions to perform that lookup-or-initialize step in one call. Usage is shown for both Maps and objects. For a Map, mapGetOrSet(map, 'abc', () => []) returns the existing array or creates and stores a new one. For an object, objGetOrSet(obj, 'abc', () => []) does the same. The README notes that the implementation avoids a separate has() check when the value is found, returning it immediately, which is presented as a performance consideration. TypeScript support is a central feature. The README states that no extra setup is needed and shows type inference working: the factory return type must match the Map or object value type, and the returned value carries that type. The examples show a correct factory returning number[] and an incorrect factory returning an object being rejected by the type checker, as well as type errors when pushing a string into a number array. The package is part of a family of small TypeScript utilities by the same author, including is-what, is-where, merge-anything, check-anything, remove-anything, map-anything, filter-anything, copy-anything, case-anything, flatten-anything, and nestify-anything. Installation is via npm i getorset-anything. The README does not describe runtime dependencies, bundle size, benchmarks, or browser support beyond the TypeScript focus.