Skip to content

Commit de540b5

Browse files
committed
feat: added isUrlString()
1 parent 3b76e1d commit de540b5

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/predicate/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ export * from './isSymbolArray.js';
5656
export * from './isUndefined.js';
5757
export * from './isUndefinedArray.js';
5858
export * from './isUnknown.js';
59+
export * from './isUrlString.js';

src/predicate/isUrlString.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isUrlString = (input: unknown): input is string => {
2+
return typeof input === 'string' && URL.canParse(input);
3+
};

test/predicate/isUrlString.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { isUrlString } from '../../src/predicate/isUrlString.js';
4+
5+
describe('isUrlString()', () => {
6+
it.each(['http://example.com', 'https://example.com'])('Returns true for valid input: %j', (input) => {
7+
expect(isUrlString(input)).toBeTruthy();
8+
});
9+
10+
it.each([1, 'a', true, null, {}, undefined])('Returns false for invalid input: %j', (input) => {
11+
expect(isUrlString(input)).toBeFalsy();
12+
});
13+
});

0 commit comments

Comments
 (0)