@oslokommune/punkt-react 15.0.4 → 15.2.0

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.
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Utility functions for the FileUpload component
3
+ */
4
+
5
+ /**
6
+ * Parse a file size string (e.g. "5MB", "500KB") to bytes.
7
+ * Also accepts raw numbers (returned as-is).
8
+ *
9
+ * @example
10
+ * parseFileSize("5MB") // 5242880
11
+ * parseFileSize("500KB") // 512000
12
+ * parseFileSize("1GB") // 1073741824
13
+ * parseFileSize(1024) // 1024
14
+ */
15
+ export const parseFileSize = (size: string | number): number => {
16
+ if (typeof size === 'number') return size
17
+
18
+ const match = size.trim().match(/^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB)$/i)
19
+ if (!match) {
20
+ console.warn(
21
+ `PktFileUpload: Invalid maxFileSize format "${size}". Use format like "5MB", "500KB", or a number in bytes.`,
22
+ )
23
+ return 0
24
+ }
25
+
26
+ const value = parseFloat(match[1])
27
+ const unit = match[2].toUpperCase()
28
+
29
+ switch (unit) {
30
+ case 'B':
31
+ return value
32
+ case 'KB':
33
+ return value * 1024
34
+ case 'MB':
35
+ return value * 1024 * 1024
36
+ case 'GB':
37
+ return value * 1024 * 1024 * 1024
38
+ default:
39
+ return value
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Format bytes to a human-readable string (e.g. "5 MB", "500 KB").
45
+ *
46
+ * @example
47
+ * formatFileSize(5242880) // "5 MB"
48
+ * formatFileSize(512000) // "500 KB"
49
+ * formatFileSize(500) // "500 B"
50
+ */
51
+ export const formatFileSize = (bytes: number): string => {
52
+ if (bytes < 1024) return `${bytes} B`
53
+ if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`
54
+ if (bytes < 1024 * 1024 * 1024) return `${Math.round(bytes / (1024 * 1024))} MB`
55
+ return `${Math.round(bytes / (1024 * 1024 * 1024))} GB`
56
+ }
@@ -10,6 +10,7 @@ export { PktCheckbox } from './checkbox/Checkbox'
10
10
  export { PktCombobox } from './combobox/Combobox'
11
11
  export { PktConsent } from './consent/Consent'
12
12
  export { PktDatepicker } from './datepicker/Datepicker'
13
+ export { PktFileUpload } from './fileupload/FileUpload'
13
14
  export { PktFooter } from './footer/Footer'
14
15
  export { PktFooterSimple } from './footerSimple/FooterSimple'
15
16
  export { PktHeader } from './header/Header'
@@ -8,6 +8,7 @@ export type { IPktCard } from './card/Card'
8
8
  export type { IPktCheckbox } from './checkbox/Checkbox'
9
9
  export type { IPktCombobox } from './combobox/Combobox'
10
10
  export type { IPktDatepicker } from './datepicker/Datepicker'
11
+ export type { IPktFileUpload } from './fileupload/FileUpload'
11
12
  export type { IPktFooter } from './footer/Footer'
12
13
  export type { IPktFooterSimple } from './footerSimple/FooterSimple'
13
14
  export type { IPktHeader } from './header/Header'