Add is.url
This commit is contained in:
parent
9df6f4ebe9
commit
1ecd9367b2
3 changed files with 28 additions and 0 deletions
|
|
@ -255,6 +255,12 @@ is.urlInstance(url);
|
||||||
//=> true
|
//=> true
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### .url(value)
|
||||||
|
|
||||||
|
Returns `true` if `value` is a URL.
|
||||||
|
|
||||||
|
Note: this only does basic checking using the [`URL` class](https://developer.mozilla.org/en-US/docs/Web/API/URL) constructor.
|
||||||
|
|
||||||
##### .truthy(value)
|
##### .truthy(value)
|
||||||
|
|
||||||
Returns `true` for all values that evaluate to true in a boolean context:
|
Returns `true` for all values that evaluate to true in a boolean context:
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,19 @@ namespace is { // tslint:disable-line:no-namespace
|
||||||
export const directInstanceOf = <T>(instance: unknown, klass: Class<T>): instance is T => Object.getPrototypeOf(instance) === klass.prototype;
|
export const directInstanceOf = <T>(instance: unknown, klass: Class<T>): instance is T => Object.getPrototypeOf(instance) === klass.prototype;
|
||||||
export const urlInstance = (value: unknown): value is URL => isObjectOfType<URL>(TypeName.URL)(value);
|
export const urlInstance = (value: unknown): value is URL => isObjectOfType<URL>(TypeName.URL)(value);
|
||||||
|
|
||||||
|
export const url = (value: unknown) => {
|
||||||
|
if (!string(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
new URL(value); // tslint:disable-line no-unused-expression
|
||||||
|
return true;
|
||||||
|
} catch (_) { // tslint:disable-line no-unused
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const truthy = (value: unknown) => Boolean(value);
|
export const truthy = (value: unknown) => Boolean(value);
|
||||||
export const falsy = (value: unknown) => !value;
|
export const falsy = (value: unknown) => !value;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -583,6 +583,15 @@ test('is.urlInstance', t => {
|
||||||
t.false(is.urlInstance(null));
|
t.false(is.urlInstance(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('is.url', t => {
|
||||||
|
const url = 'https://www.example.com';
|
||||||
|
t.true(is.url(url));
|
||||||
|
t.false(is.url(new URL(url)));
|
||||||
|
t.false(is.url({}));
|
||||||
|
t.false(is.url(undefined));
|
||||||
|
t.false(is.url(null));
|
||||||
|
});
|
||||||
|
|
||||||
test('is.truthy', t => {
|
test('is.truthy', t => {
|
||||||
t.true(is.truthy('unicorn'));
|
t.true(is.truthy('unicorn'));
|
||||||
t.true(is.truthy('🦄'));
|
t.true(is.truthy('🦄'));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue