Skip to content

Commit

Permalink
feat: added tryParseURL (#482)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
legendhimself and favna committed Oct 8, 2022
1 parent 3eb5175 commit 46c5b49
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 42 deletions.
24 changes: 17 additions & 7 deletions packages/utilities/package.json
Expand Up @@ -216,10 +216,10 @@
"import": "./dist/lib/objectValues.mjs",
"require": "./dist/lib/objectValues.js"
},
"./parseUrl": {
"types": "./dist/lib/parseUrl.d.ts",
"import": "./dist/lib/parseUrl.mjs",
"require": "./dist/lib/parseUrl.js"
"./parseURL": {
"types": "./dist/lib/tryParseURL.d.ts",
"import": "./dist/lib/tryParseURL.mjs",
"require": "./dist/lib/tryParseURL.js"
},
"./partition": {
"types": "./dist/lib/partition.d.ts",
Expand Down Expand Up @@ -262,9 +262,19 @@
"require": "./dist/lib/toTitleCase.js"
},
"./tryParse": {
"types": "./dist/lib/tryParse.d.ts",
"import": "./dist/lib/tryParse.mjs",
"require": "./dist/lib/tryParse.js"
"types": "./dist/lib/tryParseJSON.d.ts",
"import": "./dist/lib/tryParseJSON.mjs",
"require": "./dist/lib/tryParseJSON.js"
},
"./tryParseJSON": {
"types": "./dist/lib/tryParseJSON.d.ts",
"import": "./dist/lib/tryParseJSON.mjs",
"require": "./dist/lib/tryParseJSON.js"
},
"./tryParseURL": {
"types": "./dist/lib/tryParseURL.d.ts",
"import": "./dist/lib/tryParseURL.mjs",
"require": "./dist/lib/tryParseURL.js"
},
"./types": {
"types": "./dist/lib/types.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/utilities/src/index.ts
Expand Up @@ -30,13 +30,13 @@ export * from './lib/objectEntries';
export * from './lib/objectKeys';
export * from './lib/objectToTuples';
export * from './lib/objectValues';
export * from './lib/parseUrl';
export * from './lib/partition';
export * from './lib/range';
export * from './lib/regExpEsc';
export * from './lib/roundNumber';
export { sleep, sleepSync, AbortError, type SleepOptions } from './lib/sleep';
export { AbortError, sleep, sleepSync, type SleepOptions } from './lib/sleep';
export * from './lib/splitText';
export { toTitleCase, type ToTitleCaseOptions } from './lib/toTitleCase';
export * from './lib/tryParse';
export * from './lib/tryParseJSON';
export * from './lib/tryParseURL';
export * from './lib/types';
14 changes: 0 additions & 14 deletions packages/utilities/src/lib/parseUrl.ts

This file was deleted.

Expand Up @@ -2,10 +2,12 @@
* Try parse a stringified JSON string.
* @param value The value to parse
*/
export function tryParse(value: string): object | string {
export function tryParseJSON(value: string): object | string | number {
try {
return JSON.parse(value);
} catch (err) {
return value;
}
}

export { tryParseJSON as tryParse };
14 changes: 14 additions & 0 deletions packages/utilities/src/lib/tryParseURL.ts
@@ -0,0 +1,14 @@
/**
* Tries parse a string to a {@link URL} object
* @param value The possible URL to parse
* @returns an URL object if it was a valid URL or `null` if it was not.
*/
export function tryParseURL(value: string): URL | null {
try {
return new URL(value);
} catch {
return null;
}
}

export { tryParseURL as parseURL };
12 changes: 0 additions & 12 deletions packages/utilities/tests/parseUrl.test.ts

This file was deleted.

@@ -1,15 +1,15 @@
import { tryParse } from '../src';
import { tryParseJSON } from '../src';

describe('tryParse', () => {
describe('tryParseJSON', () => {
test('GIVEN basic THEN returns expected', () => {
const source = '{"a":4,"b":6}';
const expected = { a: 4, b: 6 };
expect(tryParse(source)).toEqual(expected);
expect(tryParseJSON(source)).toEqual(expected);
});

test('GIVEN invalid THEN returns expected', () => {
const source = '{"a":4,"b:6}';
const expected = '{"a":4,"b:6}';
expect(tryParse(source)).toEqual(expected);
expect(tryParseJSON(source)).toEqual(expected);
});
});
12 changes: 12 additions & 0 deletions packages/utilities/tests/tryParseURL.test.ts
@@ -0,0 +1,12 @@
import { URL } from 'url';
import { tryParseURL } from '../src';

describe('tryParseURL', () => {
test('GIVEN valid URL THEN returns URL', () => {
expect(tryParseURL('https://skyra.pw')).toStrictEqual(new URL('https://skyra.pw'));
});

test('GIVEN invalid url THEN returns null', () => {
expect(tryParseURL('thisisnotaurl')).toBeNull();
});
});
4 changes: 3 additions & 1 deletion scripts/aliases/utilities.mjs
Expand Up @@ -5,5 +5,7 @@ export const aliasStore = new Map([
['isNullOrUndefined', 'isNullish'],
['isNullOrUndefinedOrEmpty', 'isNullishOrEmpty'],
['isNullOrUndefinedOrZero', 'isNullishOrZero'],
['sleep', 'sleepSync']
['sleep', 'sleepSync'],
['tryParseJSON', 'tryParse'],
['tryParseURL', 'parseURL']
]);

0 comments on commit 46c5b49

Please sign in to comment.