You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,6 +120,7 @@ xx.xx.xxxx
120
120
***Feature** - Added a bytes module — [`toBase64`](https://next.semantic-ui.com/docs/api/utils/bytes#tobase64) and [`fromBase64`](https://next.semantic-ui.com/docs/api/utils/bytes#frombase64) — unicode-safe base64 encode/decode, the pair `btoa`/`atob` never were. A string round-trips through its UTF-8 bytes (emoji and accents survive), `toBase64` also takes an `ArrayBuffer`, typed array, or array of byte values and a `{ urlSafe }` option, and `fromBase64` decodes both alphabets, strips MIME/PEM line wrapping, and returns a string or `Uint8Array` — or `null` on malformed input, never a throw
121
121
* **Feature** - Added [`toByteSize`](https://next.semantic-ui.com/docs/api/utils/coercion#tobytesize) and [`toBytes`](https://next.semantic-ui.com/docs/api/utils/coercion#tobytes) to the coercion suite, with `coerceByteSize`/`coerceBytes` aliases. `toByteSize` reads a size expression as a whole number of bytes, so `'10mb'`, `'1.5 KB'`, and `'2 gigabytes'` all become the number an upload limit or quota compares against. Same grammar as `toDuration` (one number, one optional unit, case-insensitive, space optional), abbreviations only like the ecosystem's `bytes()` where `toDuration` reads words like `ms()`, since every importer pays per spelling and a word is one `toByteSize.config.units` line. `b`, `kb` through `pb` scale by 1024, what every config that accepts `'10mb'` already means, or by 1000 per call with `{ base: 1000 }` or once at boot via `toByteSize.config.base`. The IEC spellings `kib` through `pib` are 1024 by definition and never move. Bits, compound forms, and durations return `null`. `toBytes` is the `Uint8Array` coercion: a string reads as its UTF-8 text (never as an encoding, that is `fromBase64`'s job), a buffer, typed array, or `DataView` becomes a view over the same memory with no copy, an array of byte values is copied, and a bare number or an array holding a non-byte returns `null` rather than reading as a length or wrapping
122
122
***Feature** - Added [`byteLength`](https://next.semantic-ui.com/docs/api/utils/bytes#bytelength) and [`formatByteSize`](https://next.semantic-ui.com/docs/api/utils/bytes#formatbytesize) to the bytes module. `byteLength` counts the bytes a value holds (a string by its UTF-8 bytes, not its characters), `null` when it holds none. `formatByteSize` prints a count in the largest unit it fills (`'1.5 KB'`, `'10 MB'`), accepts anything `toByteSize` reads at the same base so `'10mb'` prints back as `'10 MB'`, and takes `decimals` (a maximum, trailing zeros drop), `unit` to hold one unit down a column, `iec` for `KiB`/`MiB` labels, `base: 1000` for SI, and `locale`. Labels and defaults are editable at boot via `formatByteSize.config`
123
+
***Feature** - Added [`formatDuration`](https://next.semantic-ui.com/docs/api/utils/dates#formatduration), the inverse of `toDuration`. Prints milliseconds in the largest unit filled (`300000` as `'5m'`, `90000` as `'1.5m'`), reads anything `toDuration` reads, and every string it prints reads back through `toDuration`, so a config can take `'5m'` on the way in and show `'5m'` on the way out. `decimals` is a maximum, `unit` holds one unit down a column, `separator` goes between the number and the unit (`'1.5 minutes'`), and `lossless` picks the largest unit that reads back to exactly the same value (`100000` is `'100s'`, not `'1.7m'`), the print a config or debug view wants. The unit ladder is editable at boot via `formatDuration.config.units`, spelled in `toDuration`'s vocabulary
123
124
***Feature** - Added [`configured`](https://next.semantic-ui.com/docs/api/utils/functions#configured) — attaches an editable `fn.config` to a function as one tree-shakable expression. All configured utilities (`humanize`, `toTitleCase`, `getArticle`, `toBoolean`, `formatDate`) use it, so a bundle only pays for the vocabularies it imports — the attribute-codec path that pulls in string casing no longer carries any of them
124
125
***Feature** - Arbitrary vocabularies now live in editable configs, set once at app boot: [`toTitleCase.config.stopWords`](https://next.semantic-ui.com/docs/api/utils/strings#totitlecase) (style guides disagree on the list, `humanize`'s `titleCase` reads the same one), [`getArticle.config.exceptions`](https://next.semantic-ui.com/docs/api/utils/strings#getarticle) for sound-contradicts-spelling words (`an hour`, `a university` now correct out of the box), and [`formatDate.config.timezones`](https://next.semantic-ui.com/docs/api/utils/dates#formatdate) for remapping an ambiguous shorthand like `IST`. `toDate` reads unix-second timestamps (a JWT `exp`) via `{ epoch: 'seconds' }`
125
126
***Feature** - Added `unescapeHTML()` for converting HTML entities back to characters — the inverse of `escapeHTML`
Copy file name to clipboardExpand all lines: ai/skills/authoring/utility-functions.md
+26-1Lines changed: 26 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Utility Functions Reference
3
3
description: Complete reference for @semantic-ui/utils — a standalone utility library providing functions for arrays, objects, strings, type checking, colors, dates, and more. Use this before reimplementing common operations.
tip: 'Every string it prints reads back through toDuration. The default rounds, so 100000 is 1.7m. Pass lossless to pick the largest unit that reads back exactly (100s), the print a config view wants'
Copy file name to clipboardExpand all lines: docs/src/pages/docs/api/utils/coercion.mdx
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,6 +202,8 @@ Coerces a duration expression to milliseconds, or `null` when it reads as no dur
202
202
203
203
The sign is kept, so `'-1.5h'` is `-5400000`. Compound expressions like `'1h 30m'` are a different grammar and return `null` rather than a partial reading, as do unknown units, bare numbers with trailing junk, and exponent notation.
204
204
205
+
[`formatDuration`](/docs/api/utils/dates#formatduration) is the inverse, printing milliseconds back in this grammar (`300000` as `'5m'`), so a config can read `'5m'` on the way in and show `'5m'` on the way out.
Copy file name to clipboardExpand all lines: docs/src/pages/docs/api/utils/dates.mdx
+75-3Lines changed: 75 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,12 @@ layout: '@layouts/Guide.astro'
3
3
pageType: 'API Reference'
4
4
title: Date Utilities
5
5
package: "@semantic-ui/utils"
6
-
methods: [formatDate]
6
+
methods: [formatDate, formatDuration]
7
7
icon: calendar
8
-
description: API reference for date manipulation and formatting functions
8
+
description: API reference for date and duration formatting functions
9
9
---
10
10
11
-
The Date utilities provide functions for formatting and manipulating dates in JavaScript.
11
+
The Date utilities format dates and durations for display. `formatDate` prints a `Date` through a token string, `formatDuration` prints a millisecond count in the largest unit it fills, the inverse of [`toDuration`](/docs/api/utils/coercion#toduration).
console.log(formatDate(date, '[Today is] dddd, MMMM Do, YYYY')); // "Today is Monday, May 15th, 2023"
150
150
```
151
+
152
+
### formatDuration
153
+
154
+
```javascript
155
+
functionformatDuration(value, { decimals, unit, lossless, separator } = {})
156
+
```
157
+
158
+
Formats a duration for display in the largest unit it fills, or `null` when there is no duration to format. `300000` prints as `'5m'`, `90000` as `'1.5m'`, `500` as `'500ms'`, and the sign is kept. Accepts anything `toDuration` reads, so `'90s'` prints as `'1.5m'` with no conversion in between.
159
+
160
+
This is the inverse of `toDuration` and the two share one grammar: every string `formatDuration` prints reads back through `toDuration`, and the unit ladder is spelled in `toDuration`'s vocabulary. It prints one quantity, never a compound `'1h 30m'`, because `toDuration` reads one quantity.
161
+
162
+
`decimals` is a maximum, so `'5m'` never prints as `'5.0m'`, and a value that rounds up to a whole unit promotes (`3598200` is `'1h'` at one decimal, not `'60m'`). `unit` holds one unit so a column reads down in the same scale, printed as spelled. `separator` goes between the number and the unit, so `{ unit:'minutes', separator:'' }` gives `'1.5 minutes'`. A space is the one separator `toDuration` reads back, since its grammar allows one there and nothing else.
163
+
164
+
`lossless` is for a config view. The default picks the largest unit the value fills and rounds, so `100000` prints as `'1.7m'`, which reads back as `102000`. Under `lossless` the walk continues to the largest unit whose print reads back to exactly the same value, `'100s'`, where the check is the very product `toDuration` computes. A value with no short exact form prints in a small unit (`93784000` is `'93784s'`), which is the honest answer for a value nobody configured by hand.
165
+
166
+
#### Parameters
167
+
168
+
| Name | Type | Description |
169
+
|----------|---------|-------------|
170
+
| value | unknown | The milliseconds, or a duration expression |
171
+
| settings | object | Optional configuration |
172
+
173
+
##### Options
174
+
175
+
| Name | Type | Default | Description |
176
+
|----------|---------|---------|-------------|
177
+
| decimals | number | `formatDuration.config.decimals` (1) | Maximum decimal places, trailing zeros drop |
178
+
| unit | string | largest unit filled | Hold one unit, printed as spelled, any spelling `toDuration` reads (`'s'`, `'minutes'`, `'hr'`) |
179
+
| lossless | boolean | `formatDuration.config.lossless` (false) | Pick the largest unit that reads back through `toDuration` to the same value |
180
+
| separator | string | `formatDuration.config.separator` (`''`) | Text between the number and the unit, `''` for `'1.5 minutes'` |
181
+
182
+
#### Returns
183
+
184
+
The formatted duration, or `null` if there is no duration to format.
`formatDuration.config` holds the defaults for `decimals`, `lossless`, and `separator`, plus `units`, the ladder walked largest first. Each entry is a spelling `toDuration.config.units` holds, which is where the span comes from, so a unit is added to both. Edit it once at app boot and every call inherits it. Per-call settings still win.
0 commit comments