Skip to content

Commit

Permalink
Merge 5310c91 into c8a061d
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Aug 16, 2018
2 parents c8a061d + 5310c91 commit 01d95a3
Show file tree
Hide file tree
Showing 8 changed files with 2,032 additions and 20 deletions.
40 changes: 22 additions & 18 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ env:
node: true

globals:
Map: false
Set: false
Symbol: false

plugins:
- ie11
Expand All @@ -21,21 +23,23 @@ rules:
ie11/no-weak-collections: error

overrides:
files: '*.test.*'
plugins:
- mocha
env:
mocha: true
rules:
mocha/handle-done-callback: error
mocha/no-exclusive-tests: error
mocha/no-global-tests: error
mocha/no-hooks-for-single-case: off
mocha/no-identical-title: error
mocha/no-mocha-arrows: error
mocha/no-nested-tests: error
mocha/no-return-and-callback: error
mocha/no-sibling-hooks: error
mocha/no-skipped-tests: error
mocha/no-top-level-hooks: error

files: '*.test.*'
plugins:
- mocha
env:
mocha: true
rules:
max-nested-callbacks:
- warn
- 6
mocha/handle-done-callback: error
mocha/no-exclusive-tests: error
mocha/no-global-tests: error
mocha/no-hooks-for-single-case: off
mocha/no-identical-title: error
mocha/no-mocha-arrows: error
mocha/no-nested-tests: error
mocha/no-return-and-callback: error
mocha/no-sibling-hooks: error
mocha/no-skipped-tests: error
mocha/no-top-level-hooks: error
18 changes: 17 additions & 1 deletion lib/deep-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var keys = Object.keys;
*
* Supports cyclic objects.
*/
function deepEqualCyclic(first, second) {
function deepEqualCyclic(first, second, match) {
// used for cyclic comparison
// contain already visited objects
var objects1 = [];
Expand All @@ -47,6 +47,16 @@ function deepEqualCyclic(first, second) {

// does the recursion for the deep equal check
return (function deepEqual(obj1, obj2, path1, path2) {
// If both are matchers they must be the same instance in order to be
// considered equal If we didn't do that we would end up running one
// matcher against the other
if (match && match.isMatcher(obj1)) {
if (match.isMatcher(obj2)) {
return obj1 === obj2;
}
return obj1.test(obj2);
}

var type1 = typeof obj1;
var type2 = typeof obj2;

Expand Down Expand Up @@ -176,4 +186,10 @@ function deepEqualCyclic(first, second) {
})(first, second, "$1", "$2");
}

deepEqualCyclic.use = function(match) {
return function(a, b) {
return deepEqualCyclic(a, b, match);
};
};

module.exports = deepEqualCyclic;
39 changes: 39 additions & 0 deletions lib/iterable-to-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

var slice = require("@sinonjs/commons").prototypes.string.slice;
var typeOf = require("@sinonjs/commons").typeOf;

module.exports = function iterableToString(obj) {
var representation = "";

function stringify(item) {
return typeof item === "string" ? "'" + item + "'" : String(item);
}

function mapToString(map) {
/* eslint-disable-next-line local-rules/no-prototype-methods */
map.forEach(function(value, key) {
representation +=
"[" + stringify(key) + "," + stringify(value) + "],";
});

representation = slice(representation, 0, -1);
return representation;
}

function genericIterableToString(iterable) {
/* eslint-disable-next-line local-rules/no-prototype-methods */
iterable.forEach(function(value) {
representation += stringify(value) + ",";
});

representation = slice(representation, 0, -1);
return representation;
}

if (typeOf(obj) === "map") {
return mapToString(obj);
}

return genericIterableToString(obj);
};
47 changes: 47 additions & 0 deletions lib/iterable-to-string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";

var assert = require("@sinonjs/referee").assert;
var iterableToString = require("./iterable-to-string");

describe("iterableToString", function() {
it("returns an String representation of Array objects", function() {
var arr = [1, "one", true, undefined, null];
var expected = "1,'one',true,undefined,null";

assert.equals(iterableToString(arr), expected);
});

if (typeof Map === "function") {
it("returns an String representation of Map objects", function() {
var map = new Map();
map.set(1, 1);
map.set("one", "one");
map.set(true, true);
map.set(undefined, undefined);
map.set(null, null);
var expected =
"[1,1]," +
"['one','one']," +
"[true,true]," +
"[undefined,undefined]," +
"[null,null]";

assert.equals(iterableToString(map), expected);
});
}

if (typeof Set === "function") {
it("returns an String representation of Set objects", function() {
var set = new Set();
set.add(1);
set.add("one");
set.add(true);
set.add(undefined);
set.add(null);

var expected = "1,'one',true,undefined,null";

assert.equals(iterableToString(set), expected);
});
}
});

0 comments on commit 01d95a3

Please sign in to comment.