Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bsert

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bsert - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

479

lib/assert.js

@@ -23,3 +23,3 @@ /*!

let operator = 'fail';
let generatedMessage = false;
let generatedMessage = Boolean(options.generatedMessage);

@@ -81,3 +81,3 @@ if (options.message != null)

generatedMessage = true;
} else if (message instanceof Error) {
} else if (isError(message)) {
throw message;

@@ -98,3 +98,6 @@ }

function equal(actual, expected, message) {
if (actual !== expected) {
if (!Object.is(actual, expected)) {
if (isError(message))
throw message;
throw new AssertionError({

@@ -111,3 +114,6 @@ message,

function notEqual(actual, expected, message) {
if (actual === expected) {
if (Object.is(actual, expected)) {
if (isError(message))
throw message;
throw new AssertionError({

@@ -126,2 +132,5 @@ message,

if (isError(message))
throw message;
if (message == null) {

@@ -145,3 +154,3 @@ message = 'Assertion failed.';

message = expected;
expected = null;
expected = undefined;
}

@@ -152,3 +161,3 @@

enforce(typeof func === 'function', 'func', 'function', throws);
enforce(typeof func === 'function', 'func', 'function');

@@ -162,3 +171,3 @@ try {

if (!thrown || !testError(err, expected, throws)) {
if (!thrown) {
let generatedMessage = false;

@@ -173,4 +182,4 @@

message,
actual: false,
expected: true,
actual: undefined,
expected,
operator: 'throws',

@@ -181,8 +190,53 @@ generatedMessage,

}
if (!testError(err, expected, message, throws))
throw err;
}
function doesNotThrow(func, expected, message) {
if (typeof expected === 'string') {
message = expected;
expected = undefined;
}
let thrown = false;
let err = null;
enforce(typeof func === 'function', 'func', 'function');
try {
func();
} catch (e) {
thrown = true;
err = e;
}
if (!thrown)
return;
if (testError(err, expected, message, doesNotThrow)) {
let generatedMessage = false;
if (message == null) {
message = 'Got unwanted exception.';
generatedMessage = true;
}
throw new AssertionError({
message,
actual: err,
expected,
operator: 'doesNotThrow',
generatedMessage,
stackStartFn: doesNotThrow
});
}
throw err;
}
async function rejects(func, expected, message) {
if (typeof expected === 'string') {
message = expected;
expected = null;
expected = undefined;
}

@@ -194,6 +248,6 @@

if (typeof func !== 'function')
enforce(func instanceof Promise, 'func', 'promise', rejects);
enforce(isPromise(func), 'func', 'promise');
try {
if (func instanceof Promise)
if (isPromise(func))
await func;

@@ -207,3 +261,3 @@ else

if (!thrown || !testError(err, expected, rejects)) {
if (!thrown) {
let generatedMessage = false;

@@ -218,4 +272,4 @@

message,
actual: false,
expected: true,
actual: undefined,
expected,
operator: 'rejects',

@@ -226,4 +280,53 @@ generatedMessage,

}
if (!testError(err, expected, message, rejects))
throw err;
}
async function doesNotReject(func, expected, message) {
if (typeof expected === 'string') {
message = expected;
expected = undefined;
}
let thrown = false;
let err = null;
if (typeof func !== 'function')
enforce(isPromise(func), 'func', 'promise');
try {
if (isPromise(func))
await func;
else
await func();
} catch (e) {
thrown = true;
err = e;
}
if (!thrown)
return;
if (testError(err, expected, message, doesNotReject)) {
let generatedMessage = false;
if (message == null) {
message = 'Got unwanted rejection.';
generatedMessage = true;
}
throw new AssertionError({
message,
actual: undefined,
expected,
operator: 'doesNotReject',
generatedMessage,
stackStartFn: doesNotReject
});
}
throw err;
}
function ifError(err) {

@@ -255,2 +358,5 @@ if (err != null) {

if (!isDeepEqual(actual, expected)) {
if (isError(message))
throw message;
throw new AssertionError({

@@ -268,2 +374,5 @@ message,

if (isDeepEqual(actual, expected)) {
if (isError(message))
throw message;
throw new AssertionError({

@@ -290,6 +399,9 @@ message,

enforce(isBuffer(actual), 'actual', 'buffer', bufferEqual);
enforce(isBuffer(expected), 'expected', 'buffer', bufferEqual);
enforce(isBuffer(actual), 'actual', 'buffer');
enforce(isBuffer(expected), 'expected', 'buffer');
if (actual !== expected && !actual.equals(expected)) {
if (isError(message))
throw message;
throw new AssertionError({

@@ -316,6 +428,9 @@ message,

enforce(isBuffer(actual), 'actual', 'buffer', notBufferEqual);
enforce(isBuffer(expected), 'expected', 'buffer', notBufferEqual);
enforce(isBuffer(actual), 'actual', 'buffer');
enforce(isBuffer(expected), 'expected', 'buffer');
if (actual === expected || actual.equals(expected)) {
if (isError(message))
throw message;
throw new AssertionError({

@@ -331,7 +446,20 @@ message,

function enforce(value, name, type, func) {
function enforce(value, name, type) {
if (!value) {
const err = new TypeError(`'${name}' must be a(n) ${type}.`);
let msg;
if (name == null) {
msg = 'Invalid type for parameter.';
} else {
if (type == null)
msg = `Invalid type for "${name}".`;
else
msg = `"${name}" must be a(n) ${type}.`;
}
const err = new TypeError(msg);
if (Error.captureStackTrace)
Error.captureStackTrace(err, func || enforce);
Error.captureStackTrace(err, enforce);
throw err;

@@ -341,7 +469,13 @@ }

function range(value, name, func) {
function range(value, name) {
if (!value) {
const err = new RangeError(`'${name}' is out of range.`);
const msg = name != null
? `"${name}" is out of range.`
: 'Parameter is out of range.';
const err = new RangeError(msg);
if (Error.captureStackTrace)
Error.captureStackTrace(err, func || range);
Error.captureStackTrace(err, range);
throw err;

@@ -352,3 +486,3 @@ }

/*
* Helpers
* Stringification
*/

@@ -364,18 +498,3 @@

if (value instanceof Date)
return value.toISOString();
if (value instanceof RegExp)
return String(value);
if (value instanceof Error)
return String(value.message);
if (!value.constructor
|| typeof value.constructor.name !== 'string'
|| value.constructor.name === 'Object') {
return '[Object]';
}
return `[Object: ${value.constructor.name}]`;
return `[${objectName(value)}]`;
case 'boolean':

@@ -392,5 +511,3 @@ return value.toString();

case 'function':
if (typeof value.name !== 'string')
return '[Function]';
return `[Function: ${value.name}]`;
return `[${objectName(value)}]`;
case 'bigint':

@@ -407,44 +524,77 @@ return `${value.toString()}n`;

if (isDate(str))
return String(str);
if (isError(str))
return String(str.message || '');
return stringify(str);
}
function testError(err, expected, func) {
/*
* Error Testing
*/
function testError(err, expected, message, func) {
if (expected == null)
return true;
if (expected instanceof RegExp)
if (isRegExp(expected))
return expected.test(err);
if (typeof expected !== 'function') {
if (func === doesNotThrow || func === doesNotReject)
throw new TypeError('"expected" must not be an object.');
if (typeof expected !== 'object')
return false;
throw new TypeError('"expected" must be an object.');
if (err == null)
return false;
let generatedMessage = false;
if (message == null) {
const name = func === rejects ? 'rejection' : 'exception';
message = `Missing expected ${name}.`;
generatedMessage = true;
}
if (err == null || typeof err !== 'object') {
throw new AssertionError({
actual: err,
expected,
message,
operator: func.name,
generatedMessage,
stackStartFn: func
});
}
const keys = Object.keys(expected);
if (expected instanceof Error)
if (isError(expected))
keys.push('name', 'message');
if (keys.length === 0)
return false;
throw new TypeError('"expected" may not be an empty object.');
for (const key of keys) {
const expect = expected[key];
if (!(key in err))
return false;
const value = err[key];
if ((expect instanceof RegExp) && typeof value === 'string') {
if (!expect.test(value))
return false;
if (typeof value === 'string'
&& isRegExp(expect)
&& expect.test(value)) {
continue;
}
if (!isDeepEqual(value, expect))
return false;
if ((key in err) && isDeepEqual(value, expect))
continue;
throw new AssertionError({
actual: err,
expected: expected,
message,
operator: func.name,
generatedMessage,
stackStartFn: func
});
}

@@ -464,49 +614,6 @@

function objectString(obj) {
return Object.prototype.toString.call(obj);
}
/*
* Comparisons
*/
function hasOwnProperty(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
function isObject(buf) {
return buf && typeof buf === 'object';
}
function isBuffer(buf) {
return isObject(buf)
&& typeof buf.writeUInt32LE === 'function'
&& typeof buf.equals === 'function';
}
function isArguments(object) {
return objectString(object) === '[object Arguments]';
}
function isView(view) {
if (isBuffer(view))
return false;
if (typeof ArrayBuffer.isView === 'function')
return ArrayBuffer.isView(view);
if (!view)
return false;
if (view instanceof DataView)
return true;
if (view.buffer && (view.buffer instanceof ArrayBuffer))
return true;
return false;
}
function isEqual(x, y) {
if (x === y)
return x !== 0 || 1 / x === 1 / y;
return x !== x && y !== y;
}
function isDeepEqual(x, y) {

@@ -517,4 +624,4 @@ return compare(x, y, null);

function compare(a, b, cache) {
// Primitives
if (isEqual(a, b))
// Primitives.
if (Object.is(a, b))
return true;

@@ -525,3 +632,3 @@

// Semi-primitives
// Semi-primitives.
if (objectString(a) !== objectString(b))

@@ -536,6 +643,6 @@ return false;

if ((a instanceof Date) && (b instanceof Date))
return a.getTime() === b.getTime();
if (isDate(a))
return Object.is(a.getTime(), b.getTime());
if ((a instanceof RegExp) && (b instanceof RegExp)) {
if (isRegExp(a)) {
return a.source === b.source

@@ -548,3 +655,3 @@ && a.global === b.global

if ((a instanceof Error) && (b instanceof Error)) {
if (isError(a)) {
if (a.message !== b.message)

@@ -554,3 +661,3 @@ return false;

if ((a instanceof ArrayBuffer) && (b instanceof ArrayBuffer)) {
if (isArrayBuffer(a)) {
a = new Uint8Array(a);

@@ -560,5 +667,6 @@ b = new Uint8Array(b);

if (isView(a) && isView(b)
&& !(a instanceof Float32Array)
&& !(b instanceof Float64Array)) {
if (isView(a) && !isBuffer(a)) {
if (isBuffer(b))
return false;
const x = new Uint8Array(a.buffer);

@@ -578,3 +686,3 @@ const y = new Uint8Array(b.buffer);

if ((a instanceof Set) && (b instanceof Set)) {
if (isSet(a)) {
if (a.size !== b.size)

@@ -585,11 +693,6 @@ return false;

for (const key of keys) {
if (a.has(key) !== b.has(key))
return false;
}
return true;
return keys.size === a.size;
}
// Recursive
// Recursive.
if (!cache) {

@@ -625,22 +728,12 @@ cache = {

function recurse(a, b, cache) {
if (isArguments(a) && isArguments(b)) {
const x = Array.prototype.slice.call(a);
const y = Array.prototype.slice.call(b);
if (!isDeepEqual(x, y))
if (isMap(a)) {
if (a.size !== b.size)
return false;
return true;
}
const keys = new Set([...a.keys(), ...b.keys()]);
if ((a instanceof Map) && (b instanceof Map)) {
if (a.size !== b.size)
if (keys.size !== a.size)
return false;
const keys = new Set([...a.keys(), ...b.keys()]);
for (const key of keys) {
if (a.has(key) !== b.has(key))
return false;
if (!compare(a.get(key), b.get(key), cache))

@@ -653,3 +746,3 @@ return false;

if (Array.isArray(a) && Array.isArray(b)) {
if (isArray(a)) {
if (a.length !== b.length)

@@ -674,6 +767,6 @@ return false;

if (keys.size !== ak.length)
return false;
for (const key of keys) {
if (hasOwnProperty(a, key) !== hasOwnProperty(b, key))
return false;
if (!compare(a[key], b[key], cache))

@@ -686,2 +779,87 @@ return false;

/*
* Helpers
*/
function objectString(obj) {
if (obj === undefined)
return '[object Undefined]';
if (obj === null)
return '[object Null]';
return Object.prototype.toString.call(obj);
}
function objectType(obj) {
return objectString(obj).slice(8, -1);
}
function objectName(value) {
if (value === undefined)
return 'undefined';
if (value === null)
return 'null';
const name = objectType(value);
if (name !== 'Object' && name !== 'Error')
return name;
if (!value.constructor
|| typeof value.constructor.name !== 'string'
|| value.constructor.name.length === 0) {
return name;
}
return value.constructor.name;
}
function isArray(obj) {
return Array.isArray(obj);
}
function isArrayBuffer(obj) {
return obj instanceof ArrayBuffer;
}
function isBuffer(obj) {
return isObject(obj)
&& typeof obj.writeUInt32LE === 'function'
&& typeof obj.equals === 'function';
}
function isDate(obj) {
return obj instanceof Date;
}
function isError(obj) {
return obj instanceof Error;
}
function isMap(obj) {
return obj instanceof Map;
}
function isObject(obj) {
return obj && typeof obj === 'object';
}
function isPromise(obj) {
return obj instanceof Promise;
}
function isRegExp(obj) {
return obj instanceof RegExp;
}
function isSet(obj) {
return obj instanceof Set;
}
function isView(obj) {
return ArrayBuffer.isView(obj);
}
function isEncoding(enc) {

@@ -708,5 +886,8 @@ if (typeof enc !== 'string')

if (typeof expected === 'string') {
const ctor = actual ? actual.constructor : null;
if (!isBuffer(actual))
return null;
if (!ctor || typeof ctor.from !== 'function')
const {constructor} = actual;
if (!constructor || typeof constructor.from !== 'function')
return null;

@@ -720,3 +901,3 @@

const raw = ctor.from(expected, enc);
const raw = constructor.from(expected, enc);

@@ -746,3 +927,5 @@ if (enc === 'hex' && raw.length !== (expected.length >>> 1))

assert.throws = throws;
assert.doesNotThrow = doesNotThrow;
assert.rejects = rejects;
assert.doesNotReject = doesNotReject;
assert.ifError = ifError;

@@ -749,0 +932,0 @@ assert.deepEqual = deepEqual;

{
"name": "bsert",
"version": "0.0.6",
"version": "0.0.7",
"description": "Minimal assert with type checking",

@@ -17,3 +17,4 @@ "keywords": [

"scripts": {
"lint": "eslint lib/ || exit 0"
"lint": "eslint lib/ test/ || exit 0",
"test": "bmocha --reporter spec test/*-test.js"
},

@@ -20,0 +21,0 @@ "engines": {

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