From 1974d8f58b460953f9c6577f28963dc4985daa67 Mon Sep 17 00:00:00 2001 From: "Acid Chicken (硫酸鶏)" Date: Wed, 6 Feb 2019 13:37:20 +0900 Subject: Add URL validation (#4148) --- src/config/load.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/config/load.ts b/src/config/load.ts index fc3e699199..5bb01f3d41 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -47,14 +47,21 @@ export default function load() { return Object.assign(config, mixin); } -function validateUrl(url: string) { +function tryCreateUrl(url: string) { try { return new URL(url); } catch (e) { - throw `url="${url}" is not a valid URL`; + throw `url="${url}" is not a valid URL.`; } } +function validateUrl(url: string) { + const result = tryCreateUrl(url); + if (result.pathname.replace('/', '').length) throw `url="${url}" is not a valid URL, has a pathname.`; + if (!url.includes(result.host)) throw `url="${url}" is not a valid URL, has an invalid hostname.`; + return result; +} + function normalizeUrl(url: string) { return url.endsWith('/') ? url.substr(0, url.length - 1) : url; } -- cgit v1.2.3-freya From e9955e01d60773b96017acc1c6cd3dfae4dfb3e6 Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Wed, 6 Feb 2019 13:42:35 +0900 Subject: Introduce option type (#4150) * Introduce option type * Improve test naming --- src/prelude/maybe.ts | 20 ++++++++++++++++++++ test/prelude/maybe.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/prelude/maybe.ts create mode 100644 test/prelude/maybe.ts (limited to 'src') diff --git a/src/prelude/maybe.ts b/src/prelude/maybe.ts new file mode 100644 index 0000000000..f9ac95c0b5 --- /dev/null +++ b/src/prelude/maybe.ts @@ -0,0 +1,20 @@ +export interface Maybe { + isJust(): this is Just; +} + +export type Just = Maybe & { + get(): T +}; + +export function just(value: T): Just { + return { + isJust: () => true, + get: () => value + }; +} + +export function nothing(): Maybe { + return { + isJust: () => false, + }; +} diff --git a/test/prelude/maybe.ts b/test/prelude/maybe.ts new file mode 100644 index 0000000000..470eec220a --- /dev/null +++ b/test/prelude/maybe.ts @@ -0,0 +1,28 @@ +/* + * Tests of Maybe + * + * How to run the tests: + * > mocha test/prelude/maybe.ts --require ts-node/register + * + * To specify test: + * > mocha test/prelude/maybe.ts --require ts-node/register -g 'test name' + */ + +import * as assert from 'assert'; +import { just, nothing } from '../../src/prelude/maybe'; + +describe('just', () => { + it('has a value', () => { + assert.deepStrictEqual(just(3).isJust(), true); + }); + + it('has the inverse called get', () => { + assert.deepStrictEqual(just(3).get(), 3); + }); +}); + +describe('nothing', () => { + it('has no value', () => { + assert.deepStrictEqual(nothing().isJust(), false); + }); +}); -- cgit v1.2.3-freya From b299988bb5472210de291a96627bc9b57d474684 Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Wed, 6 Feb 2019 13:52:32 +0900 Subject: Simplify comment (#4164) --- src/misc/logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/misc/logger.ts b/src/misc/logger.ts index 609c255849..7b669c3bb1 100644 --- a/src/misc/logger.ts +++ b/src/misc/logger.ts @@ -45,7 +45,7 @@ export default class Logger { this.log(important ? chalk.bgGreen.white('DONE') : chalk.green('DONE'), chalk.green(message), important); } - public debug(message: string, important = false): void { // デバッグ用に使う(開発者にとっては必要だが利用者にとっては不要な情報) + public debug(message: string, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報) if (process.env.NODE_ENV != 'production' || program.verbose) { this.log(chalk.gray('VERB'), chalk.gray(message), important); } -- cgit v1.2.3-freya