Socket
Socket
Sign inDemoInstall

@sinonjs/samsam

Package Overview
Dependencies
Maintainers
5
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sinonjs/samsam - npm Package Compare versions

Comparing version 2.1.3 to 3.0.0

lib/iterable-to-string.js

607

dist/samsam.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.samsam = {})));
}(this, (function (exports) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@sinonjs/commons'), require('lodash.get')) :
typeof define === 'function' && define.amd ? define(['exports', '@sinonjs/commons', 'lodash.get'], factory) :
(factory((global.samsam = {}),global.commons,global.lodash));
}(this, (function (exports,commons,lodash) { 'use strict';
var o = Object.prototype;
commons = commons && commons.hasOwnProperty('default') ? commons['default'] : commons;
lodash = lodash && lodash.hasOwnProperty('default') ? lodash['default'] : lodash;
function getClass(value) {
// Returns the internal [[Class]] by calling Object.prototype.toString
// with the provided value as this. Return value is a string, naming the
// internal class, e.g. "Array"
return o.toString.call(value).split(/[ \]]/)[1];
}
var getClass_1 = getClass;
function isNaN(value) {

@@ -61,2 +53,13 @@ // Unlike global isNaN, this avoids type coercion

var o = Object.prototype;
function getClass(value) {
// Returns the internal [[Class]] by calling Object.prototype.toString
// with the provided value as this. Return value is a string, naming the
// internal class, e.g. "Array"
return o.toString.call(value).split(/[ \]]/)[1];
}
var getClass_1 = getClass;
/**

@@ -94,8 +97,2 @@ * @name samsam.isArguments

function isDate(value) {
return value instanceof Date;
}
var isDate_1 = isDate;
var div = typeof document !== "undefined" && document.createElement("div");

@@ -127,2 +124,8 @@

function isDate(value) {
return value instanceof Date;
}
var isDate_1 = isDate;
// Returns true when the value is a regular Object and not a specialized Object

@@ -214,3 +217,3 @@ //

*/
function deepEqualCyclic(first, second) {
function deepEqualCyclic(first, second, match) {
// used for cyclic comparison

@@ -231,2 +234,12 @@ // contain already visited objects

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(obj2)) {
if (match.isMatcher(obj1)) {
return obj1 === obj2;
}
return obj2.test(obj1);
}
var type1 = typeof obj1;

@@ -368,4 +381,510 @@ var type2 = typeof obj2;

deepEqualCyclic.use = function(match) {
return function(a, b) {
return deepEqualCyclic(a, b, match);
};
};
var deepEqual = deepEqualCyclic;
var slice = commons.prototypes.string.slice;
var typeOf = commons.typeOf;
var iterableToString = 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);
};
var arrayProto = commons.prototypes.array;
var deepEqual$1 = deepEqual.use(match); // eslint-disable-line no-use-before-define
var every$1 = commons.every;
var functionName = commons.functionName;
var objectProto = commons.prototypes.object;
var stringProto = commons.prototypes.string;
var typeOf$1 = commons.typeOf;
var valueToString = commons.valueToString;
var arrayIndexOf = arrayProto.indexOf;
var arrayEvery = arrayProto.every;
var join = arrayProto.join;
var map = arrayProto.map;
var some = arrayProto.some;
var hasOwnProperty$1 = objectProto.hasOwnProperty;
var isPrototypeOf = objectProto.isPrototypeOf;
var stringIndexOf = stringProto.indexOf;
function assertType(value, type, name) {
var actual = typeOf$1(value);
if (actual !== type) {
throw new TypeError(
"Expected type of " +
name +
" to be " +
type +
", but was " +
actual
);
}
}
function assertMethodExists(value, method, name, methodPath) {
if (value[method] == null) {
throw new TypeError(
"Expected " + name + " to have method " + methodPath
);
}
}
var matcher = {
toString: function() {
return this.message;
}
};
function isMatcher(object) {
return isPrototypeOf(matcher, object);
}
function matchObject(actual, expectation) {
if (actual === null || actual === undefined) {
return false;
}
return arrayEvery(Object.keys(expectation), function(key) {
var exp = expectation[key];
var act = actual[key];
if (isMatcher(exp)) {
if (!exp.test(act)) {
return false;
}
} else if (typeOf$1(exp) === "object") {
if (!matchObject(act, exp)) {
return false;
}
} else if (!deepEqual$1(act, exp)) {
return false;
}
return true;
});
}
var TYPE_MAP = {
function: function(m, expectation, message) {
m.test = expectation;
m.message = message || "match(" + functionName(expectation) + ")";
},
number: function(m, expectation) {
m.test = function(actual) {
// we need type coercion here
return expectation == actual; // eslint-disable-line eqeqeq
};
},
object: function(m, expectation) {
var array = [];
if (typeof expectation.test === "function") {
m.test = function(actual) {
return expectation.test(actual) === true;
};
m.message = "match(" + functionName(expectation.test) + ")";
return m;
}
array = map(Object.keys(expectation), function(key) {
return key + ": " + valueToString(expectation[key]);
});
m.test = function(actual) {
return matchObject(actual, expectation);
};
m.message = "match(" + join(array, ", ") + ")";
return m;
},
regexp: function(m, expectation) {
m.test = function(actual) {
return typeof actual === "string" && expectation.test(actual);
};
},
string: function(m, expectation) {
m.test = function(actual) {
return (
typeof actual === "string" &&
stringIndexOf(actual, expectation) !== -1
);
};
m.message = 'match("' + expectation + '")';
}
};
function match(expectation, message) {
var m = Object.create(matcher);
var type = typeOf$1(expectation);
if (type in TYPE_MAP) {
TYPE_MAP[type](m, expectation, message);
} else {
m.test = function(actual) {
return deepEqual$1(actual, expectation);
};
}
if (!m.message) {
m.message = "match(" + valueToString(expectation) + ")";
}
return m;
}
matcher.or = function(m2) {
if (!arguments.length) {
throw new TypeError("Matcher expected");
} else if (!isMatcher(m2)) {
m2 = match(m2);
}
var m1 = this;
var or = Object.create(matcher);
or.test = function(actual) {
return m1.test(actual) || m2.test(actual);
};
or.message = m1.message + ".or(" + m2.message + ")";
return or;
};
matcher.and = function(m2) {
if (!arguments.length) {
throw new TypeError("Matcher expected");
} else if (!isMatcher(m2)) {
m2 = match(m2);
}
var m1 = this;
var and = Object.create(matcher);
and.test = function(actual) {
return m1.test(actual) && m2.test(actual);
};
and.message = m1.message + ".and(" + m2.message + ")";
return and;
};
match.isMatcher = isMatcher;
match.any = match(function() {
return true;
}, "any");
match.defined = match(function(actual) {
return actual !== null && actual !== undefined;
}, "defined");
match.truthy = match(function(actual) {
return !!actual;
}, "truthy");
match.falsy = match(function(actual) {
return !actual;
}, "falsy");
match.same = function(expectation) {
return match(function(actual) {
return expectation === actual;
}, "same(" + valueToString(expectation) + ")");
};
match.in = function(arrayOfExpectations) {
if (!Array.isArray(arrayOfExpectations)) {
throw new TypeError("array expected");
}
return match(function(actual) {
return some(arrayOfExpectations, function(expectation) {
return expectation === actual;
});
}, "in(" + valueToString(arrayOfExpectations) + ")");
};
match.typeOf = function(type) {
assertType(type, "string", "type");
return match(function(actual) {
return typeOf$1(actual) === type;
}, 'typeOf("' + type + '")');
};
match.instanceOf = function(type) {
if (
typeof Symbol === "undefined" ||
typeof Symbol.hasInstance === "undefined"
) {
assertType(type, "function", "type");
} else {
assertMethodExists(
type,
Symbol.hasInstance,
"type",
"[Symbol.hasInstance]"
);
}
return match(function(actual) {
return actual instanceof type;
}, "instanceOf(" +
(functionName(type) || Object.prototype.toString.call(type)) +
")");
};
function createPropertyMatcher(propertyTest, messagePrefix) {
return function(property, value) {
assertType(property, "string", "property");
var onlyProperty = arguments.length === 1;
var message = messagePrefix + '("' + property + '"';
if (!onlyProperty) {
message += ", " + valueToString(value);
}
message += ")";
return match(function(actual) {
if (
actual === undefined ||
actual === null ||
!propertyTest(actual, property)
) {
return false;
}
return onlyProperty || deepEqual$1(actual[property], value);
}, message);
};
}
match.has = createPropertyMatcher(function(actual, property) {
if (typeof actual === "object") {
return property in actual;
}
return actual[property] !== undefined;
}, "has");
match.hasOwn = createPropertyMatcher(function(actual, property) {
return hasOwnProperty$1(actual, property);
}, "hasOwn");
match.hasNested = function(property, value) {
assertType(property, "string", "property");
var onlyProperty = arguments.length === 1;
var message = 'hasNested("' + property + '"';
if (!onlyProperty) {
message += ", " + valueToString(value);
}
message += ")";
return match(function(actual) {
if (
actual === undefined ||
actual === null ||
lodash(actual, property) === undefined
) {
return false;
}
return onlyProperty || deepEqual$1(lodash(actual, property), value);
}, message);
};
match.every = function(predicate) {
if (!isMatcher(predicate)) {
throw new TypeError("Matcher expected");
}
return match(function(actual) {
if (typeOf$1(actual) === "object") {
return every$1(Object.keys(actual), function(key) {
return predicate.test(actual[key]);
});
}
return (
!!actual &&
typeOf$1(actual.forEach) === "function" &&
every$1(actual, function(element) {
return predicate.test(element);
})
);
}, "every(" + predicate.message + ")");
};
match.some = function(predicate) {
if (!isMatcher(predicate)) {
throw new TypeError("Matcher expected");
}
return match(function(actual) {
if (typeOf$1(actual) === "object") {
return !every$1(Object.keys(actual), function(key) {
return !predicate.test(actual[key]);
});
}
return (
!!actual &&
typeOf$1(actual.forEach) === "function" &&
!every$1(actual, function(element) {
return !predicate.test(element);
})
);
}, "some(" + predicate.message + ")");
};
match.array = match.typeOf("array");
match.array.deepEquals = function(expectation) {
return match(function(actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.length === expectation.length;
return (
typeOf$1(actual) === "array" &&
sameLength &&
every$1(actual, function(element, index) {
return expectation[index] === element;
})
);
}, "deepEquals([" + iterableToString(expectation) + "])");
};
match.array.startsWith = function(expectation) {
return match(function(actual) {
return (
typeOf$1(actual) === "array" &&
every$1(expectation, function(expectedElement, index) {
return actual[index] === expectedElement;
})
);
}, "startsWith([" + iterableToString(expectation) + "])");
};
match.array.endsWith = function(expectation) {
return match(function(actual) {
// This indicates the index in which we should start matching
var offset = actual.length - expectation.length;
return (
typeOf$1(actual) === "array" &&
every$1(expectation, function(expectedElement, index) {
return actual[offset + index] === expectedElement;
})
);
}, "endsWith([" + iterableToString(expectation) + "])");
};
match.array.contains = function(expectation) {
return match(function(actual) {
return (
typeOf$1(actual) === "array" &&
every$1(expectation, function(expectedElement) {
return arrayIndexOf(actual, expectedElement) !== -1;
})
);
}, "contains([" + iterableToString(expectation) + "])");
};
match.map = match.typeOf("map");
match.map.deepEquals = function mapDeepEquals(expectation) {
return match(function(actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.size === expectation.size;
return (
typeOf$1(actual) === "map" &&
sameLength &&
every$1(actual, function(element, key) {
return expectation.has(key) && expectation.get(key) === element;
})
);
}, "deepEquals(Map[" + iterableToString(expectation) + "])");
};
match.map.contains = function mapContains(expectation) {
return match(function(actual) {
return (
typeOf$1(actual) === "map" &&
every$1(expectation, function(element, key) {
return actual.has(key) && actual.get(key) === element;
})
);
}, "contains(Map[" + iterableToString(expectation) + "])");
};
match.set = match.typeOf("set");
match.set.deepEquals = function setDeepEquals(expectation) {
return match(function(actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.size === expectation.size;
return (
typeOf$1(actual) === "set" &&
sameLength &&
every$1(actual, function(element) {
return expectation.has(element);
})
);
}, "deepEquals(Set[" + iterableToString(expectation) + "])");
};
match.set.contains = function setContains(expectation) {
return match(function(actual) {
return (
typeOf$1(actual) === "set" &&
every$1(expectation, function(element) {
return actual.has(element);
})
);
}, "contains(Set[" + iterableToString(expectation) + "])");
};
match.bool = match.typeOf("boolean");
match.number = match.typeOf("number");
match.string = match.typeOf("string");
match.object = match.typeOf("object");
match.func = match.typeOf("function");
match.regexp = match.typeOf("regexp");
match.date = match.typeOf("date");
match.symbol = match.typeOf("symbol");
var matcher_1 = match;
var deepEqual$2 = deepEqual.use(match$1); // eslint-disable-line no-use-before-define
function arrayContains(array, subset, compare) {

@@ -399,3 +918,3 @@ if (subset.length === 0) {

*/
function match(object, matcher) {
function match$1(object, matcher) {
if (matcher && typeof matcher.test === "function") {

@@ -441,7 +960,7 @@ return matcher.test(object);

if (isSet_1(object)) {
return isSubset_1(matcher, object, match);
return isSubset_1(matcher, object, match$1);
}
if (getClass_1(object) === "Array" && getClass_1(matcher) === "Array") {
return arrayContains(object, matcher, match);
return arrayContains(object, matcher, match$1);
}

@@ -479,3 +998,3 @@

typeof value === "undefined" ||
!match(value, matcher[prop])
!deepEqual$2(value, matcher[prop])
) {

@@ -494,5 +1013,14 @@ return false;

var match_1 = match;
Object.keys(matcher_1).forEach(function(key) {
match$1[key] = matcher_1[key];
});
var match_1 = match$1;
var deepEqualCyclic$1 = deepEqual.use(match_1);
var samsam = {
createMatcher: matcher_1,
deepEqual: deepEqualCyclic$1,
isArguments: isArguments_1,

@@ -502,19 +1030,20 @@ isElement: isElement_1,

identical: identical_1,
deepEqual: deepEqual,
match: match_1
};
var samsam_1 = samsam.isArguments;
var samsam_2 = samsam.isElement;
var samsam_3 = samsam.isNegZero;
var samsam_4 = samsam.identical;
var samsam_5 = samsam.deepEqual;
var samsam_6 = samsam.match;
var samsam_1 = samsam.createMatcher;
var samsam_2 = samsam.deepEqual;
var samsam_3 = samsam.isArguments;
var samsam_4 = samsam.isElement;
var samsam_5 = samsam.isNegZero;
var samsam_6 = samsam.identical;
var samsam_7 = samsam.match;
exports.default = samsam;
exports.isArguments = samsam_1;
exports.isElement = samsam_2;
exports.isNegZero = samsam_3;
exports.identical = samsam_4;
exports.deepEqual = samsam_5;
exports.match = samsam_6;
exports.createMatcher = samsam_1;
exports.deepEqual = samsam_2;
exports.isArguments = samsam_3;
exports.isElement = samsam_4;
exports.isNegZero = samsam_5;
exports.identical = samsam_6;
exports.match = samsam_7;

@@ -521,0 +1050,0 @@ Object.defineProperty(exports, '__esModule', { value: true });

@@ -35,3 +35,3 @@ "use strict";

*/
function deepEqualCyclic(first, second) {
function deepEqualCyclic(first, second, match) {
// used for cyclic comparison

@@ -52,2 +52,12 @@ // contain already visited objects

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(obj2)) {
if (match.isMatcher(obj1)) {
return obj1 === obj2;
}
return obj2.test(obj1);
}
var type1 = typeof obj1;

@@ -189,2 +199,8 @@ var type2 = typeof obj2;

deepEqualCyclic.use = function(match) {
return function(a, b) {
return deepEqualCyclic(a, b, match);
};
};
module.exports = deepEqualCyclic;
"use strict";
var deepEqual = require("./deep-equal").use(match); // eslint-disable-line no-use-before-define
var getClass = require("./get-class");

@@ -7,2 +8,3 @@ var isDate = require("./is-date");

var isSubset = require("./is-subset");
var createMatcher = require("./matcher");

@@ -115,3 +117,3 @@ function arrayContains(array, subset, compare) {

typeof value === "undefined" ||
!match(value, matcher[prop])
!deepEqual(value, matcher[prop])
) {

@@ -130,2 +132,6 @@ return false;

Object.keys(createMatcher).forEach(function(key) {
match[key] = createMatcher[key];
});
module.exports = match;

6

lib/samsam.js
"use strict";
var deepEqualCyclic = require("./deep-equal");
var identical = require("./identical");

@@ -9,4 +8,8 @@ var isArguments = require("./is-arguments");

var match = require("./match");
var deepEqualCyclic = require("./deep-equal").use(match);
var createMatcher = require("./matcher");
module.exports = {
createMatcher: createMatcher,
deepEqual: deepEqualCyclic,
isArguments: isArguments,

@@ -16,4 +19,3 @@ isElement: isElement,

identical: identical,
deepEqual: deepEqualCyclic,
match: match
};
{
"name": "@sinonjs/samsam",
"version": "2.1.3",
"version": "3.0.0",
"description": "Value identification and comparison functions",

@@ -34,3 +34,7 @@ "homepage": "http://sinonjs.github.io/samsam/",

],
"dependencies": {},
"dependencies": {
"@sinonjs/commons": "1.0.2",
"array-from": "^2.1.1",
"lodash.get": "4.4.2"
},
"devDependencies": {

@@ -37,0 +41,0 @@ "@sinonjs/referee": "^2.0.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc