@bemoje/map 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) 2023 Benjamin Møller Jensen
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.
@@ -0,0 +1,6 @@
1
+ /*!
2
+ * @bemoje/map v0.0.1
3
+ * (c) Benjamin Møller Jensen
4
+ * Released under the MIT License.
5
+ */"use strict";function mapGetOrElse(map,key,callback){let value=map.get(key);return void 0===value?(value=callback(key),map.set(key,value),value):value}function mapReverse(map){return new Map([...map.entries()].reverse())}function mapUpdate(map,key,fun){const value=fun(map.get(key),key);return map.set(key,value),value}exports.mapGetOrElse=mapGetOrElse,exports.mapReverse=mapReverse,exports.mapUpdate=mapUpdate;
6
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/lib/mapGetOrElse.ts","../src/lib/mapReverse.ts","../src/lib/mapUpdate.ts"],"sourcesContent":["/**\n * Returns a value from a map, while setting a given default value before returning it, if the key is not present.\n * @param map map to get value from\n * @param key key to get value for\n * @param callback callback to set the value if key is not present\n * @template K - The type of the keys in the Map.\n * @returns The value retrieved from the Map or generated by the callback function.\n * @example ```ts\n * const map = new Map<string, number>()\n * map.set('key', 1)\n * mapGetOrElse(map, 'nonexistentKey', () => 2) // Output: 2\n * ```\n */\nexport function mapGetOrElse<K, V>(map: Map<K, V>, key: K, callback: (key: K) => V): V {\n let value = map.get(key)\n if (value !== undefined) return value\n value = callback(key)\n map.set(key, value)\n return value\n}\n","/**\n * Reverses the order of entries in a Map.\n * @remarks This function creates a new Map with the entries of the original Map in reverse order.\n * The original Map is not modified.\n * @typeparam K - The type of the keys in the Map.\n * @typeparam V - The type of the values in the Map.\n * @param map The Map to reverse.\n * @returns A new Map with the entries of the original Map in reverse order.\n * @example ```ts\n * mapReverse(new Map([['a', 1], ['b', 2], ['c', 3]]));;\n * //=> [['c', 3], ['b', 2], ['a', 1]]\n * ```\n */\nexport function mapReverse<K, V>(map: Map<K, V>): Map<K, V> {\n return new Map<K, V>([...map.entries()].reverse())\n}\n","/**\n * Updates the value for the given key in the map using the provided function.\n * If the key does not exist in the map, the function will be called with `undefined` as the value.\n * @template K - The type of the keys in the map.\n * @param map - The map to update.\n * @param key - The key of the value to update.\n * @param fun - The function to use to update the value.\n * This function takes the current value (or `undefined` if the key does not exist) and the key as arguments,\n * and should return the new value.\n * @returns - The new value for the key.\n * @example ```ts\n * const map = new Map<string, number>()\n * map.set('a', 1)\n * mapUpdate(map, 'a', (value, key) => value !== undefined ? value + 1 : 0)\n * map.get('a')\n * //=> 2\n * ```\n */\nexport function mapUpdate<K, V>(map: Map<K, V>, key: K, fun: (value: V | undefined, key: K) => V): V {\n const value = fun(map.get(key), key)\n map.set(key, value)\n return value\n}\n"],"names":["mapGetOrElse","map","key","callback","value","get","set","mapReverse","Map","entries","reverse","mapUpdate","fun"],"mappings":";;;;wBAagB,CAAAA,YAAYA,CAAOC,GAAc,CAAEC,GAAM,CAAEC,QAAuB,CAAA,CAChF,GAAI,CAAAC,KAAK,CAAGH,GAAG,CAACI,GAAG,CAACH,GAAG,CAAC,CAAA,MACpB,UAAAE,KAAmB,EACvBA,KAAK,CAAGD,QAAQ,CAACD,GAAG,CAAC,CACrBD,GAAG,CAACK,GAAG,CAACJ,GAAG,CAAEE,KAAK,CAAC,CACZA,KAAK,EAHoBA,KAIlC,CCNM,QAAU,CAAAG,UAAUA,CAAON,GAAc,CAAA,CAC7C,MAAO,IAAI,CAAAO,GAAG,CAAO,CAAC,GAAGP,GAAG,CAACQ,OAAO,EAAE,CAAC,CAACC,OAAO,CAAA,CAAE,CACnD,SCGgB,CAAAC,SAASA,CAAOV,GAAc,CAAEC,GAAM,CAAEU,GAAwC,CAAA,CAC9F,KAAM,CAAAR,KAAK,CAAGQ,GAAG,CAACX,GAAG,CAACI,GAAG,CAACH,GAAG,CAAC,CAAEA,GAAG,CAAC,CAEpC,MADA,CAAAD,GAAG,CAACK,GAAG,CAACJ,GAAG,CAAEE,KAAK,CAAC,CACZA,KACT"}
@@ -0,0 +1,6 @@
1
+ /*!
2
+ * @bemoje/map v0.0.1
3
+ * (c) Benjamin Møller Jensen
4
+ * Released under the MIT License.
5
+ */function mapGetOrElse(map,key,callback){let value=map.get(key);return void 0===value?(value=callback(key),map.set(key,value),value):value}function mapReverse(map){return new Map([...map.entries()].reverse())}function mapUpdate(map,key,fun){const value=fun(map.get(key),key);return map.set(key,value),value}export{mapGetOrElse,mapReverse,mapUpdate};
6
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/lib/mapGetOrElse.ts","../src/lib/mapReverse.ts","../src/lib/mapUpdate.ts"],"sourcesContent":["/**\n * Returns a value from a map, while setting a given default value before returning it, if the key is not present.\n * @param map map to get value from\n * @param key key to get value for\n * @param callback callback to set the value if key is not present\n * @template K - The type of the keys in the Map.\n * @returns The value retrieved from the Map or generated by the callback function.\n * @example ```ts\n * const map = new Map<string, number>()\n * map.set('key', 1)\n * mapGetOrElse(map, 'nonexistentKey', () => 2) // Output: 2\n * ```\n */\nexport function mapGetOrElse<K, V>(map: Map<K, V>, key: K, callback: (key: K) => V): V {\n let value = map.get(key)\n if (value !== undefined) return value\n value = callback(key)\n map.set(key, value)\n return value\n}\n","/**\n * Reverses the order of entries in a Map.\n * @remarks This function creates a new Map with the entries of the original Map in reverse order.\n * The original Map is not modified.\n * @typeparam K - The type of the keys in the Map.\n * @typeparam V - The type of the values in the Map.\n * @param map The Map to reverse.\n * @returns A new Map with the entries of the original Map in reverse order.\n * @example ```ts\n * mapReverse(new Map([['a', 1], ['b', 2], ['c', 3]]));;\n * //=> [['c', 3], ['b', 2], ['a', 1]]\n * ```\n */\nexport function mapReverse<K, V>(map: Map<K, V>): Map<K, V> {\n return new Map<K, V>([...map.entries()].reverse())\n}\n","/**\n * Updates the value for the given key in the map using the provided function.\n * If the key does not exist in the map, the function will be called with `undefined` as the value.\n * @template K - The type of the keys in the map.\n * @param map - The map to update.\n * @param key - The key of the value to update.\n * @param fun - The function to use to update the value.\n * This function takes the current value (or `undefined` if the key does not exist) and the key as arguments,\n * and should return the new value.\n * @returns - The new value for the key.\n * @example ```ts\n * const map = new Map<string, number>()\n * map.set('a', 1)\n * mapUpdate(map, 'a', (value, key) => value !== undefined ? value + 1 : 0)\n * map.get('a')\n * //=> 2\n * ```\n */\nexport function mapUpdate<K, V>(map: Map<K, V>, key: K, fun: (value: V | undefined, key: K) => V): V {\n const value = fun(map.get(key), key)\n map.set(key, value)\n return value\n}\n"],"names":["mapGetOrElse","map","key","callback","value","get","set","mapReverse","Map","entries","reverse","mapUpdate","fun"],"mappings":";;;;WAagB,CAAAA,YAAYA,CAAOC,GAAc,CAAEC,GAAM,CAAEC,QAAuB,CAAA,CAChF,GAAI,CAAAC,KAAK,CAAGH,GAAG,CAACI,GAAG,CAACH,GAAG,CAAC,CAAA,MACpB,UAAAE,KAAmB,EACvBA,KAAK,CAAGD,QAAQ,CAACD,GAAG,CAAC,CACrBD,GAAG,CAACK,GAAG,CAACJ,GAAG,CAAEE,KAAK,CAAC,CACZA,KAAK,EAHoBA,KAIlC,CCNM,QAAU,CAAAG,UAAUA,CAAON,GAAc,CAAA,CAC7C,MAAO,IAAI,CAAAO,GAAG,CAAO,CAAC,GAAGP,GAAG,CAACQ,OAAO,EAAE,CAAC,CAACC,OAAO,CAAA,CAAE,CACnD,SCGgB,CAAAC,SAASA,CAAOV,GAAc,CAAEC,GAAM,CAAEU,GAAwC,CAAA,CAC9F,KAAM,CAAAR,KAAK,CAAGQ,GAAG,CAACX,GAAG,CAACI,GAAG,CAACH,GAAG,CAAC,CAAEA,GAAG,CAAC,CAEpC,MADA,CAAAD,GAAG,CAACK,GAAG,CAACJ,GAAG,CAAEE,KAAK,CAAC,CACZA,KACT"}
@@ -0,0 +1,4 @@
1
+ export * from './lib/mapGetOrElse';
2
+ export * from './lib/mapReverse';
3
+ export * from './lib/mapUpdate';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Returns a value from a map, while setting a given default value before returning it, if the key is not present.
3
+ * @param map map to get value from
4
+ * @param key key to get value for
5
+ * @param callback callback to set the value if key is not present
6
+ * @template K - The type of the keys in the Map.
7
+ * @returns The value retrieved from the Map or generated by the callback function.
8
+ * @example ```ts
9
+ * const map = new Map<string, number>()
10
+ * map.set('key', 1)
11
+ * mapGetOrElse(map, 'nonexistentKey', () => 2) // Output: 2
12
+ * ```
13
+ */
14
+ export declare function mapGetOrElse<K, V>(map: Map<K, V>, key: K, callback: (key: K) => V): V;
15
+ //# sourceMappingURL=mapGetOrElse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapGetOrElse.d.ts","sourceRoot":"","sources":["../../../src/lib/mapGetOrElse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAMrF"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Reverses the order of entries in a Map.
3
+ * @remarks This function creates a new Map with the entries of the original Map in reverse order.
4
+ * The original Map is not modified.
5
+ * @typeparam K - The type of the keys in the Map.
6
+ * @typeparam V - The type of the values in the Map.
7
+ * @param map The Map to reverse.
8
+ * @returns A new Map with the entries of the original Map in reverse order.
9
+ * @example ```ts
10
+ * mapReverse(new Map([['a', 1], ['b', 2], ['c', 3]]));;
11
+ * //=> [['c', 3], ['b', 2], ['a', 1]]
12
+ * ```
13
+ */
14
+ export declare function mapReverse<K, V>(map: Map<K, V>): Map<K, V>;
15
+ //# sourceMappingURL=mapReverse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapReverse.d.ts","sourceRoot":"","sources":["../../../src/lib/mapReverse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAE1D"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Updates the value for the given key in the map using the provided function.
3
+ * If the key does not exist in the map, the function will be called with `undefined` as the value.
4
+ * @template K - The type of the keys in the map.
5
+ * @param map - The map to update.
6
+ * @param key - The key of the value to update.
7
+ * @param fun - The function to use to update the value.
8
+ * This function takes the current value (or `undefined` if the key does not exist) and the key as arguments,
9
+ * and should return the new value.
10
+ * @returns - The new value for the key.
11
+ * @example ```ts
12
+ * const map = new Map<string, number>()
13
+ * map.set('a', 1)
14
+ * mapUpdate(map, 'a', (value, key) => value !== undefined ? value + 1 : 0)
15
+ * map.get('a')
16
+ * //=> 2
17
+ * ```
18
+ */
19
+ export declare function mapUpdate<K, V>(map: Map<K, V>, key: K, fun: (value: V | undefined, key: K) => V): V;
20
+ //# sourceMappingURL=mapUpdate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapUpdate.d.ts","sourceRoot":"","sources":["../../../src/lib/mapUpdate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAInG"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@bemoje/map",
3
+ "version": "0.0.1",
4
+ "description": "Map related tools/util.",
5
+ "main": "dist/index.cjs.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "scripts": {
9
+ "lint": "eslint \"*/**/*.{ts,js,json}\" --fix",
10
+ "test": "jest --preset ts-jest",
11
+ "build": "rimraf dist && rollup --config ./rollup.config.js --bundleConfigAsCjs",
12
+ "docs": "rimraf ../../docs/map && typedoc --out ../../docs/map --entryPoints src/index.ts",
13
+ "prepub": "npm run lint && npm run build && npm run test && npm run docs"
14
+ },
15
+ "author": {
16
+ "name": "Benjamin Møller Jensen",
17
+ "email": "bemoje@bemoje.net",
18
+ "url": "https://github.com/bemoje/"
19
+ },
20
+ "license": "MIT",
21
+ "keywords": [],
22
+ "dependencies": {},
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/bemoje/tsmono.git"
26
+ },
27
+ "funding": {
28
+ "type": "patreon",
29
+ "url": "https://www.patreon.com/user?u=40752770"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/bemoje/tsmono/issues"
33
+ },
34
+ "homepage": "https://github.com/bemoje/tsmono"
35
+ }
package/project.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "map",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "pkg/map/src",
5
+ "projectType": "library",
6
+ "targets": {},
7
+ "tags": []
8
+ }