Skip to content

Commit

Permalink
✨ formated type of float/long/data/time #127
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Dec 20, 2024
1 parent 57861f5 commit 2efd652
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/tidy-worms-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fessional/razor-common": patch
"@fessional/razor-mobile": patch
---

✨ formated type and safeNumber,safeInt
107 changes: 107 additions & 0 deletions layers/common/tests/common-type.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
41 changes: 41 additions & 0 deletions layers/common/types/common.global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

}

//
Expand Down

0 comments on commit 2efd652

Please sign in to comment.