From 3bdaf21475a6046c7b0aa68ac6ef5cf19022a2d2 Mon Sep 17 00:00:00 2001 From: Artur Androsovych Date: Tue, 10 Jul 2018 12:04:20 +0300 Subject: [PATCH] Add `is.urlInstance` (#60) --- readme.md | 11 +++++++++++ source/index.ts | 4 +++- source/tests/test.ts | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 26031e8..51a8b0d 100644 --- a/readme.md +++ b/readme.md @@ -176,6 +176,17 @@ is.directInstanceOf(new UnicornError(), Error); //=> false ``` +##### .urlInstance(value) + +Returns `true` if `value` is an instance of the [`URL` class](https://developer.mozilla.org/en-US/docs/Web/API/URL). + +```js +const url = new URL('https://example.com'); + +is.urlInstance(url); +//=> true +``` + ##### .truthy(value) Returns `true` for all values that evaluate to true in a boolean context: diff --git a/source/index.ts b/source/index.ts index 868ec18..c6aeea2 100644 --- a/source/index.ts +++ b/source/index.ts @@ -48,7 +48,8 @@ export const enum TypeName { ArrayBuffer = 'ArrayBuffer', SharedArrayBuffer = 'SharedArrayBuffer', DataView = 'DataView', - Promise = 'Promise' + Promise = 'Promise', + URL = 'URL' } const toString = Object.prototype.toString; @@ -177,6 +178,7 @@ namespace is { // tslint:disable-line:no-namespace export const dataView = isObjectOfType(TypeName.DataView); export const directInstanceOf = (instance: any, klass: Class): instance is T => Object.getPrototypeOf(instance) === klass.prototype; + export const urlInstance = (value: any): value is URL => isObjectOfType(TypeName.URL)(value); export const truthy = (value: any) => Boolean(value); export const falsy = (value: any) => !value; diff --git a/source/tests/test.ts b/source/tests/test.ts index a2d56ad..810b42f 100644 --- a/source/tests/test.ts +++ b/source/tests/test.ts @@ -3,6 +3,7 @@ import net from 'net'; import Stream from 'stream'; import util from 'util'; import tempy from 'tempy'; +import {URL} from 'url'; import test, {TestContext, Context} from 'ava'; import {JSDOM} from 'jsdom'; import {Subject, Observable} from 'rxjs'; @@ -534,6 +535,14 @@ test('is.directInstanceOf', t => { t.false(m.directInstanceOf(errorSubclass, Error)); }); +test('is.urlInstance', t => { + const url = new URL('https://www.example.com'); + t.true(m.urlInstance(url)); + t.false(m.urlInstance({})); + t.false(m.urlInstance(undefined)); + t.false(m.urlInstance(null)); +}); + test('is.truthy', t => { t.true(m.truthy('unicorn')); t.true(m.truthy('🦄'));