Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New methods: isURL/isURLObject #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,29 @@ isUndefined ( undefined ); // => true
isUndefined ( null ); // => false
```

#### `isURL` 🆕

Checks if a value is a valid URL.

```ts
import {isURL} from 'is';

isURL ( 'https://github.com/fabiospampinato/is' ); // => true
isURL ( new URL ( 'https://github.com/fabiospampinato/is' ) ); // => true
isURL ( 'invalid' ); // => false
```

#### `isURLObject` 🆕

Checks if a value is a URL object.

```ts
import {isURLObject} from 'is';

isURLObject ( new URL ( 'https://github.com/fabiospampinato/is' ) ); // => true
isURLObject ( 'https://github.com/fabiospampinato/is' ); // => false
```

#### `isWeakMap`

Checks if a value is a WeakMap.
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ import isUint32Array from './is_uint32array';
import isUint8Array from './is_uint8array';
import isUint8ClampedArray from './is_uint8clampedarray';
import isUndefined from './is_undefined';
import isURL from './is_url';
import isURLObject from './is_object'
import isWeakMap from './is_weak_map';
import isWeakRef from './is_weak_ref';
import isWeakReferable from './is_weak_referable';
Expand Down Expand Up @@ -170,6 +172,8 @@ export {
isUint8Array,
isUint8ClampedArray,
isUndefined,
isURL,
isURLObject,
isWeakMap,
isWeakRef,
isWeakReferable,
Expand Down
12 changes: 12 additions & 0 deletions src/is_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

/* MAIN */

const isURL = ( value: unknown ): value is string | URL => {

return URL.canParse ( String ( value ) );

};

/* EXPORT */

export default isURL;
12 changes: 12 additions & 0 deletions src/is_url_object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

/* MAIN */

const isURLObject = ( value: unknown ): value is URL => {

return value?.constructor === URL;

};

/* EXPORT */

export default isURLObject;
1 change: 1 addition & 0 deletions test/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const realm = vm.runInNewContext ( /* javascript */`
'string': 'a',
'symbol': Symbol (),
'undefined': undefined,
'url': 'https://github.com/fabiospampinato/is',
'weakMap': new WeakMap (),
'weakRef': new WeakRef ( WeakRef ),
'weakSet': new WeakSet ()
Expand Down
48 changes: 48 additions & 0 deletions test/is_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

/* IMPORT */

import {describe} from 'fava';
import {isURL} from '../dist/index.js';
import {realm} from './_utils.js';

/* MAIN */

describe ( 'isURL', test => {

test ( 'should return "true" for a valid URL string or URL object', t => {

const url = 'https://github.com/fabiospampinato/is';
const blob = new Blob ( [ url ], { type: 'text/plain' } );

t.true ( isURL ( url ) );
t.true ( isURL ( new URL ( url ) ) );
t.true ( isURL ( URL.createObjectURL ( blob ) ) );
t.true ( isURL ( 'http://www.github.com/fabiospampinato/is/' ) );

});

test ( 'should return "false" for everything else', t => {

t.false ( isURL ( 'invalid' ) );
t.false ( isURL ( 'www.github.com' ) );
t.false ( isURL ( 'github.com/' ) );
t.false ( isURL ( '' ) );
t.false ( isURL () );
t.false ( isURL ( true ) );
t.false ( isURL ( false ) );
t.false ( isURL ( 0 ) );
t.false ( isURL ( null ) );
t.false ( isURL ( undefined ) );
t.false ( isURL ( NaN ) );
t.false ( isURL ( [] ) );
t.false ( isURL ( {} ) );

});

test ( 'should work with a URL from another realm', t => {

t.true ( isURL ( realm.url ) );

});

});
39 changes: 39 additions & 0 deletions test/is_url_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

/* IMPORT */

import {describe} from 'fava';
import {isURLObject} from '../dist/index.js';

/* MAIN */

describe ( 'isURLObject', test => {

const url = 'https://github.com/fabiospampinato/is';
const blob = new Blob ( [ url ], { type: 'text/plain' } );

test ( 'should return "true" for a URL object', t => {

t.true ( isURLObject ( new URL ( url ) ) );

});

test ( 'should return "false" for everything else', t => {

t.false ( isURLObject ( url ) );
t.false ( isURLObject ( URL.createObjectURL ( blob ) ) );
t.false ( isURLObject ( 'http://www.github.com/fabiospampinato/is/' ) );
t.false ( isURLObject ( 'invalid' ) );
t.false ( isURLObject ( 'www.github.com' ) );
t.false ( isURLObject ( 'github.com/' ) );
t.false ( isURLObject ( '' ) );
t.false ( isURLObject () );
t.false ( isURLObject ( true ) );
t.false ( isURLObject ( false ) );
t.false ( isURLObject ( 0 ) );
t.false ( isURLObject ( null ) );
t.false ( isURLObject ( undefined ) );
t.false ( isURLObject ( NaN ) );

});

});