From c00158da1229c0de83ee875ae43461a5761be9ba Mon Sep 17 00:00:00 2001 From: Andrew Dodson Date: Wed, 2 Jul 2025 22:01:48 +0100 Subject: [PATCH 1/2] feat(mapCompare): adds map/compare with new tests to isJSON --- map/compare.js | 19 +++++++++++++++ string/isJSON.js | 26 ++++++++++++++++++++ test/specs/map/compare.spec.js | 41 ++++++++++++++++++++++++++++++++ test/specs/string/isJSON.spec.js | 26 ++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 map/compare.js create mode 100644 string/isJSON.js create mode 100644 test/specs/map/compare.spec.js create mode 100644 test/specs/string/isJSON.spec.js diff --git a/map/compare.js b/map/compare.js new file mode 100644 index 0000000..b70c6cb --- /dev/null +++ b/map/compare.js @@ -0,0 +1,19 @@ +/** + * Compare two Map objects for equality + * @param {Map} map1 - The first map to compare + * @param {Map} map2 - The second map to compare + * @returns {boolean} - True if the maps are equal, false otherwise + */ +export default function mapCompare(map1, map2) { + if (map1.size !== map2.size) { + return false; // Different sizes, cannot be equal + } + + for (const [key, value] of map1.entries()) { + if (!map2.has(key) || map2.get(key) !== value) { + return false; // Key or value mismatch + } + } + + return true; // Maps are equal +} diff --git a/string/isJSON.js b/string/isJSON.js new file mode 100644 index 0000000..d21e0be --- /dev/null +++ b/string/isJSON.js @@ -0,0 +1,26 @@ +/** + * isJSON + * Determines whether the input string is a valid JSON string + * @param {string} str - String to parse + * @returns {boolean} Is JSON string or not + * + * @example isJSON + * isJSON(null) + * // false + * isJSON('{"name": "John", "age": 30}') + * // true + * isJSON('{"name": "John", "age": 30,}') + * // false + */ +export default function strIsJSON(str) { + if (typeof str !== 'string') { + return false; + } + try { + JSON.parse(str); + return true; + } + catch { + return false; + } +} diff --git a/test/specs/map/compare.spec.js b/test/specs/map/compare.spec.js new file mode 100644 index 0000000..4a74082 --- /dev/null +++ b/test/specs/map/compare.spec.js @@ -0,0 +1,41 @@ +import mapCompare from '../../../map/compare.js'; + +describe('map/compare', () => { + + [ + [ + [], + [], + ], + [ + [['a', '1'], ['b', null], [1, 2]], + [['a', '1'], ['b', null], [1, 2]], + ], + ].forEach(([a, b]) => { + it(`should compare two maps with values: ${JSON.stringify(a)}`, () => { + const map1 = new Map(a); + const map2 = new Map(b); + const result = mapCompare(map1, map2); + expect(result).to.eql(true); + }); + }); + + [ + [ + [[1, '1']], + [], + ], + [ + [['a', '1'], ['b', null], [1, 2]], + [['a', '1'], ['b', false], [1, 2]], + ], + ].forEach(([a, b]) => { + it(`should reject two maps with differing values: ${JSON.stringify(a)}`, () => { + const map1 = new Map(a); + const map2 = new Map(b); + const result = mapCompare(map1, map2); + expect(result).to.eql(false); + }); + }); + +}); diff --git a/test/specs/string/isJSON.spec.js b/test/specs/string/isJSON.spec.js new file mode 100644 index 0000000..9e94c2a --- /dev/null +++ b/test/specs/string/isJSON.spec.js @@ -0,0 +1,26 @@ +import isJSON from '../../../string/isJSON.js'; + +describe('string/isJSON', () => { + + it('should accept a string and return an object', () => { + + const json = { + test: 1 + }; + + const str = JSON.stringify(json); + + const test = isJSON(str); + + expect(test).to.eql(true); + }); + + it('should return undefined if json is invalid', () => { + + // Convert there and back + + const test = isJSON('invalid json'); + + expect(test).to.eql(false); + }); +}); From c43ccf27eec75500094fdc43e78f21ad9fcd85e8 Mon Sep 17 00:00:00 2001 From: Andrew Dodson Date: Wed, 2 Jul 2025 22:29:24 +0100 Subject: [PATCH 2/2] test(map): add map directory to node tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e057e10..df247c0 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "semantic-release": "semantic-release -d false", "test": "npm run lint && npm run test:node && npm run test:browser", "test:browser": "gulp", - "test:node": "mocha ./test/specs/{array,time,object,string,http}/*.js", + "test:node": "mocha ./test/specs/{array,http,map,object,string,time}/*.js", "test:spawn": "node_modules/.bin/browserstack-runner", "test:deploy": "npm test && npm run test:spawn", "test:spawn-local": "env $(cat .env | xargs) npm run test:spawn"