-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from nemuvski/new-feature-utils
関数と型ユーティリティを追加, 一部関数のドキュメント追記
- Loading branch information
Showing
22 changed files
with
494 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* decodeURIComponent()で発生する例外をキャッチして、例外が発生した場合はエラーオブジェクトを返却する | ||
* | ||
* @param value | ||
* @returns {{ data: string; error: null } | { data: null; error: URIError }} | ||
* @example | ||
* const { data, error } = safeDecodeURIComponent('foo%20bar') // => { data: 'foo bar', error: null } | ||
* if (error) { | ||
* console.error(error) | ||
* } else { | ||
* console.log(data) // => 'foo bar' | ||
* } | ||
*/ | ||
export function safeDecodeURIComponent(value: string): { data: string; error: null } | { data: null; error: URIError } { | ||
try { | ||
const data = decodeURIComponent(value) | ||
return { data, error: null } | ||
} catch (e) { | ||
return { | ||
data: null, | ||
/** | ||
* try中で発生する例外はdecodeURIComponentの仕様によるものなので、SyntaxErrorでアサーションしている | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#exceptions} | ||
*/ | ||
error: e as URIError, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* encodeURIComponent()で発生する例外をキャッチして、例外が発生した場合はエラーオブジェクトを返却する | ||
* | ||
* @param value | ||
* @returns {{ data: string; error: null } | { data: null; error: URIError }} | ||
* @example | ||
* const { data, error } = safeEncodeURIComponent('foo bar') // => { data: 'foo%20bar', error: null } | ||
* if (error) { | ||
* console.error(error) | ||
* } else { | ||
* console.log(data) // => 'foo%20bar' | ||
* } | ||
*/ | ||
export function safeEncodeURIComponent(value: string): { data: string; error: null } | { data: null; error: URIError } { | ||
try { | ||
const data = encodeURIComponent(value) | ||
return { data, error: null } | ||
} catch (e) { | ||
return { | ||
data: null, | ||
/** | ||
* try中で発生する例外はencodeURIComponentの仕様によるものなので、SyntaxErrorでアサーションしている | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#exceptions} | ||
*/ | ||
error: e as SyntaxError, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* JSON.parse()で発生する例外をキャッチして、例外が発生した場合はエラーオブジェクトを返す | ||
* | ||
* 型変数 `T` はJSON.parse()でパースするデータの型を指定する | ||
* | ||
* ※ ただし、型ガードは行わないため、呼び出し元で型ガードを適宜実施すること | ||
* | ||
* @param value | ||
* @param options | ||
* @returns {{ data: T; error: null } | { data: null; error: SyntaxError }} | ||
* @example | ||
* const { data, error } = safeJsonParse<{ foo: string }>('{"foo": "bar"}') // => { data: { foo: 'bar' }, error: null } | ||
* if (error) { | ||
* console.error(error) | ||
* } else { | ||
* console.log(data) // => { foo: 'bar' } | ||
* } | ||
*/ | ||
export function safeJsonParse<T>( | ||
value: string, | ||
options?: { reviver?: Parameters<typeof JSON.parse>[1] } | ||
): { data: T; error: null } | { data: null; error: SyntaxError } { | ||
try { | ||
const data = JSON.parse(value, options?.reviver) | ||
return { data, error: null } | ||
} catch (e) { | ||
return { | ||
data: null, | ||
/** | ||
* try中で発生する例外はJSON.parseの仕様によるものなので、SyntaxErrorでアサーションしている | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#exceptions} | ||
*/ | ||
error: e as SyntaxError, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* JSON.stringify()で発生する例外をキャッチして、例外が発生した場合はエラーオブジェクトを返す | ||
* | ||
* @param value | ||
* @param options | ||
* @returns {{ data: string; error: null } | { data: null; error: TypeError }} | ||
* @example | ||
* const { data, error } = safeJsonStringify({ foo: 'bar' }) // => { data: '{"foo":"bar"}', error: null } | ||
* if (error) { | ||
* console.error(error) | ||
* } else { | ||
* console.log(data) // => '{"foo":"bar"}' | ||
* } | ||
*/ | ||
export function safeJsonStringify<T>( | ||
value: T, | ||
options?: { | ||
replacer?: Parameters<typeof JSON.stringify>[1] | ||
space?: Parameters<typeof JSON.stringify>[2] | ||
} | ||
): { data: string; error: null } | { data: null; error: TypeError } { | ||
try { | ||
const data = JSON.stringify(value, options?.replacer, options?.space) | ||
return { data, error: null } | ||
} catch (e) { | ||
return { | ||
data: null, | ||
/** | ||
* try中で発生する例外はJSON.stringifyの仕様によるものなので、TypeErrorでアサーションしている | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions} | ||
*/ | ||
error: e as TypeError, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/utils/tests/functions/safeDecodeURIComponent.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { safeDecodeURIComponent } from '../../src' | ||
|
||
describe('safeDecodeURIComponent()', () => { | ||
test('デコードされた値が返ってくる', () => { | ||
expect(safeDecodeURIComponent('%3Fx%3Dtest')).toEqual({ data: '?x=test', error: null }) | ||
expect(safeDecodeURIComponent('foo%20bar')).toEqual({ data: 'foo bar', error: null }) | ||
expect(safeDecodeURIComponent('%E3%83%86%E3%82%B9%E3%83%88')).toEqual({ data: 'テスト', error: null }) | ||
}) | ||
|
||
test('URIErrorが返ってくる', () => { | ||
const { data, error } = safeDecodeURIComponent('%') | ||
expect(data).toBeNull() | ||
expect(error).toBeInstanceOf(URIError) | ||
}) | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/utils/tests/functions/safeEncodeURIComponent.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { safeEncodeURIComponent } from '../../src' | ||
|
||
describe('safeEncodeURIComponent()', () => { | ||
test('エンコードされた値が返ってくる', () => { | ||
expect(safeEncodeURIComponent('?x=test')).toEqual({ data: '%3Fx%3Dtest', error: null }) | ||
expect(safeEncodeURIComponent('foo bar')).toEqual({ data: 'foo%20bar', error: null }) | ||
expect(safeEncodeURIComponent('テスト')).toEqual({ data: '%E3%83%86%E3%82%B9%E3%83%88', error: null }) | ||
}) | ||
|
||
test('URIErrorが返ってくる', () => { | ||
const { data, error } = safeEncodeURIComponent('\uD800') | ||
expect(data).toBeNull() | ||
expect(error).toBeInstanceOf(URIError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { safeJsonParse } from '../../src' | ||
|
||
describe('safeJsonParse()', () => { | ||
describe('パースに成功する', () => { | ||
test('optionsなし', () => { | ||
const jsonString = '{"foo": "bar"}' | ||
const { data, error } = safeJsonParse<{ foo: string }>(jsonString) | ||
expect(data).toEqual({ foo: 'bar' }) | ||
expect(error).toBeNull() | ||
}) | ||
|
||
test('options.reviverあり', () => { | ||
const jsonString = '{"foo": "bar"}' | ||
const { data, error } = safeJsonParse<{ foo: string }>(jsonString, { | ||
reviver: (key, value) => { | ||
if (key === 'foo') { | ||
return `${value}!` | ||
} | ||
return value | ||
}, | ||
}) | ||
expect(data).toEqual({ foo: 'bar!' }) | ||
expect(error).toBeNull() | ||
}) | ||
}) | ||
|
||
test('パースに失敗する', () => { | ||
const jsonString = '{"foo": "bar"' | ||
const { data, error } = safeJsonParse(jsonString) | ||
expect(data).toBeNull() | ||
expect(error).toBeInstanceOf(SyntaxError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { safeJsonStringify } from '../../src' | ||
|
||
describe('safeJsonStringify()', () => { | ||
describe('文字列化に成功する', () => { | ||
test('optionsなし', () => { | ||
const { data, error } = safeJsonStringify({ foo: 'bar' }) | ||
expect(data).toBe('{"foo":"bar"}') | ||
expect(error).toBeNull() | ||
}) | ||
|
||
test('options.replacerあり (関数のケース)', () => { | ||
const target = { foo: 'bar', hoo: 3, goo: true } | ||
const { data, error } = safeJsonStringify(target, { | ||
// @ts-ignore: 問題ないので型エラーを無視 | ||
replacer: (key: keyof typeof target, value: (typeof target)[keyof typeof target]) => { | ||
if (key === 'foo') { | ||
return `${value}!` | ||
} | ||
if (key === 'hoo' && typeof value === 'number') { | ||
return value * 2 | ||
} | ||
return value | ||
}, | ||
}) | ||
expect(data).toBe('{"foo":"bar!","hoo":6,"goo":true}') | ||
expect(error).toBeNull() | ||
}) | ||
|
||
test('options.replacerあり (配列のケース)', () => { | ||
const target = { foo: 'bar', hoo: 3, goo: true, 3: 'three' } | ||
const { data, error } = safeJsonStringify(target, { | ||
replacer: ['foo', 'hoo', 3], | ||
}) | ||
expect(data).toBe('{"foo":"bar","hoo":3,"3":"three"}') | ||
expect(error).toBeNull() | ||
}) | ||
|
||
test('options.spaceあり (数値のケース)', () => { | ||
const target = { foo: 'bar' } | ||
const { data, error } = safeJsonStringify(target, { | ||
space: 2, | ||
}) | ||
expect(data).toBe('{\n "foo": "bar"\n}') | ||
expect(error).toBeNull() | ||
}) | ||
|
||
test('options.spaceあり (文字のケース)', () => { | ||
const target = { foo: 'bar' } | ||
const { data, error } = safeJsonStringify(target, { | ||
space: '\t', | ||
}) | ||
expect(data).toBe('{\n\t"foo": "bar"\n}') | ||
expect(error).toBeNull() | ||
}) | ||
}) | ||
|
||
test('文字列化に失敗する', () => { | ||
// BigInt は JSON.stringify で文字列化できないので例外が発生する | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions | ||
const { data, error } = safeJsonStringify({ bigint: 2n }) | ||
expect(data).toBeNull() | ||
expect(error).toBeInstanceOf(TypeError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.