@simplysm/core-common 14.0.4 → 14.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/types.md CHANGED
@@ -1,10 +1,8 @@
1
1
  # Types
2
2
 
3
- Immutable value types for UUID, date/time, and a self-expiring map.
4
-
5
3
  ## `Uuid`
6
4
 
7
- UUID v4 class. Uses `crypto.getRandomValues` for cryptographically secure generation.
5
+ UUID v4 class. Generates cryptographically secure UUIDs using `crypto.getRandomValues`.
8
6
 
9
7
  ```typescript
10
8
  class Uuid {
@@ -22,32 +20,27 @@ class Uuid {
22
20
 
23
21
  | Method | Description |
24
22
  |--------|-------------|
25
- | `generate()` | Creates a new random UUID v4 instance |
26
- | `fromBytes(bytes)` | Creates a UUID from a 16-byte `Uint8Array`. Throws `ArgumentError` if length is not 16 |
27
-
28
- ### Constructor
29
-
30
- | Parameter | Type | Description |
31
- |-----------|------|-------------|
32
- | `uuid` | `string` | UUID string in format `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`. Throws `ArgumentError` if invalid |
23
+ | `generate()` | Create a new random UUID v4 instance |
24
+ | `fromBytes(bytes)` | Create UUID from a 16-byte `Uint8Array`. Throws `ArgumentError` if length is not 16. |
33
25
 
34
26
  ### Instance Methods
35
27
 
36
28
  | Method | Returns | Description |
37
29
  |--------|---------|-------------|
38
- | `toString()` | `string` | Returns the UUID string representation |
39
- | `toBytes()` | `Bytes` | Converts to a 16-byte `Uint8Array` |
30
+ | `toString()` | `string` | UUID string in `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` format |
31
+ | `toBytes()` | `Bytes` | 16-byte `Uint8Array` representation |
40
32
 
41
- ```typescript
42
- const id = Uuid.generate();
43
- const fromStr = new Uuid("550e8400-e29b-41d4-a716-446655440000");
44
- const bytes = id.toBytes(); // Uint8Array(16)
45
- const restored = Uuid.fromBytes(bytes);
46
- ```
33
+ ### Constructor
34
+
35
+ | Parameter | Type | Description |
36
+ |-----------|------|-------------|
37
+ | `uuid` | `string` | UUID string. Must match format `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`. Throws `ArgumentError` if invalid. |
38
+
39
+ ---
47
40
 
48
41
  ## `LazyGcMap<TKey, TValue>`
49
42
 
50
- A Map with automatic expiration. Entries not accessed within `expireTime` are garbage collected. Supports `using` syntax (Symbol.dispose).
43
+ Auto-expiring Map with LRU-style access tracking. Items are automatically deleted after the configured expire time if not accessed. Must be disposed after use to stop the GC timer.
51
44
 
52
45
  ```typescript
53
46
  class LazyGcMap<TKey, TValue> {
@@ -62,52 +55,46 @@ class LazyGcMap<TKey, TValue> {
62
55
  get(key: TKey): TValue | undefined;
63
56
  set(key: TKey, value: TValue): void;
64
57
  delete(key: TKey): boolean;
65
- getOrCreate(key: TKey, factory: () => TValue): TValue;
66
58
  clear(): void;
67
- dispose(): void;
68
- [Symbol.dispose](): void;
69
-
59
+ getOrCreate(key: TKey, factory: () => TValue): TValue;
70
60
  values(): IterableIterator<TValue>;
71
61
  keys(): IterableIterator<TKey>;
72
62
  entries(): IterableIterator<[TKey, TValue]>;
63
+ dispose(): void;
64
+ [Symbol.dispose](): void;
73
65
  }
74
66
  ```
75
67
 
76
68
  ### Constructor Options
77
69
 
78
- | Option | Type | Default | Description |
79
- |--------|------|---------|-------------|
80
- | `gcInterval` | `number` | `expireTime / 10` (min 1000ms) | Interval between GC runs (ms) |
81
- | `expireTime` | `number` | required | Time since last access before entry is removed (ms) |
82
- | `onExpire` | `(key, value) => void \| Promise<void>` | `undefined` | Callback when an entry expires |
70
+ | Option | Type | Description |
71
+ |--------|------|-------------|
72
+ | `gcInterval` | `number \| undefined` | GC check interval in ms. Default: `expireTime / 10` (min 1000ms). |
73
+ | `expireTime` | `number` | Time in ms after last access before an item is expired. |
74
+ | `onExpire` | `(key, value) => void \| Promise<void>` | Callback invoked when an item expires. Errors are logged, not thrown. |
83
75
 
84
76
  ### Methods
85
77
 
86
- | Method | Description |
87
- |--------|-------------|
88
- | `has(key)` | Check if key exists (does NOT refresh access time) |
89
- | `get(key)` | Get value and refresh access time (LRU) |
90
- | `set(key, value)` | Set value and start GC timer if needed |
91
- | `delete(key)` | Delete entry; stops GC if map becomes empty |
92
- | `getOrCreate(key, factory)` | Get or create value. Throws if disposed |
93
- | `clear()` | Remove all entries (instance remains usable) |
94
- | `dispose()` | Stop GC timer and clear all data. Instance becomes unusable |
95
-
96
- **Important:** Always call `dispose()` or use `using` to prevent memory leaks from the GC timer.
97
-
98
- ```typescript
99
- using map = new LazyGcMap<string, Connection>({
100
- expireTime: 60_000,
101
- onExpire: async (_key, conn) => { await conn.close(); },
102
- });
103
-
104
- map.set("conn1", createConnection());
105
- const conn = map.get("conn1"); // refreshes access time
106
- ```
78
+ | Method | Returns | Description |
79
+ |--------|---------|-------------|
80
+ | `size` | `number` | Number of stored items |
81
+ | `has(key)` | `boolean` | Check if key exists (does not refresh access time) |
82
+ | `get(key)` | `TValue \| undefined` | Get value and refresh access time (LRU) |
83
+ | `set(key, value)` | `void` | Store value and start GC timer if needed |
84
+ | `delete(key)` | `boolean` | Remove item. Stops GC timer if map becomes empty. |
85
+ | `clear()` | `void` | Remove all items (instance remains usable) |
86
+ | `getOrCreate(key, factory)` | `TValue` | Get existing value or create via factory, store, and return. Throws if disposed. |
87
+ | `values()` | `IterableIterator<TValue>` | Iterate over values |
88
+ | `keys()` | `IterableIterator<TKey>` | Iterate over keys |
89
+ | `entries()` | `IterableIterator<[TKey, TValue]>` | Iterate over [key, value] pairs |
90
+ | `dispose()` | `void` | Stop GC timer and clear all data |
91
+ | `[Symbol.dispose]()` | `void` | Supports `using` statement |
92
+
93
+ ---
107
94
 
108
95
  ## `DateTime`
109
96
 
110
- Immutable date-time class wrapping JavaScript `Date`. Millisecond precision, local timezone.
97
+ Immutable date-time class wrapping JavaScript `Date`. Provides millisecond precision and local timezone operation.
111
98
 
112
99
  ```typescript
113
100
  class DateTime {
@@ -122,14 +109,14 @@ class DateTime {
122
109
 
123
110
  // Getters
124
111
  get year(): number;
125
- get month(): number; // 1-12
112
+ get month(): number;
126
113
  get day(): number;
127
114
  get hour(): number;
128
115
  get minute(): number;
129
116
  get second(): number;
130
117
  get millisecond(): number;
131
118
  get tick(): number;
132
- get dayOfWeek(): number; // 0(Sun)-6(Sat)
119
+ get dayOfWeek(): number;
133
120
  get timezoneOffsetMinutes(): number;
134
121
  get isValid(): boolean;
135
122
 
@@ -153,32 +140,43 @@ class DateTime {
153
140
 
154
141
  // Formatting
155
142
  toFormatString(formatStr: string): string;
156
- toString(): string; // "yyyy-MM-ddTHH:mm:ss.fffzzz"
143
+ toString(): string;
157
144
  }
158
145
  ```
159
146
 
160
- ### `DateTime.parse` Supported Formats
147
+ ### Static Methods
148
+
149
+ | Method | Description |
150
+ |--------|-------------|
151
+ | `parse(str)` | Parse string to DateTime. Supported formats: `yyyy-MM-dd HH:mm:ss`, `yyyy-MM-dd HH:mm:ss.fff`, `yyyyMMddHHmmss`, `yyyy-MM-dd AM/PM HH:mm:ss`, ISO 8601. Throws `ArgumentError` on failure. |
152
+
153
+ ### Getters
161
154
 
162
- | Format | Example |
163
- |--------|---------|
164
- | `yyyy-MM-dd HH:mm:ss` | `"2025-01-15 10:30:00"` |
165
- | `yyyy-MM-dd HH:mm:ss.fff` | `"2025-01-15 10:30:00.123"` |
166
- | `yyyyMMddHHmmss` | `"20250115103000"` |
167
- | `yyyy-MM-dd AM/PM HH:mm:ss` | `"2025-01-15 AM 10:30:00"` |
168
- | ISO 8601 | `"2025-01-15T10:30:00Z"` |
169
- | Korean AM/PM | `"2025-01-15 오전 10:30:00"` |
155
+ | Property | Type | Description |
156
+ |----------|------|-------------|
157
+ | `year` | `number` | Full year |
158
+ | `month` | `number` | Month (1-12) |
159
+ | `day` | `number` | Day of month (1-31) |
160
+ | `hour` | `number` | Hour (0-23) |
161
+ | `minute` | `number` | Minute (0-59) |
162
+ | `second` | `number` | Second (0-59) |
163
+ | `millisecond` | `number` | Millisecond (0-999) |
164
+ | `tick` | `number` | Milliseconds since epoch |
165
+ | `dayOfWeek` | `number` | Day of week (0=Sunday, 6=Saturday) |
166
+ | `timezoneOffsetMinutes` | `number` | Local timezone offset in minutes (e.g., +540 for KST) |
167
+ | `isValid` | `boolean` | Whether the date is valid |
170
168
 
171
- ```typescript
172
- const now = new DateTime();
173
- const d = new DateTime(2025, 1, 15, 10, 30, 0);
174
- const parsed = DateTime.parse("2025-01-15 10:30:00");
175
- const next = d.addDays(7).setHour(14);
176
- d.toFormatString("yyyy-MM-dd HH:mm"); // "2025-01-15 10:30"
177
- ```
169
+ ### Formatting
170
+
171
+ `toFormatString(formatStr)` uses C#-style format patterns. See `dt.format` in [utilities.md](./utilities.md) for the full pattern table.
172
+
173
+ `toString()` returns `"yyyy-MM-ddTHH:mm:ss.fffzzz"`.
174
+
175
+ ---
178
176
 
179
177
  ## `DateOnly`
180
178
 
181
- Immutable date class (no time component). Local timezone.
179
+ Immutable date-only class (no time component). Local timezone based.
182
180
 
183
181
  ```typescript
184
182
  class DateOnly {
@@ -198,10 +196,10 @@ class DateOnly {
198
196
 
199
197
  // Getters
200
198
  get year(): number;
201
- get month(): number; // 1-12
199
+ get month(): number;
202
200
  get day(): number;
203
201
  get tick(): number;
204
- get dayOfWeek(): number; // 0(Sun)-6(Sat)
202
+ get dayOfWeek(): number;
205
203
  get isValid(): boolean;
206
204
 
207
205
  // Immutable setters
@@ -214,7 +212,7 @@ class DateOnly {
214
212
  addMonths(months: number): DateOnly;
215
213
  addDays(days: number): DateOnly;
216
214
 
217
- // Week calculations
215
+ // Week sequence
218
216
  getBaseYearMonthSeqForWeekSeq(weekStartDay?: number, minDaysInFirstWeek?: number): { year: number; monthSeq: number };
219
217
  getWeekSeqStartDate(weekStartDay?: number, minDaysInFirstWeek?: number): DateOnly;
220
218
  getWeekSeqOfYear(weekStartDay?: number, minDaysInFirstWeek?: number): { year: number; weekSeq: number };
@@ -222,37 +220,37 @@ class DateOnly {
222
220
 
223
221
  // Formatting
224
222
  toFormatString(formatStr: string): string;
225
- toString(): string; // "yyyy-MM-dd"
223
+ toString(): string;
226
224
  }
227
225
  ```
228
226
 
229
- ### `DateOnly.parse` Supported Formats
227
+ ### Static Methods
230
228
 
231
- | Format | Example |
232
- |--------|---------|
233
- | `yyyy-MM-dd` | `"2025-01-15"` (timezone-safe) |
234
- | `yyyyMMdd` | `"20250115"` (timezone-safe) |
235
- | ISO 8601 | `"2025-01-15T00:00:00Z"` (converted to local) |
229
+ | Method | Description |
230
+ |--------|-------------|
231
+ | `parse(str)` | Parse string to DateOnly. Supported formats: `yyyy-MM-dd`, `yyyyMMdd`, ISO 8601. Throws `ArgumentError` on failure. |
232
+ | `getDateByYearWeekSeq(arg, weekStartDay?, minDaysInFirstWeek?)` | Get the start date of a given week number within a year or month. |
236
233
 
237
- ### Week Calculation Parameters
234
+ ### Week Sequence Methods
238
235
 
239
- | Parameter | Default | Description |
240
- |-----------|---------|-------------|
241
- | `weekStartDay` | `1` (Monday) | Week start day: 0=Sun, 1=Mon, ..., 6=Sat |
242
- | `minDaysInFirstWeek` | `4` (ISO 8601) | Minimum days in the first week |
236
+ All week methods accept optional parameters:
237
+ - `weekStartDay`: 0=Sunday, 1=Monday (default), ..., 6=Saturday
238
+ - `minDaysInFirstWeek`: Minimum days for first week (default: 4, ISO 8601)
243
239
 
244
- ```typescript
245
- const today = new DateOnly();
246
- const d = new DateOnly(2025, 1, 15);
247
- const parsed = DateOnly.parse("2025-01-15");
248
- d.getWeekSeqOfYear(); // { year: 2025, weekSeq: 3 }
249
- d.getWeekSeqOfMonth(); // { year: 2025, monthSeq: 1, weekSeq: 3 }
250
- DateOnly.getDateByYearWeekSeq({ year: 2025, weekSeq: 2 }); // 2025-01-06
251
- ```
240
+ | Method | Returns | Description |
241
+ |--------|---------|-------------|
242
+ | `getBaseYearMonthSeqForWeekSeq(...)` | `{ year, monthSeq }` | Base year and month for this date's week |
243
+ | `getWeekSeqStartDate(...)` | `DateOnly` | Start date of this date's week |
244
+ | `getWeekSeqOfYear(...)` | `{ year, weekSeq }` | Year and week number within that year |
245
+ | `getWeekSeqOfMonth(...)` | `{ year, monthSeq, weekSeq }` | Year, month, and week number within that month |
246
+
247
+ `toString()` returns `"yyyy-MM-dd"`.
248
+
249
+ ---
252
250
 
253
251
  ## `Time`
254
252
 
255
- Immutable time class (no date component). Values automatically wrap within 24 hours.
253
+ Immutable time-only class (no date component). Values wrap around 24 hours. Negative values are normalized.
256
254
 
257
255
  ```typescript
258
256
  class Time {
@@ -277,7 +275,7 @@ class Time {
277
275
  setSecond(second: number): Time;
278
276
  setMillisecond(millisecond: number): Time;
279
277
 
280
- // Arithmetic (wraps at 24h)
278
+ // Arithmetic (24-hour wrapping)
281
279
  addHours(hours: number): Time;
282
280
  addMinutes(minutes: number): Time;
283
281
  addSeconds(seconds: number): Time;
@@ -285,23 +283,25 @@ class Time {
285
283
 
286
284
  // Formatting
287
285
  toFormatString(formatStr: string): string;
288
- toString(): string; // "HH:mm:ss.fff"
286
+ toString(): string;
289
287
  }
290
288
  ```
291
289
 
292
- ### `Time.parse` Supported Formats
290
+ ### Static Methods
293
291
 
294
- | Format | Example |
295
- |--------|---------|
296
- | `HH:mm:ss` | `"10:30:00"` |
297
- | `HH:mm:ss.fff` | `"10:30:00.123"` |
298
- | `AM/PM HH:mm:ss` | `"AM 10:30:00"` |
299
- | ISO 8601 (time part) | `"2025-01-15T10:30:00Z"` |
292
+ | Method | Description |
293
+ |--------|-------------|
294
+ | `parse(str)` | Parse string to Time. Supported formats: `HH:mm:ss`, `HH:mm:ss.fff`, `AM/PM HH:mm:ss`, ISO 8601 (time part extracted). Throws `ArgumentError` on failure. |
300
295
 
301
- ```typescript
302
- const now = new Time();
303
- const t = new Time(14, 30, 0);
304
- const parsed = Time.parse("10:30:00");
305
- t.addHours(12); // wraps: 02:30:00
306
- t.toFormatString("tt h:mm:ss"); // "PM 2:30:00"
307
- ```
296
+ ### Getters
297
+
298
+ | Property | Type | Description |
299
+ |----------|------|-------------|
300
+ | `hour` | `number` | Hour (0-23) |
301
+ | `minute` | `number` | Minute (0-59) |
302
+ | `second` | `number` | Second (0-59) |
303
+ | `millisecond` | `number` | Millisecond (0-999) |
304
+ | `tick` | `number` | Total milliseconds from midnight (0 to 86399999) |
305
+ | `isValid` | `boolean` | Whether the time is valid |
306
+
307
+ `toString()` returns `"HH:mm:ss.fff"`.