@sveltebase/utils 1.0.2 → 1.0.4
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 +33 -2
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,14 +18,13 @@ bun add svelte-sonner
|
|
|
18
18
|
|
|
19
19
|
> `svelte` is a peer dependency and should already exist in your app.
|
|
20
20
|
|
|
21
|
-
## What’s included
|
|
22
|
-
|
|
23
21
|
- `Cookies` — browser cookie helpers
|
|
24
22
|
- `createAsync` — wraps async functions with loading and error state
|
|
25
23
|
- `tryCatch` — run a task with built-in toast/error handling
|
|
26
24
|
- `timestamps` — generate `createdAt` / `updatedAt` values
|
|
27
25
|
- `wait` — simple promise-based delay helper
|
|
28
26
|
- `createId` — generate random UUIDs with fallback support
|
|
27
|
+
- `pluralize` — format counts using custom zero, one, and other rules
|
|
29
28
|
- `TryCatchReturn` — shared return type for async helpers
|
|
30
29
|
|
|
31
30
|
## Exports
|
|
@@ -35,6 +34,7 @@ import {
|
|
|
35
34
|
Cookies,
|
|
36
35
|
createAsync,
|
|
37
36
|
createId,
|
|
37
|
+
pluralize,
|
|
38
38
|
timestamps,
|
|
39
39
|
tryCatch,
|
|
40
40
|
wait,
|
|
@@ -283,6 +283,37 @@ Useful for:
|
|
|
283
283
|
|
|
284
284
|
---
|
|
285
285
|
|
|
286
|
+
## `pluralize`
|
|
287
|
+
|
|
288
|
+
A helper for formatting counts into readable strings using custom rules for zero, one, and multiple items.
|
|
289
|
+
|
|
290
|
+
### Example
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
import { pluralize } from "@sveltebase/utils";
|
|
294
|
+
|
|
295
|
+
// 1. Basic usage
|
|
296
|
+
pluralize(0, { other: "items" }); // "0 items"
|
|
297
|
+
pluralize(1, { one: "item", other: "items" }); // "1 item"
|
|
298
|
+
pluralize(5, { one: "item", other: "items" }); // "5 items"
|
|
299
|
+
|
|
300
|
+
// 2. Custom zero text
|
|
301
|
+
pluralize(0, { zero: "No items", one: "item", other: "items" }); // "No items"
|
|
302
|
+
|
|
303
|
+
// 3. Callback formatting for complex pluralizations
|
|
304
|
+
pluralize(3, {
|
|
305
|
+
other: (count) => `${count} matches found`
|
|
306
|
+
}); // "3 matches found"
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Useful for:
|
|
310
|
+
|
|
311
|
+
- localized text formatting
|
|
312
|
+
- user-facing list item counts (e.g., "1 item", "No items", "4 items")
|
|
313
|
+
- dynamic messaging depending on numeric results
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
286
317
|
## `TryCatchReturn`
|
|
287
318
|
|
|
288
319
|
A shared type for functions used with `createAsync` and `tryCatch`.
|
package/dist/index.d.ts
CHANGED
|
@@ -23,4 +23,9 @@ export declare function tryCatch(task: () => Promise<TryCatchReturn> | TryCatchR
|
|
|
23
23
|
export declare const wait: (ms: number) => Promise<unknown>;
|
|
24
24
|
export { createAsync } from "./async.svelte.js";
|
|
25
25
|
export declare function createId(): string;
|
|
26
|
+
export declare function pluralize(count: number, { zero, one, other }: {
|
|
27
|
+
zero?: string;
|
|
28
|
+
one?: string;
|
|
29
|
+
other: string | ((count: number) => string);
|
|
30
|
+
}): string;
|
|
26
31
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,WAAW,CAA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G,MAAM,GAAG,IAAI;iBAYnB,MAAM,YAAW,IAAI,CAAC,aAAa,EAAE,MAAM,GAAG,QAAQ,CAAC,GAAQ,IAAI;CAGjF,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,SAAS,OAAO,EAC1C,UAAU,EAAE,CAAC,GACZ,CAAC,SAAS,IAAI,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAInF;AAED,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,iBAkBlF;AAED,eAAO,MAAM,IAAI,GAAI,IAAI,MAAM,qBAAsD,CAAC;AAEtF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,wBAAgB,QAAQ,IAAI,MAAM,CAgBjC;AAED,wBAAgB,SAAS,CACvB,KAAK,EAAE,MAAM,EACb,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAA;CAAE,UAOnG"}
|
package/dist/index.js
CHANGED
|
@@ -109,3 +109,12 @@ export function createId() {
|
|
|
109
109
|
return v.toString(16);
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
|
+
export function pluralize(count, { zero, one, other }) {
|
|
113
|
+
if (count === 0 && zero)
|
|
114
|
+
return zero;
|
|
115
|
+
if (count === 1 && one)
|
|
116
|
+
return `1 ${one}`;
|
|
117
|
+
if (typeof other === "function")
|
|
118
|
+
return other(count);
|
|
119
|
+
return `${count} ${other}`;
|
|
120
|
+
}
|