getorset-anything
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -11,2 +11,3 @@ type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never; | ||
* const arr = mapGetOrSet(map, '123', () => []) | ||
* // ↳ [] | ||
* | ||
@@ -25,2 +26,3 @@ * arr.push('xyz') | ||
* const arr = objGetOrSet(obj, '123', () => []) | ||
* // ↳ [] | ||
* | ||
@@ -31,3 +33,15 @@ * arr.push('xyz') | ||
declare function objGetOrSet<O extends Record<string | number | symbol, unknown>, T extends () => O[keyof O]>(obj: O, key: keyof O, initialValue: T): ReturnType<T>; | ||
/** | ||
* Retrieve the value in an array, or if it wasn't found, set an initial value and return that. | ||
* | ||
* @example | ||
* ```js | ||
* const arr: number[] = [] | ||
* | ||
* const val = arrGetOrSet(arr, 0, () => 123) | ||
* // ↳ 123 | ||
* ``` | ||
*/ | ||
declare function arrGetOrSet<A extends unknown[], T extends () => A[number]>(arr: A, index: number, initialValue: T): ReturnType<T>; | ||
export { mapGetOrSet, objGetOrSet }; | ||
export { arrGetOrSet, mapGetOrSet, objGetOrSet }; |
@@ -17,3 +17,11 @@ function mapGetOrSet(map, key, initialValue) { | ||
} | ||
function arrGetOrSet(arr, index, initialValue) { | ||
let val = arr[index]; | ||
if (val === void 0) { | ||
val = initialValue(); | ||
arr[index] = val; | ||
} | ||
return val; | ||
} | ||
export { mapGetOrSet, objGetOrSet }; | ||
export { arrGetOrSet, mapGetOrSet, objGetOrSet }; |
{ | ||
"name": "getorset-anything", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Gets a value from a Map/Obj or sets an initial value when not found and returns that. TypeScript supported.", | ||
@@ -61,5 +61,2 @@ "type": "module", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mesqueeb/getorset-anything/issues" | ||
}, | ||
"homepage": "https://github.com/mesqueeb/getorset-anything#readme", | ||
@@ -66,0 +63,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10453
96
0
1