From 9d1abb1eba15c02e8f04422066437068a220741c Mon Sep 17 00:00:00 2001 From: PythonCoderAS <13932583+PythonCoderAS@users.noreply.github.com> Date: Sat, 18 Dec 2021 11:53:34 -0500 Subject: [PATCH 1/6] Update security.md --- SECURITY.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SECURITY.md b/SECURITY.md index c077c23..ca081ac 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -6,8 +6,9 @@ Use this section to tell people about which versions of your project are currently being supported with security updates. | Version | Supported | -| ------- | ------------------ | -| 1.x. | :white_check_mark: | +|---------|--------------------| +| 2.x | :white_check_mark: | +| 1.x. | :x: | ## Reporting a Vulnerability From 2b30852a2b69cf5787ccc0979f82a645e945e73e Mon Sep 17 00:00:00 2001 From: PythonCoderAS <13932583+PythonCoderAS@users.noreply.github.com> Date: Sat, 18 Dec 2021 11:54:25 -0500 Subject: [PATCH 2/6] Update npmignore --- .npmignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.npmignore b/.npmignore index c0a6349..c1f7a3a 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ .github tsconfig.json -arrayStringMap.ts \ No newline at end of file +arrayStringMap.ts +*.test.* \ No newline at end of file From 81ae149bd06e2776b5863fb3917885f20149179b Mon Sep 17 00:00:00 2001 From: PythonCoderAS <13932583+PythonCoderAS@users.noreply.github.com> Date: Sat, 18 Dec 2021 15:40:19 -0500 Subject: [PATCH 3/6] Make back into a cjs package --- README.md | 28 +++++++++++++++++++++++++++- arrayStringMap.test.ts | 2 +- package-lock.json | 20 ++++++++++++++++++-- package.json | 3 ++- tsconfig.json | 2 +- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2ad1cc8..fb0c63a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # array-string-map + ![node-current](https://img.shields.io/node/v/array-string-map) [![npm](https://img.shields.io/npm/v/array-string-map)](https://www.npmjs.com/package/array-string-map) [![npm](https://img.shields.io/npm/dt/array-string-map)](https://www.npmjs.com/package/array-string-map) @@ -9,4 +10,29 @@ A Map that internally encodes Arrays to strings so that two Arrays with the same ## Motivation -I was using a Map to store array values, and I kept on running into a bug where I could not get those values back. I got fed up, and then after some Googling, realized that array lookups will not work when two different arrays with the same elements are used (such as when I use array literal syntax to create them). My solution was to encode the values as string keys. +I was using a Map to store array values, and I kept on running into a bug where I could not get those values back. I got +fed up, and then after some Googling, realized that array lookups will not work when two different arrays with the same +elements are used (such as when I use array literal syntax to create them). My solution was to encode the values as +string keys. + +## Usage + +ESM modules or TypeScript scripts can just import the normal way, such as: + +```ts +import ArrayStringMap from 'array-string-map' +``` + +However, for CommonJS modules, you need to use the `require` function, such as: + +```js +const {default: ArrayStringMap} = require("./arrayStringMap") +``` + +## API + +### ArrayStringMap + +A class that implements the [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) +interface. All methods that can be used on a Map are available on the ArrayStringMap. + diff --git a/arrayStringMap.test.ts b/arrayStringMap.test.ts index 021656d..dfeb30a 100644 --- a/arrayStringMap.test.ts +++ b/arrayStringMap.test.ts @@ -1,5 +1,5 @@ import { assert } from "chai" -import ArrayStringMap from "./arrayStringMap.js" +import ArrayStringMap from "./arrayStringMap" type TwoNumberArray = [number, number] diff --git a/package-lock.json b/package-lock.json index 7c72f7f..b344849 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,23 @@ { "name": "array-string-map", - "version": "1.0.1", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "array-string-map", - "version": "1.0.1", + "version": "2.0.0", "license": "GNU General Public License v3.0", "devDependencies": { "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", + "@types/node": "^17.0.0", "chai": "^4.3.4", "mocha": "^9.1.3", "typescript": "^4.5.4" + }, + "engines": { + "node": ">=8.X.X" } }, "node_modules/@types/chai": { @@ -28,6 +32,12 @@ "integrity": "sha512-scN0hAWyLVAvLR9AyW7HoFF5sJZglyBsbPuHO4fv7JRvfmPBMfp1ozWqOf/e4wwPNxezBZXRfWzMb6iFLgEVRA==", "dev": true }, + "node_modules/@types/node": { + "version": "17.0.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.0.tgz", + "integrity": "sha512-eMhwJXc931Ihh4tkU+Y7GiLzT/y/DBNpNtr4yU9O2w3SYBsr9NaOPhQlLKRmoWtI54uNwuo0IOUFQjVOTZYRvw==", + "dev": true + }, "node_modules/@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", @@ -1027,6 +1037,12 @@ "integrity": "sha512-scN0hAWyLVAvLR9AyW7HoFF5sJZglyBsbPuHO4fv7JRvfmPBMfp1ozWqOf/e4wwPNxezBZXRfWzMb6iFLgEVRA==", "dev": true }, + "@types/node": { + "version": "17.0.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.0.tgz", + "integrity": "sha512-eMhwJXc931Ihh4tkU+Y7GiLzT/y/DBNpNtr4yU9O2w3SYBsr9NaOPhQlLKRmoWtI54uNwuo0IOUFQjVOTZYRvw==", + "dev": true + }, "@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", diff --git a/package.json b/package.json index d9fc8ec..c126d61 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "array-string-map", "version": "2.0.0", "description": "A Map that internally encodes Arrays to strings so that two Arrays with the same elements will return the same item.", - "type": "module", + "type": "commonjs", "main": "arrayStringMap.js", "types": "arrayStringMap.d.ts", "scripts": { @@ -29,6 +29,7 @@ "devDependencies": { "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", + "@types/node": "^17.0.0", "chai": "^4.3.4", "mocha": "^9.1.3", "typescript": "^4.5.4" diff --git a/tsconfig.json b/tsconfig.json index ea61249..ff2d5e1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "esnext", + "module": "commonjs", "target": "es2019", "strict": true, "allowJs": true, From 9aa195333bbf8eaf234dec06fbc832f5e28814b1 Mon Sep 17 00:00:00 2001 From: PythonCoderAS <13932583+PythonCoderAS@users.noreply.github.com> Date: Sat, 18 Dec 2021 15:44:55 -0500 Subject: [PATCH 4/6] Add more tests --- arrayStringMap.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ arrayStringMap.ts | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/arrayStringMap.test.ts b/arrayStringMap.test.ts index 021656d..f9aaa42 100644 --- a/arrayStringMap.test.ts +++ b/arrayStringMap.test.ts @@ -31,6 +31,11 @@ describe("Empty map", () => { it ("Empty map entries returns empty array", () => { assert([...arrayStringMap.entries()].length === 0); }) + it ("Empty map forEach", () => { + arrayStringMap.forEach(() => { + assert(false, "forEach should not be called"); + }); + }) }) describe("Map with one object", () => { @@ -97,6 +102,16 @@ describe("Map with one object", () => { // works as expected assert(copiedMap._converterInfo.size === 0, "Converter map size is 0"); }) + it ("Map forEach is called once", () => { + let count = 0; + arrayStringMap.forEach((value, key, map) => { + count++; + assert(value === sampleValue1, "Value is sampleValue1"); + assert(key === sampleArray1, "Key is sampleArray1"); + assert(map === arrayStringMap, "Map is arrayStringMap"); + }); + assert(count === 1, "ForEach is called once"); + }) }) describe("Map with one object and different separator", () => { @@ -138,6 +153,16 @@ describe("Map with one object and alternate array", () => { assert([...arrayStringMap.values()].length === 1, "Array length is 1"); assert([...arrayStringMap.values()][0] === sampleValue2, "Value is sampleValue2"); }) + it ("Map forEach is called once", () => { + let count = 0; + arrayStringMap.forEach((value, key, map) => { + count++; + assert(value === sampleValue2, "Value is sampleValue2"); + assert(key === sampleArray2, "Key is sampleArray2"); + assert(map === arrayStringMap, "Map is arrayStringMap"); + }); + assert(count === 1, "ForEach is called once"); + }) }) describe("Map with two objects", () => { @@ -222,4 +247,20 @@ describe("Map with two objects", () => { // works as expected assert(copiedMap._converterInfo.size === 1, "Converter map size is 1"); }) + it("Map forEach is called twice", () => { + let count = 0; + arrayStringMap.forEach((value, key, map) => { + count++; + assert(map === arrayStringMap, "Map is arrayStringMap"); + if (count === 0){ + assert(key === sampleArray1, "Key is sampleArray1"); + assert(value === sampleValue1, "Value is sampleValue1"); + } + else if (count === 1){ + assert(key === sampleArray3, "Key is sampleArray3"); + assert(value === sampleValue2, "Value is sampleValue2"); + } + }); + assert(count === 2, "ForEach is called twice"); + }) }) \ No newline at end of file diff --git a/arrayStringMap.ts b/arrayStringMap.ts index 2b58681..c0fe4f2 100644 --- a/arrayStringMap.ts +++ b/arrayStringMap.ts @@ -50,7 +50,7 @@ export default class ArrayStringMap implements Map { } } - forEach(callbackfn: (value: V, key: K, map: Map) => void, thisArg?: any): void { + forEach(callbackfn: (value: V, key: K, map: ArrayStringMap) => void, thisArg?: any): void { this._converterInfo.forEach((value, key) => { // TypeScript complains that this will be undefined, but the items in // `this._converterInfo` and `this._map` will always be defined in each other. From a237cf29aa45cde504a8c79d15a080f6ab7c36e5 Mon Sep 17 00:00:00 2001 From: PythonCoderAS <13932583+PythonCoderAS@users.noreply.github.com> Date: Sat, 18 Dec 2021 15:51:58 -0500 Subject: [PATCH 5/6] Add alternate implementation of forEach --- arrayStringMap.test.ts | 2 +- arrayStringMap.ts | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/arrayStringMap.test.ts b/arrayStringMap.test.ts index 8835b39..6688f77 100644 --- a/arrayStringMap.test.ts +++ b/arrayStringMap.test.ts @@ -250,7 +250,6 @@ describe("Map with two objects", () => { it("Map forEach is called twice", () => { let count = 0; arrayStringMap.forEach((value, key, map) => { - count++; assert(map === arrayStringMap, "Map is arrayStringMap"); if (count === 0){ assert(key === sampleArray1, "Key is sampleArray1"); @@ -260,6 +259,7 @@ describe("Map with two objects", () => { assert(key === sampleArray3, "Key is sampleArray3"); assert(value === sampleValue2, "Value is sampleValue2"); } + count++; }); assert(count === 2, "ForEach is called twice"); }) diff --git a/arrayStringMap.ts b/arrayStringMap.ts index c0fe4f2..543eb4f 100644 --- a/arrayStringMap.ts +++ b/arrayStringMap.ts @@ -51,11 +51,9 @@ export default class ArrayStringMap implements Map { } forEach(callbackfn: (value: V, key: K, map: ArrayStringMap) => void, thisArg?: any): void { - this._converterInfo.forEach((value, key) => { - // TypeScript complains that this will be undefined, but the items in - // `this._converterInfo` and `this._map` will always be defined in each other. - return callbackfn(this._map.get(key)!, value, thisArg); - }); + for (const [key, value] of this.entries()) { + callbackfn.call(thisArg, value, key, this); + } } get(key: K): V | undefined { From 14e7539c8d43f655d2d5cdbd46f821e7a88b508c Mon Sep 17 00:00:00 2001 From: PythonCoderAS <13932583+PythonCoderAS@users.noreply.github.com> Date: Sat, 18 Dec 2021 15:54:37 -0500 Subject: [PATCH 6/6] Version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c126d61..5b225e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "array-string-map", - "version": "2.0.0", + "version": "3.0.0", "description": "A Map that internally encodes Arrays to strings so that two Arrays with the same elements will return the same item.", "type": "commonjs", "main": "arrayStringMap.js",