Skip to content

Commit 5630c02

Browse files
authored
feat(mapCompare): adds map/compare with new tests to isJSON (#172)
* feat(mapCompare): adds map/compare with new tests to isJSON * test(map): add map directory to node tests
1 parent 628d518 commit 5630c02

5 files changed

Lines changed: 113 additions & 1 deletion

File tree

map/compare.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Compare two Map objects for equality
3+
* @param {Map} map1 - The first map to compare
4+
* @param {Map} map2 - The second map to compare
5+
* @returns {boolean} - True if the maps are equal, false otherwise
6+
*/
7+
export default function mapCompare(map1, map2) {
8+
if (map1.size !== map2.size) {
9+
return false; // Different sizes, cannot be equal
10+
}
11+
12+
for (const [key, value] of map1.entries()) {
13+
if (!map2.has(key) || map2.get(key) !== value) {
14+
return false; // Key or value mismatch
15+
}
16+
}
17+
18+
return true; // Maps are equal
19+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"semantic-release": "semantic-release -d false",
1717
"test": "npm run lint && npm run test:node && npm run test:browser",
1818
"test:browser": "gulp",
19-
"test:node": "mocha ./test/specs/{array,time,object,string,http}/*.js",
19+
"test:node": "mocha ./test/specs/{array,http,map,object,string,time}/*.js",
2020
"test:spawn": "node_modules/.bin/browserstack-runner",
2121
"test:deploy": "npm test && npm run test:spawn",
2222
"test:spawn-local": "env $(cat .env | xargs) npm run test:spawn"

string/isJSON.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* isJSON
3+
* Determines whether the input string is a valid JSON string
4+
* @param {string} str - String to parse
5+
* @returns {boolean} Is JSON string or not
6+
*
7+
* @example <caption>isJSON</caption>
8+
* isJSON(null)
9+
* // false
10+
* isJSON('{"name": "John", "age": 30}')
11+
* // true
12+
* isJSON('{"name": "John", "age": 30,}')
13+
* // false
14+
*/
15+
export default function strIsJSON(str) {
16+
if (typeof str !== 'string') {
17+
return false;
18+
}
19+
try {
20+
JSON.parse(str);
21+
return true;
22+
}
23+
catch {
24+
return false;
25+
}
26+
}

test/specs/map/compare.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import mapCompare from '../../../map/compare.js';
2+
3+
describe('map/compare', () => {
4+
5+
[
6+
[
7+
[],
8+
[],
9+
],
10+
[
11+
[['a', '1'], ['b', null], [1, 2]],
12+
[['a', '1'], ['b', null], [1, 2]],
13+
],
14+
].forEach(([a, b]) => {
15+
it(`should compare two maps with values: ${JSON.stringify(a)}`, () => {
16+
const map1 = new Map(a);
17+
const map2 = new Map(b);
18+
const result = mapCompare(map1, map2);
19+
expect(result).to.eql(true);
20+
});
21+
});
22+
23+
[
24+
[
25+
[[1, '1']],
26+
[],
27+
],
28+
[
29+
[['a', '1'], ['b', null], [1, 2]],
30+
[['a', '1'], ['b', false], [1, 2]],
31+
],
32+
].forEach(([a, b]) => {
33+
it(`should reject two maps with differing values: ${JSON.stringify(a)}`, () => {
34+
const map1 = new Map(a);
35+
const map2 = new Map(b);
36+
const result = mapCompare(map1, map2);
37+
expect(result).to.eql(false);
38+
});
39+
});
40+
41+
});

test/specs/string/isJSON.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import isJSON from '../../../string/isJSON.js';
2+
3+
describe('string/isJSON', () => {
4+
5+
it('should accept a string and return an object', () => {
6+
7+
const json = {
8+
test: 1
9+
};
10+
11+
const str = JSON.stringify(json);
12+
13+
const test = isJSON(str);
14+
15+
expect(test).to.eql(true);
16+
});
17+
18+
it('should return undefined if json is invalid', () => {
19+
20+
// Convert there and back
21+
22+
const test = isJSON('invalid json');
23+
24+
expect(test).to.eql(false);
25+
});
26+
});

0 commit comments

Comments
 (0)