Add is.urlString (#73)
This commit is contained in:
parent
2ee148f5a1
commit
566f363632
3 changed files with 40 additions and 1 deletions
16
readme.md
16
readme.md
|
|
@ -255,6 +255,22 @@ is.urlInstance(url);
|
||||||
//=> true
|
//=> true
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### .url(value)
|
||||||
|
|
||||||
|
Returns `true` if `value` is a URL string.
|
||||||
|
|
||||||
|
Note: this only does basic checking using the [`URL` class](https://developer.mozilla.org/en-US/docs/Web/API/URL) constructor.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const url = 'https://example.com';
|
||||||
|
|
||||||
|
is.url(url);
|
||||||
|
//=> true
|
||||||
|
|
||||||
|
is.url(new URL(url));
|
||||||
|
//=> false
|
||||||
|
```
|
||||||
|
|
||||||
##### .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:
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
/// <reference lib="esnext.asynciterable"/>
|
/// <reference lib="esnext.asynciterable"/>
|
||||||
/// <reference lib="dom"/>
|
/// <reference lib="dom"/>
|
||||||
import symbolObservable from 'symbol-observable';
|
import symbolObservable from 'symbol-observable';
|
||||||
|
import {URL} from 'url';
|
||||||
|
|
||||||
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
|
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
|
||||||
type Primitive = null | undefined | string | number | boolean | Symbol;
|
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 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 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 truthy = (value: unknown) => Boolean(value);
|
||||||
export const falsy = (value: unknown) => !value;
|
export const falsy = (value: unknown) => !value;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -576,13 +576,22 @@ test('is.directInstanceOf', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.urlInstance', t => {
|
test('is.urlInstance', t => {
|
||||||
const url = new URL('https://www.example.com');
|
const url = new URL('https://example.com');
|
||||||
t.true(is.urlInstance(url));
|
t.true(is.urlInstance(url));
|
||||||
t.false(is.urlInstance({}));
|
t.false(is.urlInstance({}));
|
||||||
t.false(is.urlInstance(undefined));
|
t.false(is.urlInstance(undefined));
|
||||||
t.false(is.urlInstance(null));
|
t.false(is.urlInstance(null));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('is.urlString', t => {
|
||||||
|
const url = 'https://example.com';
|
||||||
|
t.true(is.urlString(url));
|
||||||
|
t.false(is.urlString(new URL(url)));
|
||||||
|
t.false(is.urlString({}));
|
||||||
|
t.false(is.urlString(undefined));
|
||||||
|
t.false(is.urlString(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