Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions map/compare.js
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions string/isJSON.js
Original file line number Diff line number Diff line change
@@ -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 <caption>isJSON</caption>
* 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;
}
}
41 changes: 41 additions & 0 deletions test/specs/map/compare.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

});
26 changes: 26 additions & 0 deletions test/specs/string/isJSON.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});