getorset-anything 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Luca Ban - Mesqueeb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Get or Set Anything 🐊
2
+
3
+ <a href="https://www.npmjs.com/package/getorset-anything"><img src="https://img.shields.io/npm/v/getorset-anything.svg" alt="Total Downloads"></a>
4
+ <a href="https://www.npmjs.com/package/getorset-anything"><img src="https://img.shields.io/npm/dw/getorset-anything.svg" alt="Latest Stable Version"></a>
5
+
6
+ ```
7
+ npm i getorset-anything
8
+ ```
9
+
10
+ Get a Map/Obj value, or if it didn't exist yet set it and return that. Fully **TypeScript** supported! A simple & small integration.
11
+
12
+ ## Motivation
13
+
14
+ I created this package because I always hated doing this over and over again:
15
+
16
+ ```ts
17
+ const map = new Map<string, number[]>()
18
+
19
+ const id = 'abc'
20
+
21
+ let arr = map.get(id)
22
+ if (arr === undefined) {
23
+ arr = []
24
+ map.set(id, arr)
25
+ }
26
+
27
+ arr.push(100)
28
+ ```
29
+
30
+ So that is exactly what `getorset-anything` does for you! 💯
31
+
32
+ `getorset-anything` has performance in mind. It won't do a `.has()` check, like other libraries do, when it found the value it will immediately return it.
33
+
34
+ ## Usage
35
+
36
+ Maps
37
+
38
+ ```ts
39
+ import { mapGetOrSet } from 'getorset-anything'
40
+
41
+
42
+ const map = new Map<string, number[]>()
43
+
44
+ const arr = mapGetOrSet(map, 'abc', () => [])
45
+
46
+ arr.push(100) // OK!
47
+ ```
48
+
49
+ Objects
50
+
51
+ ```ts
52
+ import { objGetOrSet } from 'getorset-anything'
53
+
54
+
55
+ const obj: Record<string, number[]> = {}
56
+
57
+ const arr = objGetOrSet(obj, 'abc', () => [])
58
+
59
+ arr.push(100) // OK!
60
+ ```
61
+
62
+ ## TypeScript Support
63
+
64
+ You don't have to do anything extra for TypeScript! It comes with awesome type support.
65
+
66
+ ```ts
67
+ import { mapGetOrSet } from 'getorset-anything'
68
+
69
+ const map = new Map<string, number[]>()
70
+
71
+ const arr = mapGetOrSet(map, 'abc', () => []) // OK!
72
+ const arr2 = mapGetOrSet(map, 'abc', () => ({})) // NG! ⛔️
73
+
74
+ arr.push(100) // OK!
75
+ arr.push('100') // NG! ⛔️
76
+ ```
77
+
78
+ ## Meet the family
79
+
80
+ - [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
81
+ - [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
82
+ - [find-and-replace-anything 🎣](https://github.com/mesqueeb/find-and-replace-anything)
83
+ - [compare-anything 🛰](https://github.com/mesqueeb/compare-anything)
84
+ - [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
85
+ - [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
86
+ - [is-what 🙉](https://github.com/mesqueeb/is-what)
package/dist/index.cjs ADDED
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
7
+ *
8
+ * @example
9
+ * ```js
10
+ * const map = new Map<string, number[]>()
11
+ *
12
+ * const arr = mapGetOrSet(map, '123', () => [])
13
+ *
14
+ * arr.push('xyz')
15
+ * ```
16
+ */
17
+ function mapGetOrSet(map, key, initialValue) {
18
+ let val = map.get(key);
19
+ if (val === undefined) {
20
+ val = initialValue();
21
+ map.set(key, val);
22
+ }
23
+ return val;
24
+ }
25
+ /**
26
+ * Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
27
+ *
28
+ * @example
29
+ * ```js
30
+ * const obj: Record<string, number[]> = {}
31
+ *
32
+ * const arr = objGetOrSet(obj, '123', () => [])
33
+ *
34
+ * arr.push('xyz')
35
+ * ```
36
+ */
37
+ function objGetOrSet(obj, key, initialValue) {
38
+ let val = obj[key];
39
+ if (val === undefined) {
40
+ val = initialValue();
41
+ obj[key] = val;
42
+ }
43
+ return val;
44
+ }
45
+
46
+ exports.mapGetOrSet = mapGetOrSet;
47
+ exports.objGetOrSet = objGetOrSet;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
3
+ *
4
+ * @example
5
+ * ```js
6
+ * const map = new Map<string, number[]>()
7
+ *
8
+ * const arr = mapGetOrSet(map, '123', () => [])
9
+ *
10
+ * arr.push('xyz')
11
+ * ```
12
+ */
13
+ function mapGetOrSet(map, key, initialValue) {
14
+ let val = map.get(key);
15
+ if (val === undefined) {
16
+ val = initialValue();
17
+ map.set(key, val);
18
+ }
19
+ return val;
20
+ }
21
+ /**
22
+ * Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
23
+ *
24
+ * @example
25
+ * ```js
26
+ * const obj: Record<string, number[]> = {}
27
+ *
28
+ * const arr = objGetOrSet(obj, '123', () => [])
29
+ *
30
+ * arr.push('xyz')
31
+ * ```
32
+ */
33
+ function objGetOrSet(obj, key, initialValue) {
34
+ let val = obj[key];
35
+ if (val === undefined) {
36
+ val = initialValue();
37
+ obj[key] = val;
38
+ }
39
+ return val;
40
+ }
41
+
42
+ export { mapGetOrSet, objGetOrSet };
@@ -0,0 +1,29 @@
1
+ declare type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
2
+ declare type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
3
+ /**
4
+ * Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
5
+ *
6
+ * @example
7
+ * ```js
8
+ * const map = new Map<string, number[]>()
9
+ *
10
+ * const arr = mapGetOrSet(map, '123', () => [])
11
+ *
12
+ * arr.push('xyz')
13
+ * ```
14
+ */
15
+ export declare function mapGetOrSet<M extends Map<unknown, unknown>>(map: M, key: KeyOfMap<M>, initialValue: () => ValueOfMap<M>): ValueOfMap<M>;
16
+ /**
17
+ * Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
18
+ *
19
+ * @example
20
+ * ```js
21
+ * const obj: Record<string, number[]> = {}
22
+ *
23
+ * const arr = objGetOrSet(obj, '123', () => [])
24
+ *
25
+ * arr.push('xyz')
26
+ * ```
27
+ */
28
+ export declare function objGetOrSet<O extends Record<string | number | symbol, unknown>>(obj: O, key: keyof O, initialValue: () => O[keyof O]): O[keyof O];
29
+ export {};
package/package.json ADDED
@@ -0,0 +1,104 @@
1
+ {
2
+ "name": "getorset-anything",
3
+ "version": "0.0.1",
4
+ "sideEffects": false,
5
+ "type": "module",
6
+ "description": "Gets a value from a Map/Obj or sets an initial value when not found and returns that. TypeScript supported.",
7
+ "module": "./dist/index.es.js",
8
+ "main": "./dist/index.cjs",
9
+ "types": "./dist/types/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.es.js",
13
+ "require": "./dist/index.cjs",
14
+ "types": "./dist/types/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=12.13"
22
+ },
23
+ "scripts": {
24
+ "clean": "del ./dist",
25
+ "lint": "tsc --noEmit && eslint ./src --ext .ts",
26
+ "test": "vitest run",
27
+ "build": "npm run clean && rollup -c ./scripts/build.js",
28
+ "release": "npm run lint && del dist && npm run build && np"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/mesqueeb/getorset-anything.git"
33
+ },
34
+ "keywords": [
35
+ "javascript",
36
+ "map",
37
+ "mapgetset",
38
+ "map-get-or-set",
39
+ "object-get-or-set",
40
+ "getorset-anything",
41
+ "get-or-set",
42
+ "js-map",
43
+ "typescript",
44
+ "map-setter",
45
+ "object-setter",
46
+ "obj-set-get",
47
+ "map-util",
48
+ "obj-util",
49
+ "setget"
50
+ ],
51
+ "author": "Luca Ban - Mesqueeb",
52
+ "funding": "https://github.com/sponsors/mesqueeb",
53
+ "license": "MIT",
54
+ "bugs": {
55
+ "url": "https://github.com/mesqueeb/getorset-anything/issues"
56
+ },
57
+ "homepage": "https://github.com/mesqueeb/getorset-anything#readme",
58
+ "dependencies": {},
59
+ "devDependencies": {
60
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
61
+ "@typescript-eslint/parser": "^5.10.1",
62
+ "del-cli": "^4.0.1",
63
+ "eslint": "^8.7.0",
64
+ "eslint-config-prettier": "^8.3.0",
65
+ "eslint-plugin-tree-shaking": "^1.10.0",
66
+ "np": "^7.6.0",
67
+ "prettier": "^2.5.1",
68
+ "rollup": "^2.66.1",
69
+ "rollup-plugin-typescript2": "^0.31.1",
70
+ "typescript": "^4.5.5",
71
+ "vitest": "^0.2.3"
72
+ },
73
+ "np": {
74
+ "yarn": false,
75
+ "branch": "production"
76
+ },
77
+ "eslintConfig": {
78
+ "ignorePatterns": [
79
+ "node_modules",
80
+ "dist",
81
+ "scripts",
82
+ "test"
83
+ ],
84
+ "root": true,
85
+ "parser": "@typescript-eslint/parser",
86
+ "plugins": [
87
+ "@typescript-eslint",
88
+ "tree-shaking"
89
+ ],
90
+ "extends": [
91
+ "eslint:recommended",
92
+ "plugin:@typescript-eslint/eslint-recommended",
93
+ "plugin:@typescript-eslint/recommended",
94
+ "prettier"
95
+ ],
96
+ "rules": {
97
+ "@typescript-eslint/no-empty-function": "off",
98
+ "@typescript-eslint/no-explicit-any": "off",
99
+ "@typescript-eslint/ban-ts-ignore": "off",
100
+ "tree-shaking/no-side-effects-in-initialization": "error",
101
+ "@typescript-eslint/ban-ts-comment": "off"
102
+ }
103
+ }
104
+ }