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.5 to 0.0.6

613

lib/assert.js

@@ -21,4 +21,4 @@ /*!

let message = '';
let operator = '!=';
let message = null;
let operator = 'fail';
let generatedMessage = false;

@@ -32,7 +32,12 @@

if (!message) {
const a = stringify(options.actual);
const b = stringify(options.expected);
if (message == null) {
if (operator === 'fail') {
message = 'Assertion failed.';
} else {
const a = stringify(options.actual);
const b = stringify(options.expected);
message = `${a} ${operator} ${b}`;
message = `${a} ${operator} ${b}`;
}
generatedMessage = true;

@@ -45,8 +50,6 @@ }

if (typeof options.start === 'function')
start = options.start;
if (typeof options.stackStartFunction === 'function')
start = options.stackStartFunction;
else if (typeof options.stackStartFn === 'function')
start = options.stackStartFn;
else if (typeof options.stackStartFunction === 'function')
start = options.stackStartFunction;

@@ -66,3 +69,3 @@ this.type = 'AssertionError';

/**
/*
* Assert

@@ -73,8 +76,21 @@ */

if (!value) {
let generatedMessage = false;
if (arguments.length === 0) {
message = 'No value argument passed to `assert()`.';
generatedMessage = true;
} else if (message == null) {
message = 'Assertion failed.';
generatedMessage = true;
} else if (message instanceof Error) {
throw message;
}
throw new AssertionError({
message,
actual: false,
actual: value,
expected: true,
operator: '==',
start: assert
generatedMessage,
stackStartFn: assert
});

@@ -84,5 +100,3 @@ }

assert.ok = assert;
assert.equal = function equal(actual, expected, message) {
function equal(actual, expected, message) {
if (actual !== expected) {

@@ -93,9 +107,9 @@ throw new AssertionError({

expected,
operator: '===',
start: equal
operator: 'strictEqual',
stackStartFn: equal
});
}
};
}
assert.notEqual = function notEqual(actual, expected, message) {
function notEqual(actual, expected, message) {
if (actual === expected) {

@@ -106,16 +120,16 @@ throw new AssertionError({

expected,
operator: '!==',
start: notEqual
operator: 'notStrictEqual',
stackStartFn: notEqual
});
}
};
}
assert.strictEqual = assert.equal;
function fail(message) {
let generatedMessage = false;
assert.notStrictEqual = assert.notEqual;
if (message == null) {
message = 'Assertion failed.';
generatedMessage = true;
}
assert.fail = function fail(message) {
if (message == null || message === '')
message = 'Failed';
throw new AssertionError({

@@ -125,24 +139,201 @@ message,

expected: true,
operator: '==',
start: fail
operator: 'fail',
generatedMessage,
stackStartFn: fail
});
};
}
assert.enforce = function enforce(value, name, type) {
function throws(func, expected, message) {
if (typeof expected === 'string') {
message = expected;
expected = null;
}
let thrown = false;
let err = null;
enforce(typeof func === 'function', 'func', 'function', throws);
try {
func();
} catch (e) {
thrown = true;
err = e;
}
if (!thrown || !testError(err, expected, throws)) {
let generatedMessage = false;
if (message == null) {
message = 'Missing expected exception.';
generatedMessage = true;
}
throw new AssertionError({
message,
actual: false,
expected: true,
operator: 'throws',
generatedMessage,
stackStartFn: throws
});
}
}
async function rejects(func, expected, message) {
if (typeof expected === 'string') {
message = expected;
expected = null;
}
let thrown = false;
let err = null;
if (typeof func !== 'function')
enforce(func instanceof Promise, 'func', 'promise', rejects);
try {
if (func instanceof Promise)
await func;
else
await func();
} catch (e) {
thrown = true;
err = e;
}
if (!thrown || !testError(err, expected, rejects)) {
let generatedMessage = false;
if (message == null) {
message = 'Missing expected rejection.';
generatedMessage = true;
}
throw new AssertionError({
message,
actual: false,
expected: true,
operator: 'rejects',
generatedMessage,
stackStartFn: rejects
});
}
}
function ifError(err) {
if (err != null) {
let message = 'ifError got unwanted exception: ';
if (typeof err === 'object' && typeof err.message === 'string') {
if (err.message.length === 0 && err.constructor)
message += err.constructor.name;
else
message += err.message;
} else {
message += stringify(err);
}
throw new AssertionError({
message,
actual: err,
expected: null,
operator: 'ifError',
generatedMessage: true,
stackStartFn: ifError
});
}
}
function deepEqual(actual, expected, message) {
if (!isDeepEqual(actual, expected)) {
throw new AssertionError({
message,
actual,
expected,
operator: 'deepStrictEqual',
stackStartFn: deepEqual
});
}
}
function notDeepEqual(actual, expected, message) {
if (isDeepEqual(actual, expected)) {
throw new AssertionError({
message,
actual,
expected,
operator: 'notDeepStrictEqual',
stackStartFn: notDeepEqual
});
}
}
function bufferEqual(actual, expected, enc, message) {
if (!isEncoding(enc)) {
message = enc;
enc = null;
}
if (enc == null)
enc = 'hex';
expected = bufferize(actual, expected, enc);
enforce(isBuffer(actual), 'actual', 'buffer', bufferEqual);
enforce(isBuffer(expected), 'expected', 'buffer', bufferEqual);
if (actual !== expected && !actual.equals(expected)) {
throw new AssertionError({
message,
actual: actual.toString(enc),
expected: expected.toString(enc),
operator: 'bufferEqual',
stackStartFn: bufferEqual
});
}
}
function notBufferEqual(actual, expected, enc, message) {
if (!isEncoding(enc)) {
message = enc;
enc = null;
}
if (enc == null)
enc = 'hex';
expected = bufferize(actual, expected, enc);
enforce(isBuffer(actual), 'actual', 'buffer', notBufferEqual);
enforce(isBuffer(expected), 'expected', 'buffer', notBufferEqual);
if (actual === expected || actual.equals(expected)) {
throw new AssertionError({
message,
actual: actual.toString(enc),
expected: expected.toString(enc),
operator: 'notBufferEqual',
stackStartFn: notBufferEqual
});
}
}
function enforce(value, name, type, func) {
if (!value) {
const err = new TypeError(`'${name}' must be a(n) ${type}.`);
if (Error.captureStackTrace)
Error.captureStackTrace(err, enforce);
Error.captureStackTrace(err, func || enforce);
throw err;
}
};
}
assert.range = function range(value, name) {
function range(value, name, func) {
if (!value) {
const err = new RangeError(`'${name}' is out of range.`);
if (Error.captureStackTrace)
Error.captureStackTrace(err, range);
Error.captureStackTrace(err, func || range);
throw err;
}
};
}

@@ -161,2 +352,11 @@ /*

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

@@ -172,6 +372,6 @@ || typeof value.constructor.name !== 'string'

case 'number':
return value.toString(10);
return value.toString();
case 'string':
if (value.length > 64)
value = `${value.substring(0, 61)}...`;
if (value.length > 80)
value = `${value.substring(0, 77)}...`;
return JSON.stringify(value);

@@ -187,3 +387,3 @@ case 'symbol':

default:
return '[Unknown]';
return `[${typeof value}]`;
}

@@ -199,4 +399,310 @@ }

function testError(err, expected, func) {
if (expected == null)
return true;
if (expected instanceof RegExp)
return expected.test(err);
if (typeof expected !== 'function') {
if (typeof expected !== 'object')
return false;
if (err == null)
return false;
const keys = Object.keys(expected);
if (expected instanceof Error)
keys.push('name', 'message');
if (keys.length === 0)
return false;
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;
continue;
}
if (!isDeepEqual(value, expect))
return false;
}
return true;
}
if (expected.prototype !== undefined && (err instanceof expected))
return true;
if (Error.isPrototypeOf(expected))
return false;
return expected.call({}, err) === true;
}
function objectString(obj) {
return Object.prototype.toString.call(obj);
}
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) {
return compare(x, y, null);
}
function compare(a, b, cache) {
// Primitives
if (isEqual(a, b))
return true;
if (!isObject(a) || !isObject(b))
return false;
// Semi-primitives
if (objectString(a) !== objectString(b))
return false;
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
return false;
if (isBuffer(a) && isBuffer(b))
return a.equals(b);
if ((a instanceof Date) && (b instanceof Date))
return a.getTime() === b.getTime();
if ((a instanceof RegExp) && (b instanceof RegExp)) {
return a.source === b.source
&& a.global === b.global
&& a.multiline === b.multiline
&& a.lastIndex === b.lastIndex
&& a.ignoreCase === b.ignoreCase;
}
if ((a instanceof Error) && (b instanceof Error)) {
if (a.message !== b.message)
return false;
}
if ((a instanceof ArrayBuffer) && (b instanceof ArrayBuffer)) {
a = new Uint8Array(a);
b = new Uint8Array(b);
}
if (isView(a) && isView(b)
&& !(a instanceof Float32Array)
&& !(b instanceof Float64Array)) {
const x = new Uint8Array(a.buffer);
const y = new Uint8Array(b.buffer);
if (x.length !== y.length)
return false;
for (let i = 0; i < x.length; i++) {
if (x[i] !== y[i])
return false;
}
return true;
}
if ((a instanceof Set) && (b instanceof Set)) {
if (a.size !== b.size)
return false;
const keys = new Set([...a, ...b]);
for (const key of keys) {
if (a.has(key) !== b.has(key))
return false;
}
return true;
}
// Recursive
if (!cache) {
cache = {
a: new Map(),
b: new Map(),
p: 0
};
} else {
const aa = cache.a.get(a);
if (aa != null) {
const bb = cache.b.get(b);
if (bb != null)
return aa === bb;
}
cache.p += 1;
}
cache.a.set(a, cache.p);
cache.b.set(b, cache.p);
const ret = recurse(a, b, cache);
cache.a.delete(a);
cache.b.delete(b);
return ret;
}
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))
return false;
return true;
}
if ((a instanceof Map) && (b instanceof Map)) {
if (a.size !== b.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))
return false;
}
return true;
}
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++) {
if (!compare(a[i], b[i], cache))
return false;
}
return true;
}
const ak = Object.keys(a);
const bk = Object.keys(b);
if (ak.length !== bk.length)
return false;
const keys = new Set([...ak, ...bk]);
for (const key of keys) {
if (hasOwnProperty(a, key) !== hasOwnProperty(b, key))
return false;
if (!compare(a[key], b[key], cache))
return false;
}
return true;
}
function isEncoding(enc) {
if (typeof enc !== 'string')
return false;
switch (enc) {
case 'ascii':
case 'binary':
case 'base64':
case 'hex':
case 'latin1':
case 'ucs2':
case 'utf8':
case 'utf16le':
return true;
}
return false;
}
function bufferize(actual, expected, enc) {
if (typeof expected === 'string') {
const ctor = actual ? actual.constructor : null;
if (!ctor || typeof ctor.from !== 'function')
return null;
if (!isEncoding(enc))
return null;
if (enc === 'hex' && (expected.length & 1))
return null;
const raw = ctor.from(expected, enc);
if (enc === 'hex' && raw.length !== (expected.length >>> 1))
return null;
return raw;
}
return expected;
}
/*
* Expose
* API
*/

@@ -207,3 +713,24 @@

assert.strict = assert;
assert.ok = assert;
assert.equal = equal;
assert.notEqual = notEqual;
assert.strictEqual = equal;
assert.notStrictEqual = notEqual;
assert.fail = fail;
assert.throws = throws;
assert.rejects = rejects;
assert.ifError = ifError;
assert.deepEqual = deepEqual;
assert.notDeepEqual = notDeepEqual;
assert.deepStrictEqual = deepEqual;
assert.notDeepStrictEqual = notDeepEqual;
assert.bufferEqual = bufferEqual;
assert.notBufferEqual = notBufferEqual;
assert.enforce = enforce;
assert.range = range;
/*
* Expose
*/
module.exports = assert;

2

package.json
{
"name": "bsert",
"version": "0.0.5",
"version": "0.0.6",
"description": "Minimal assert with type checking",

@@ -5,0 +5,0 @@ "keywords": [

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