ui-formatter 0.1.1 โ†’ 0.1.2

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/README.md CHANGED
@@ -1 +1,446 @@
1
- # ui-formatter
1
+ # ui-formatter
2
+
3
+ A lightweight, zero-dependency TypeScript utility library for formatting dates, times, numbers, and phone numbers.
4
+
5
+ Designed for applications that require **high performance**, **small bundle size**, and **no external dependencies**.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - ๐Ÿš€ Zero dependencies
12
+ - โšก High-performance implementations
13
+ - ๐Ÿ“… Date formatting
14
+ - ๐Ÿ•’ Time formatting
15
+ - ๐Ÿ”ข Number formatting
16
+ - โ˜Ž๏ธ Phone/Fax formatting
17
+ - ๐Ÿงน Phone normalization
18
+ - ๐Ÿ’ฏ TypeScript support
19
+ - ๐ŸŒ Works in Browser and Node.js
20
+
21
+ ---
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install ui-formatter
27
+ ```
28
+
29
+ or
30
+
31
+ ```bash
32
+ yarn add ui-formatter
33
+ ```
34
+
35
+ ---
36
+
37
+ ## Quick Example
38
+
39
+ ```ts
40
+ import {
41
+ formatDate,
42
+ formatDateTime,
43
+ formatNumber,
44
+ formatInteger,
45
+ formatPhone
46
+ } from "ui-formatter"
47
+
48
+ const date = new Date()
49
+
50
+ formatDate(date, "MM/dd/yyyy")
51
+ // 07/02/2026
52
+
53
+ formatDateTime(date, "yyyy-MM-dd")
54
+ // 2026-07-02 14:35
55
+
56
+ formatNumber(1234567.89, 2)
57
+ // 1,234,567.89
58
+
59
+ formatInteger(1000000)
60
+ // 1,000,000
61
+
62
+ formatPhone("8005551234")
63
+ // 800 555-1234
64
+ ```
65
+
66
+ ---
67
+
68
+ # API
69
+
70
+ ---
71
+
72
+ ## Date Utilities
73
+
74
+ ### getDateFormat()
75
+
76
+ Returns the default date format.
77
+
78
+ ```ts
79
+ const format = getDateFormat()
80
+
81
+ // M/d/yyyy
82
+ ```
83
+
84
+ ---
85
+
86
+ ### addDays()
87
+
88
+ Returns a new date with the specified number of days added.
89
+
90
+ ```ts
91
+ const tomorrow = addDays(new Date(), 1)
92
+ ```
93
+
94
+ ---
95
+
96
+ ### formatDate()
97
+
98
+ Formats a Date using a custom format.
99
+
100
+ Supported tokens:
101
+
102
+ | Token | Description |
103
+ |--------|-------------|
104
+ | yyyy | 4-digit year |
105
+ | yy | 2-digit year |
106
+ | MM | 2-digit month |
107
+ | M | Month |
108
+ | dd | 2-digit day |
109
+ | d | Day |
110
+
111
+ Example
112
+
113
+ ```ts
114
+ formatDate(new Date(), "yyyy-MM-dd")
115
+
116
+ // 2026-07-02
117
+ ```
118
+
119
+ ---
120
+
121
+ ### formatTime()
122
+
123
+ Returns
124
+
125
+ ```
126
+ HH:mm
127
+ ```
128
+
129
+ Example
130
+
131
+ ```ts
132
+ formatTime(new Date())
133
+
134
+ // 15:30
135
+ ```
136
+
137
+ ---
138
+
139
+ ### formatLongTime()
140
+
141
+ Returns
142
+
143
+ ```
144
+ HH:mm:ss
145
+ ```
146
+
147
+ Example
148
+
149
+ ```ts
150
+ formatLongTime(new Date())
151
+
152
+ // 15:30:45
153
+ ```
154
+
155
+ ---
156
+
157
+ ### formatFullTime()
158
+
159
+ Returns
160
+
161
+ ```
162
+ HH:mm:ss.SSS
163
+ ```
164
+
165
+ Example
166
+
167
+ ```ts
168
+ formatFullTime(new Date())
169
+
170
+ // 15:30:45.123
171
+ ```
172
+
173
+ ---
174
+
175
+ ### formatDateTime()
176
+
177
+ Formats
178
+
179
+ ```
180
+ <Date> <HH:mm>
181
+ ```
182
+
183
+ Example
184
+
185
+ ```ts
186
+ formatDateTime(new Date(), "yyyy-MM-dd")
187
+
188
+ // 2026-07-02 15:30
189
+ ```
190
+
191
+ ---
192
+
193
+ ### formatLongDateTime()
194
+
195
+ Formats
196
+
197
+ ```
198
+ <Date> <HH:mm:ss>
199
+ ```
200
+
201
+ ---
202
+
203
+ ### formatFullDateTime()
204
+
205
+ Formats
206
+
207
+ ```
208
+ <Date> <HH:mm:ss.SSS>
209
+ ```
210
+
211
+ ---
212
+
213
+ ### datetimeToString()
214
+
215
+ Converts a Date into ISO-like format.
216
+
217
+ ```ts
218
+ datetimeToString(new Date())
219
+
220
+ // 2026-07-02T15:30:45
221
+ ```
222
+
223
+ ---
224
+
225
+ ### dateToString()
226
+
227
+ Returns a compact timestamp.
228
+
229
+ ```ts
230
+ dateToString(new Date())
231
+
232
+ // 20260702153045
233
+ ```
234
+
235
+ Milliseconds may be included.
236
+
237
+ ```ts
238
+ dateToString(new Date(), true)
239
+
240
+ // 20260702153045123
241
+ ```
242
+
243
+ ---
244
+
245
+ # Number Utilities
246
+
247
+ ---
248
+
249
+ ### formatInteger()
250
+
251
+ Formats an integer with thousands separators.
252
+
253
+ ```ts
254
+ formatInteger(123456789)
255
+
256
+ // 123,456,789
257
+ ```
258
+
259
+ Custom separator
260
+
261
+ ```ts
262
+ formatInteger(123456789, ".")
263
+
264
+ // 123.456.789
265
+ ```
266
+
267
+ ---
268
+
269
+ ### formatNumber()
270
+
271
+ Formats decimal numbers.
272
+
273
+ ```ts
274
+ formatNumber(1234567.89, 2)
275
+
276
+ // 1,234,567.89
277
+ ```
278
+
279
+ Custom separators
280
+
281
+ ```ts
282
+ formatNumber(
283
+ 1234567.89,
284
+ 2,
285
+ ",",
286
+ "."
287
+ )
288
+
289
+ // 1.234.567,89
290
+ ```
291
+
292
+ ---
293
+
294
+ # Phone Utilities
295
+
296
+ ---
297
+
298
+ ### normalizePhone()
299
+
300
+ Removes all characters except digits and `+`.
301
+
302
+ ```ts
303
+ normalizePhone("(800) 555-1234")
304
+
305
+ // 8005551234
306
+ ```
307
+
308
+ ---
309
+
310
+ ### normalizeFax()
311
+
312
+ Alias of
313
+
314
+ ```
315
+ normalizePhone()
316
+ ```
317
+
318
+ ---
319
+
320
+ ### formatPhone()
321
+
322
+ Formats phone numbers into a readable representation.
323
+
324
+ ```ts
325
+ formatPhone("8005551234")
326
+
327
+ // 800 555-1234
328
+ ```
329
+
330
+ Supports
331
+
332
+ - US numbers
333
+ - international numbers
334
+ - partial numbers
335
+
336
+ ---
337
+
338
+ ### formatFax()
339
+
340
+ Formats fax numbers.
341
+
342
+ ```ts
343
+ formatFax("0212345678")
344
+
345
+ // 02-12345678
346
+ ```
347
+
348
+ ---
349
+
350
+ # Utility Functions
351
+
352
+ ---
353
+
354
+ ### isNotEmpty()
355
+
356
+ Checks whether an array or string is not empty.
357
+
358
+ ```ts
359
+ isNotEmpty([])
360
+
361
+ // false
362
+
363
+ isNotEmpty("hello")
364
+
365
+ // true
366
+ ```
367
+
368
+ ---
369
+
370
+ ### isChecked()
371
+
372
+ Utility for HTML templates.
373
+
374
+ ```ts
375
+ isChecked(true)
376
+
377
+ // checked
378
+
379
+ isChecked(false)
380
+
381
+ // ""
382
+ ```
383
+
384
+ Can also compare string values.
385
+
386
+ ```ts
387
+ isChecked("admin", "admin")
388
+
389
+ // checked
390
+ ```
391
+
392
+ ---
393
+
394
+ # Design Goals
395
+
396
+ This library is designed with the following principles:
397
+
398
+ - Zero runtime dependencies
399
+ - High performance
400
+ - Minimal allocations
401
+ - Small bundle size
402
+ - Predictable behavior
403
+ - Browser and Node compatibility
404
+
405
+ Several implementations avoid regular expressions and repeated string concatenation in favor of allocation-friendly algorithms.
406
+
407
+ ---
408
+
409
+ # Performance
410
+
411
+ The library includes optimized implementations for:
412
+
413
+ - Manual date parsing
414
+ - Number formatting
415
+ - Integer formatting
416
+ - Phone normalization
417
+
418
+ Many functions use:
419
+
420
+ - single-pass parsing
421
+ - preallocated buffers
422
+ - minimal temporary objects
423
+
424
+ making them suitable for high-throughput applications.
425
+
426
+ ---
427
+
428
+ # Browser Support
429
+
430
+ Works in:
431
+
432
+ - Chrome
433
+ - Firefox
434
+ - Safari
435
+ - Edge
436
+ - Node.js
437
+ - Bun
438
+ - Deno
439
+
440
+ ---
441
+
442
+ # TypeScript
443
+
444
+ Fully written in TypeScript.
445
+
446
+ No additional typings are required.
package/lib/index.js CHANGED
@@ -233,7 +233,7 @@ var formatter = (function () {
233
233
  return "";
234
234
  }
235
235
  var s = phone;
236
- var x = removePhoneFormat(phone);
236
+ var x = normalizePhone(phone);
237
237
  if (x.length === 10) {
238
238
  var USNumber = x.match(formatter.usPhone);
239
239
  if (USNumber != null) {
@@ -260,7 +260,7 @@ var formatter = (function () {
260
260
  return "";
261
261
  }
262
262
  var s = fax;
263
- var x = removePhoneFormat(fax);
263
+ var x = normalizeFax(fax);
264
264
  var l = x.length;
265
265
  if (l <= 6) {
266
266
  s = x;
@@ -285,20 +285,30 @@ var formatter = (function () {
285
285
  }
286
286
  return s;
287
287
  };
288
- formatter.fax = / |\-|\.|\(|\)/g;
289
- formatter.phone = / |\-|\.|\(|\)/g;
290
288
  formatter.usPhone = /(\d{3})(\d{3})(\d{4})/;
291
289
  return formatter;
292
290
  }());
293
291
  exports.formatter = formatter;
294
- function removePhoneFormat(phone) {
295
- return phone ? phone.replace(formatter.phone, "") : "";
292
+ function normalizePhone(s) {
293
+ if (!s) {
294
+ return "";
295
+ }
296
+ var len = s.length;
297
+ var buf = new Array(len);
298
+ var j = 0;
299
+ for (var i = 0; i < len; i++) {
300
+ var c = s.charCodeAt(i);
301
+ if ((c >= 48 && c <= 57) || c === 43) {
302
+ buf[j++] = s[i];
303
+ }
304
+ }
305
+ return j === len ? buf.join("") : buf.slice(0, j).join("");
296
306
  }
297
- exports.removePhoneFormat = removePhoneFormat;
298
- function removeFaxFormat(fax) {
299
- return fax ? fax.replace(formatter.fax, "") : "";
307
+ exports.normalizePhone = normalizePhone;
308
+ function normalizeFax(fax) {
309
+ return normalizePhone(fax);
300
310
  }
301
- exports.removeFaxFormat = removeFaxFormat;
311
+ exports.normalizeFax = normalizeFax;
302
312
  function formatPhone(phone) {
303
313
  return formatter.formatPhone(phone);
304
314
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ui-formatter",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Format web UI",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -253,8 +253,8 @@ export function formatNumber(v?: number | null, precision = 0, decimalSeparator?
253
253
  // tslint:disable-next-line:class-name
254
254
  export class formatter {
255
255
  // private static _preg = / |\+|\-|\.|\(|\)/g;
256
- static fax = / |\-|\.|\(|\)/g
257
- static phone = / |\-|\.|\(|\)/g
256
+ // static fax = / |\-|\.|\(|\)/g
257
+ // static phone = / |\-|\.|\(|\)/g
258
258
  static usPhone = /(\d{3})(\d{3})(\d{4})/
259
259
  static formatPhone(phone?: string | null): string {
260
260
  if (!phone) {
@@ -263,7 +263,7 @@ export class formatter {
263
263
  // reformat phone number
264
264
  // 555 123-4567 or (+1) 555 123-4567
265
265
  let s = phone
266
- const x = removePhoneFormat(phone)
266
+ const x = normalizePhone(phone)
267
267
  if (x.length === 10) {
268
268
  const USNumber = x.match(formatter.usPhone)
269
269
  if (USNumber != null) {
@@ -289,7 +289,7 @@ export class formatter {
289
289
  // reformat phone number
290
290
  // 035-456745 or 02-1234567
291
291
  let s = fax
292
- const x = removePhoneFormat(fax)
292
+ const x = normalizeFax(fax)
293
293
  const l = x.length
294
294
  if (l <= 6) {
295
295
  s = x
@@ -311,11 +311,23 @@ export class formatter {
311
311
  return s
312
312
  }
313
313
  }
314
- export function removePhoneFormat(phone?: string): string {
315
- return phone ? phone.replace(formatter.phone, "") : ""
314
+ export function normalizePhone(s?: string | null): string {
315
+ if (!s) {
316
+ return ""
317
+ }
318
+ const len = s.length
319
+ const buf = new Array<string>(len)
320
+ let j = 0
321
+ for (let i = 0; i < len; i++) {
322
+ const c = s.charCodeAt(i)
323
+ if ((c >= 48 && c <= 57) || c === 43) {
324
+ buf[j++] = s[i]
325
+ }
326
+ }
327
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
316
328
  }
317
- export function removeFaxFormat(fax?: string): string {
318
- return fax ? fax.replace(formatter.fax, "") : ""
329
+ export function normalizeFax(fax?: string): string {
330
+ return normalizePhone(fax)
319
331
  }
320
332
  export function formatPhone(phone?: string | null): string {
321
333
  return formatter.formatPhone(phone)