Add is.urlString (#73)

This commit is contained in:
Itai Steinherz 2018-12-13 17:52:21 +02:00 committed by Sindre Sorhus
parent 2ee148f5a1
commit 566f363632
3 changed files with 40 additions and 1 deletions

View file

@ -3,6 +3,7 @@
/// <reference lib="esnext.asynciterable"/>
/// <reference lib="dom"/>
import symbolObservable from 'symbol-observable';
import {URL} from 'url';
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
type Primitive = null | undefined | string | number | boolean | Symbol;
@ -189,6 +190,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 urlInstance = (value: unknown): value is URL => isObjectOfType<URL>(TypeName.URL)(value);
export const urlString = (value: unknown) => {
if (!string(value)) {
return false;
}
try {
new URL(value); // tslint:disable-line no-unused-expression
return true;
} catch {
return false;
}
};
export const truthy = (value: unknown) => Boolean(value);
export const falsy = (value: unknown) => !value;