From 2efd6529f2f14a8cd06015d72baa17f11f6915de Mon Sep 17 00:00:00 2001 From: trydofor Date: Fri, 20 Dec 2024 11:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20formated=20type=20of=20float/long/d?= =?UTF-8?q?ata/time=20#127?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/tidy-worms-design.md | 6 ++ layers/common/tests/common-type.test.ts | 107 ++++++++++++++++++++++++ layers/common/types/common.global.d.ts | 41 +++++++++ 3 files changed, 154 insertions(+) create mode 100644 .changeset/tidy-worms-design.md create mode 100644 layers/common/tests/common-type.test.ts diff --git a/.changeset/tidy-worms-design.md b/.changeset/tidy-worms-design.md new file mode 100644 index 0000000..72be564 --- /dev/null +++ b/.changeset/tidy-worms-design.md @@ -0,0 +1,6 @@ +--- +"@fessional/razor-common": patch +"@fessional/razor-mobile": patch +--- + +✨ formated type and safeNumber,safeInt diff --git a/layers/common/tests/common-type.test.ts b/layers/common/tests/common-type.test.ts new file mode 100644 index 0000000..8ce9746 --- /dev/null +++ b/layers/common/tests/common-type.test.ts @@ -0,0 +1,107 @@ +import { expect, test } from 'vitest'; + +test('StrInt type formats', () => { + // Correct formats (these should pass the type check) + const good: StrInt[] = [ + '+1234567890', + '-1234567890', + '1234567890', + '+0', + '-0', + '0', + '0B0101', + '0XFF', + ]; + expect(good).toBeTruthy(); + + // is not assignable to type 'StrInt' + // const bad: StrInt[] = [ + // '', + // 'Infinity', + // 'NaN', + // 'a', + // '+123.5', + // '-123.5', + // '3.14e1', + // ]; +}); + +test('StrNum type formats', () => { + // Correct formats (these should pass the type check) + const good: StrNum[] = [ + '+1234567890', + '-1234567890', + '1234567890', + '+123.5', + '-123.5', + '123.5', + '+0', + '-0', + '0', + '3.14e1', + '0B0101', + '0XFF', + ]; + expect(good).toBeTruthy(); + + // is not assignable to type 'StrNum' + // const bad: StrNum[] = [ + // '', + // 'Infinity', + // 'NaN', + // 'a', + // ]; +}); + +test('StrDate type formats', () => { + // Correct formats (these should pass the type check) + const good: StrDate[] = [ + '222222-3333-4444', + '2024-12-20', + ]; + expect(good).toBeTruthy(); + + // is not assignable to type 'StrDate' + // const bad: StrDate[] = [ + // '', + // '-2024-12-20', + // '2024', + // '2024-', + // '2024-12', + // '2024-12-', + // '2024-12-13.', + // 'a-b-b', + // ]; +}); + +test('StrTime type formats', () => { + // Correct formats (these should pass the type check) + const good: StrTime[] = [ + '222222:3333:4444', + '2024:12:20', + '2024:12:20.567', + ]; + expect(good).toBeTruthy(); + + // is not assignable to type 'StrTime' + // const bad: StrTime[] = [ + // '', + // '2024:12:', + // '2024:12', + // '2024:', + // '2024', + // '2024:12:3.14e1', + // 'a:b:b', + // ]; +}); + +test('StrDateTime type formats', () => { + // Correct formats (these should pass the type check) + const good: StrDateTime[] = [ + '2024-12-20 22:33:44', + '2024-12-20T22:33:44', + '2024-12-20 22:33:44.555', + '2024-12-20T22:33:44.555', + ]; + expect(good).toBeTruthy(); +}); diff --git a/layers/common/types/common.global.d.ts b/layers/common/types/common.global.d.ts index f33c2b5..3773a5c 100644 --- a/layers/common/types/common.global.d.ts +++ b/layers/common/types/common.global.d.ts @@ -36,6 +36,47 @@ * short number value of true or false */ type BoolNum = 1 | 0; + + /** + * string style of a bigint or int value. + * - good: '123', '0', '-456', '0B0101', '0XFF' + * - bad: '', 'Infinity', 'NaN', 'a', '+123.5', '-123.5', '3.14e1' + */ + type StrInt = `${bigint}` | `+${bigint}`; + + /** + * string style of a number, including both integers and floating point numbers. + * - good: '123', '45.67', '-89.01' '0', '3.14e1', '0B0101', '0XFF' + * - bad: '', 'Infinity', 'NaN', 'a' + */ + type StrNum = `${number}`; + + /** + * simple date format in the form of 'yyyy-mm-dd'. + * - good: '222222-3333-4444', '2024-12-20' + * - bad: '', '-2024-12-20', '2024', '2024-', '2024-12', '2024-12-', '2024-12-13.', 'a-b-b' + */ + type StrDate = `${bigint}-${bigint}-${bigint}`; + + /** + * simple time format in the form of 'HH:MM:ss' or 'HH:MM:ss.SSS'. + * - good: '222222:3333:4444', '2024:12:20', '2024:12:20.567' + * - bad: '', '2024:12:', '2024:12', '2024:', '2024', '2024:12:3.14e1', 'a:b:b', + */ + type StrTime = `${bigint}:${bigint}:${bigint}` | `${bigint}:${bigint}:${bigint}.${bigint}`; + + /** + * simple date and time in either of two formats: + * - `${StrDate} ${StrTime}`: 'yyyy-mm-dd HH:MM:ss' (e.g., '2024-12-25 14:30:45') + * - `${StrDate}T${StrTime}`: 'yyyy-mm-ddTHH:MM:ss' (e.g., '2024-12-25T14:30:45') + * + * avoid Expression produces a union type that is too complex to represent.ts + */ + type StrDateTime = `${bigint}-${bigint}-${bigint} ${bigint}:${bigint}:${bigint}` + | `${bigint}-${bigint}-${bigint}T${bigint}:${bigint}:${bigint}` + | `${bigint}-${bigint}-${bigint} ${bigint}:${bigint}:${bigint}.${bigint}` + | `${bigint}-${bigint}-${bigint}T${bigint}:${bigint}:${bigint}.${bigint}`; + } //