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

@bayou/typecheck

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bayou/typecheck - npm Package Compare versions

Comparing version 1.0.6 to 1.2.4

2

index.js

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -7,6 +7,6 @@ {

"dependencies": {
"@bayou/util-core": "1.0.6"
"@bayou/util-core": "1.2.4"
},
"name": "@bayou/typecheck",
"version": "1.0.6"
"version": "1.2.4"
}

@@ -31,5 +31,5 @@ @bayou/typecheck

```
Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
Licensed AS IS and WITHOUT WARRANTY under the Apache License,
Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>
```

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -13,7 +13,10 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

it('should return the provided value when passed an object', () => {
const value = { a: 1, b: 2 };
const func = () => 123;
function test(value) {
assert.strictEqual(TObject.check(value), value);
}
assert.strictEqual(TObject.check(value), value);
assert.strictEqual(TObject.check(func), func);
test({ a: 1, b: 2 });
test([1, 2, 3]);
test(() => 123);
test(new Map());
});

@@ -55,2 +58,57 @@

describe('orNull(value)', () => {
it('should return the provided value when passed an object', () => {
function test(value) {
assert.strictEqual(TObject.orNull(value), value);
}
test({ a: 1, b: 2 });
test([1, 2, 3]);
test(() => 123);
test(new Map());
});
it('should return `null` when passed `null`', () => {
assert.isNull(TObject.orNull(null));
});
it('should throw an Error when passed anything other than an object or `null`', () => {
assert.throws(() => TObject.orNull(undefined));
assert.throws(() => TObject.orNull(false));
assert.throws(() => TObject.orNull(54));
assert.throws(() => TObject.orNull('this better not work'));
});
});
describe('orNull(value, clazz)', () => {
it('should return the provided value when passed an object of a matching class', () => {
function test(value, clazz) {
assert.strictEqual(TObject.orNull(value, clazz), value);
}
test({ a: 1, b: 2 }, Object);
test([1, 2, 3], Array);
test([1, 2, 3], Object);
test(() => 123, Function);
test(new Map(), Map);
test(new Map(), Object);
});
it('should return `null` when passed a `null` value, no matter what class is passed', () => {
assert.isNull(TObject.orNull(null, Object));
assert.isNull(TObject.orNull(null, Set));
});
it('should throw an Error when passed an object of a non-matching class', () => {
assert.throws(() => TObject.orNull(new Map(), Set));
assert.throws(() => TObject.orNull(new Set(), Map));
});
it('should throw an Error when passed anything other than an object or `null`', () => {
assert.throws(() => TObject.orNull(false, Boolean));
assert.throws(() => TObject.orNull(914, Number));
assert.throws(() => TObject.orNull('florp', String));
});
});
describe('plain()', () => {

@@ -57,0 +115,0 @@ it('should accept plain objects', () => {

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -28,3 +28,3 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

describe('check(value)', () => {
it('should return the provided value when passed a string', () => {
it('accepts strings', () => {
const value = 'this better work!';

@@ -35,3 +35,3 @@

it('should throw an Error when passed anything other than a string', () => {
it('rejects anything other than a string', () => {
assert.throws(() => TString.check(54));

@@ -47,3 +47,3 @@ assert.throws(() => TString.check(true));

describe('check(value, regex)', () => {
it('should allow a string when it matches the provided regex', () => {
it('accepts a string which matches the given regex', () => {
const value = 'deadbeef7584930215cafe';

@@ -54,3 +54,3 @@

it('should throw an Error when a string fails to match the provided regex', () => {
it('rejects a non-matching string', () => {
const value = 'this better not work!';

@@ -63,3 +63,3 @@

describe('hexBytes(value)', () => {
it('should return the provided value if it is a string of hex bytes', () => {
it('accepts a string of hex bytes', () => {
const value = 'deadbeef7584930215cafe';

@@ -70,3 +70,3 @@

it('should throw an Error when anything other than a string of hex bytes is provided', () => {
it('rejects a string of non-hex', () => {
const value = 'this better not work!';

@@ -79,3 +79,3 @@

describe('hexBytes(value, minBytes)', () => {
it('should return the provided value if it is a string of hex bytes of the required minimum length', () => {
it('accepts a string of hex bytes of the required minimum length', () => {
const value = 'deadbeef7584930215cafe';

@@ -86,3 +86,3 @@

it('should return the provided value if it is a string of hex bytes greater than the required minimum length', () => {
it('accepts a string of hex bytes greater than the required minimum length', () => {
const value = 'deadbeef7584930215cafe';

@@ -93,3 +93,3 @@

it('should throw an Error if the number of bytes is less than the minimum', () => {
it('rejects too-short strings', () => {
const value = 'deadbeef7584930215cafe';

@@ -102,3 +102,3 @@

describe('hexBytes(value, minBytes, maxBytes)', () => {
it('should return the provided value if it is a string of hex bytes of the required minimum length', () => {
it('accepts a string of hex bytes of the required minimum length', () => {
const value = 'deadbeef7584930215cafe';

@@ -109,3 +109,3 @@

it('should return the provided value if it is a string of hex bytes within the required length range', () => {
it('accepts a string of hex bytes within the required length range', () => {
const value = 'deadbeef7584930215cafe';

@@ -116,3 +116,3 @@

it('should return the provided value if it is a string of hex bytes equal to the maximum length', () => {
it('accepts a string of hex bytes equal to the maximum length', () => {
const value = 'deadbeef7584930215cafe';

@@ -123,3 +123,3 @@

it('should throw an Error if the number of bytes is less than the minimum', () => {
it('rejects too-short strings', () => {
const value = 'deadbeef7584930215cafe';

@@ -130,3 +130,3 @@

it('should throw an Error if the number of bytes is greater than the minimum', () => {
it('rejects too-long strings', () => {
const value = 'deadbeef7584930215cafe';

@@ -357,12 +357,14 @@

describe('nonEmpty()', () => {
it('should return the provided value if it is a string with length >= 1', () => {
const value = 'This better work!';
it('accepts strings of length `1` or longer', () => {
function test(value) {
assert.strictEqual(TString.nonEmpty(value), value);
}
assert.strictEqual(TString.nonEmpty(value), value);
test('x');
test('xy');
test('This better work!');
});
it('should throw an Error if value is a string of length 0', () => {
const value = '';
assert.throws(() => TString.nonEmpty(value));
it('should throw if value is a string of length 0', () => {
assert.throws(() => TString.nonEmpty(''));
});

@@ -372,3 +374,3 @@ });

describe('orNull()', () => {
it('should return the provided value if it is a string', () => {
it('accepts strings', () => {
const value = 'This better work!';

@@ -379,9 +381,7 @@

it('should return the provided value if it is null', () => {
const value = null;
assert.strictEqual(TString.orNull(value), value);
it('accepts `null`', () => {
assert.strictEqual(TString.orNull(null), null);
});
it('should throw an Error if value is not a string and is not null', () => {
it('rejects non-`null` non-strings', () => {
assert.throws(() => TString.orNull(undefined));

@@ -396,28 +396,71 @@ assert.throws(() => TString.orNull(5.1));

describe('urlAbsolute()', () => {
it('should return the provided value if it is an absolute URL string', () => {
const value = 'https://www.example.com/';
it('accepts absolute URLs', () => {
function test(value) {
assert.strictEqual(TString.urlAbsolute(value), value, value);
}
assert.strictEqual(TString.urlAbsolute(value), value);
test('https://www.example.com/');
test('http://foo.com/');
test('https://bar.baz.co/florp');
test('https://bar.baz.co/florp/');
test('https://bar.baz.co/biff/boo');
});
it('should throw an Error if value is not a URL string at all', () => {
assert.throws(() => TString.urlAbsolute('this better not work!'));
assert.throws(() => TString.urlAbsolute('/home/users/fnord'));
assert.throws(() => TString.urlAbsolute('http:example.com'));
assert.throws(() => TString.urlAbsolute('http:example.com/foo'));
assert.throws(() => TString.urlAbsolute('http:/example.com'));
assert.throws(() => TString.urlAbsolute('http://example.com')); // Needs final slash.
assert.throws(() => TString.urlAbsolute(5.1));
assert.throws(() => TString.urlAbsolute(undefined));
assert.throws(() => TString.urlAbsolute(null));
it('rejects non-absolute URLs', () => {
function test(value) {
assert.throws(() => TString.urlAbsolute(value), /badValue/, inspect(value));
}
test('/home/users/fnord');
test('http:example.com');
test('http:example.com/foo');
test('http:/example.com');
test('http://example.com'); // Needs final slash.
});
it('should throw an Error if value has auth info', () => {
assert.throws(() => TString.urlAbsolute('http://user@example.com/'));
assert.throws(() => TString.urlAbsolute('http://user:pass@example.com/'));
it('rejects non-URLs', () => {
function test(value) {
assert.throws(() => TString.urlAbsolute(value), /badValue/, inspect(value));
}
test('');
test('this better not work!');
test(5.1);
test(undefined);
test(null);
});
it('rejects URLs with auth info', () => {
function test(value) {
assert.throws(() => TString.urlAbsolute(value), /badValue/, inspect(value));
}
test('http://user@example.com/');
test('http://user:pass@example.com/');
});
it('rejects URLs with a query', () => {
function test(value) {
assert.throws(() => TString.urlAbsolute(value), /badValue/, inspect(value));
}
test('https://milk.com/?a=10');
test('https://milk.com/?x=1&y=2');
test('http://milk.com/bcd?e=10');
test('http://milk.com/bcd/efgh?i=123&jkl=234');
});
it('rejects URLs with a hash', () => {
function test(value) {
assert.throws(() => TString.urlAbsolute(value), /badValue/, inspect(value));
}
test('https://milk.com/#florp');
test('http://milk.com/bcd#florp');
test('http://milk.com/bcd/efgh#florp');
});
});
describe('urlOrigin()', () => {
it('should return the provided value if it is an origin-only URL', () => {
it('accepts origin-only URLs', () => {
let which = 0;

@@ -434,20 +477,34 @@ function test(value) {

it('should throw an Error if value is not an origin-only URL', () => {
assert.throws(() => TString.urlOrigin('http://foo.bar/'));
assert.throws(() => TString.urlOrigin('http://foo.bar/x'));
assert.throws(() => TString.urlOrigin('https://foo@bar.com'));
assert.throws(() => TString.urlOrigin('https://florp:like@example.com'));
it('rejects URLs that are not origin-only', () => {
function test(value) {
assert.throws(() => TString.urlOrigin(value), /badValue/, inspect(value));
}
test('http://foo.bar/'); // Shouldn't end with a slash.
test('http://foo.bar/x');
test('https://foo@bar.com');
test('https://florp:like@example.com');
test('http://foo.bar/?a=10');
test('http://foo.bar/x?a=10');
test('http://foo.bar/x/?a=10');
test('http://foo.bar/#123');
test('http://foo.bar/baz#123');
test('http://foo.bar/baz/#123');
});
it('should throw an Error if value is not a URL string at all', () => {
assert.throws(() => TString.urlOrigin('this better not work!'));
assert.throws(() => TString.urlOrigin('/home/users/fnord'));
assert.throws(() => TString.urlOrigin('http:example.com'));
assert.throws(() => TString.urlOrigin('http:example.com/foo'));
assert.throws(() => TString.urlOrigin('http:/example.com'));
assert.throws(() => TString.urlOrigin(5.1));
assert.throws(() => TString.urlOrigin(undefined));
assert.throws(() => TString.urlOrigin(null));
it('rejects non-URLs', () => {
function test(value) {
assert.throws(() => TString.urlOrigin(value), /badValue/, inspect(value));
}
test('this better not work!');
test('/home/users/fnord');
test('http:example.com');
test('http:example.com/foo');
test('http:/example.com');
test(5.1);
test(undefined);
test(null);
});
});
});

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -29,2 +29,25 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

/**
* Checks a value which must either be of type `Object` or the exact value
* `null`.
*
* @param {*} value Value to check.
* @param {object|null} [clazz = null] Class (constructor) that `value` must
* be an instance of, or `null` if there is no class requirement.
* @returns {object|null} `value`.
*/
static orNull(value, clazz = null) {
if (value === null) {
return null;
}
try {
return TObject.check(value, clazz);
} catch (e) {
// Throw a higher-fidelity error.
const name = (clazz === null) ? 'Object' : `class ${clazz.name}`;
throw Errors.badValue(value, `${name}|null`);
}
}
/**
* Checks that a value is of type `Object` and is furthermore a plain object,

@@ -31,0 +54,0 @@ * which is to say, not any of an array, a function, or an instance of a class

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -3,0 +3,0 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

@@ -1,2 +0,2 @@

// Copyright 2016-2018 the Bayou Authors (Dan Bornstein et alia).
// Copyright 2016-2019 the Bayou Authors (Dan Bornstein et alia).
// Licensed AS IS and WITHOUT WARRANTY under the Apache License,

@@ -149,4 +149,5 @@ // Version 2.0. Details: <http://www.apache.org/licenses/LICENSE-2.0>

* Checks a value which must be a syntactically valid absolute URL with a path
* (which can just be `/`) and without auth info. (Auth info consists of a
* username and optional password before the host name.)
* (which can just be `/`) and without any of auth info, a query, or a hash.
* (Auth info consists of a username and optional password before the host
* name.)
*

@@ -161,23 +162,16 @@ * @param {*} value Value to check.

} catch (e) {
// Throw a higher-fidelity error.
throw Errors.badValue(value, String, 'absolute URL syntax');
// Set up `url` so that the test below will cause us to throw the proper
// error.
url = {};
}
// Some versions of `URL` will parse a missing origin into the literal
// string `null`, hence the third check here. The last check ensures that
// the original `value` is well-formed; while `new URL()` is somewhat
// lenient, the `href` it produces is guaranteed to be well-formed, and so
// the `===` comparison transitively ensures that the original `value` is
// also well-formed.
if (!( url.host
&& url.origin
&& (url.origin !== 'null')
&& (url.href === value))) {
// **Note:** Though `new URL()` is lenient with respect to parsing, if it
// _does_ parse successfully, `origin` and `pathname` will always be
// well-formed, and if they combine to form the originally given value, we
// know the original doesn't have any of the verboten parts (nor a missing
// path).
if (value !== `${url.origin}${url.pathname}`) {
throw Errors.badValue(value, String, 'absolute URL syntax');
}
if (url.username || url.password) {
throw Errors.badValue(value, String, 'absolute URL syntax, without auth');
}
return value;

@@ -184,0 +178,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