@locusgraph/utils 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +20 -2
- package/dist/dates.d.ts +1 -0
- package/dist/dates.js +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/money.d.ts +6 -0
- package/dist/money.js +26 -0
- package/dist/numbers.d.ts +2 -0
- package/dist/numbers.js +7 -0
- package/dist/quota.d.ts +8 -0
- package/dist/quota.js +13 -0
- package/package.json +36 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LocusGraph
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @locusgraph/utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Formatting and quota helpers LocusGraph apps share. No dependencies, no React.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @locusgraph/utils
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
| Function | Example |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `formatCents(4000)` | `$40.00` |
|
|
12
|
+
| `formatMicroUsd(1_234_500)` | `$1.2345` (sub-cent amounts keep four places) |
|
|
13
|
+
| `centsToMicroUsd(1000)` | `10000000` |
|
|
14
|
+
| `formatCount(10450)` | `10,450` |
|
|
15
|
+
| `pluralize(2, "key")` | `2 keys` |
|
|
16
|
+
| `formatDate("2026-11-15T12:00:00Z", "Never")` | `Nov 15, 2026` (UTC; the fallback when empty or invalid) |
|
|
17
|
+
| `quotaPercent({ used, included })` | 0–100, capped |
|
|
18
|
+
| `quotaStatus({ used, included })` | `"ok"`, `"near"` from 80%, `"over"` past the allowance |
|
|
19
|
+
| `balanceLevel(microUsd)` | `"empty"` at or below 0, `"low"` under $2, else `"fine"` |
|
|
20
|
+
|
|
21
|
+
Money follows the platform's rules: prices and Stripe amounts are integer cents; wallet balances and charges are integer micro-USD ($1 = 1,000,000).
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatDate(value: string | number | Date | null | undefined, empty?: string): string;
|
package/dist/dates.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const mediumDate = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "UTC" });
|
|
2
|
+
export function formatDate(value, empty = "") {
|
|
3
|
+
if (value === null || value === undefined) {
|
|
4
|
+
return empty;
|
|
5
|
+
}
|
|
6
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
7
|
+
return Number.isNaN(date.getTime()) ? empty : mediumDate.format(date);
|
|
8
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/money.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function formatCents(cents: number): string;
|
|
2
|
+
export declare function formatMicroUsd(microUsd: number): string;
|
|
3
|
+
export declare function centsToMicroUsd(cents: number): number;
|
|
4
|
+
export type BalanceLevel = "fine" | "low" | "empty";
|
|
5
|
+
export declare const lowBalanceMicroUsd = 2000000;
|
|
6
|
+
export declare function balanceLevel(balanceMicroUsd: number, lowAtMicroUsd?: number): BalanceLevel;
|
package/dist/money.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const microUsdPerDollar = 1_000_000;
|
|
2
|
+
const microUsdPerCent = 10_000;
|
|
3
|
+
const usd = (fractionDigits) => new Intl.NumberFormat("en-US", {
|
|
4
|
+
style: "currency",
|
|
5
|
+
currency: "USD",
|
|
6
|
+
minimumFractionDigits: fractionDigits.min,
|
|
7
|
+
maximumFractionDigits: fractionDigits.max,
|
|
8
|
+
});
|
|
9
|
+
const wholeCentsFormat = usd({ min: 2, max: 2 });
|
|
10
|
+
const subCentFormat = usd({ min: 2, max: 4 });
|
|
11
|
+
export function formatCents(cents) {
|
|
12
|
+
return wholeCentsFormat.format(cents / 100);
|
|
13
|
+
}
|
|
14
|
+
export function formatMicroUsd(microUsd) {
|
|
15
|
+
const format = microUsd % microUsdPerCent === 0 ? wholeCentsFormat : subCentFormat;
|
|
16
|
+
return format.format(microUsd / microUsdPerDollar);
|
|
17
|
+
}
|
|
18
|
+
export function centsToMicroUsd(cents) {
|
|
19
|
+
return cents * microUsdPerCent;
|
|
20
|
+
}
|
|
21
|
+
export const lowBalanceMicroUsd = 2_000_000;
|
|
22
|
+
export function balanceLevel(balanceMicroUsd, lowAtMicroUsd = lowBalanceMicroUsd) {
|
|
23
|
+
if (balanceMicroUsd <= 0)
|
|
24
|
+
return "empty";
|
|
25
|
+
return balanceMicroUsd < lowAtMicroUsd ? "low" : "fine";
|
|
26
|
+
}
|
package/dist/numbers.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const countFormat = new Intl.NumberFormat("en-US");
|
|
2
|
+
export function formatCount(value) {
|
|
3
|
+
return countFormat.format(value);
|
|
4
|
+
}
|
|
5
|
+
export function pluralize(count, singular, plural = `${singular}s`) {
|
|
6
|
+
return `${formatCount(count)} ${count === 1 ? singular : plural}`;
|
|
7
|
+
}
|
package/dist/quota.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type QuotaStatus = "ok" | "near" | "over";
|
|
2
|
+
export type QuotaAmounts = {
|
|
3
|
+
used: number;
|
|
4
|
+
included: number;
|
|
5
|
+
};
|
|
6
|
+
export declare const nearLimitPercent = 80;
|
|
7
|
+
export declare function quotaPercent({ used, included }: QuotaAmounts): number;
|
|
8
|
+
export declare function quotaStatus(quota: QuotaAmounts): QuotaStatus;
|
package/dist/quota.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const nearLimitPercent = 80;
|
|
2
|
+
export function quotaPercent({ used, included }) {
|
|
3
|
+
if (included <= 0) {
|
|
4
|
+
return used > 0 ? 100 : 0;
|
|
5
|
+
}
|
|
6
|
+
return Math.min(Math.round((used / included) * 100), 100);
|
|
7
|
+
}
|
|
8
|
+
export function quotaStatus(quota) {
|
|
9
|
+
if (quota.used > quota.included) {
|
|
10
|
+
return "over";
|
|
11
|
+
}
|
|
12
|
+
return quotaPercent(quota) >= nearLimitPercent ? "near" : "ok";
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@locusgraph/utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Formatting and quota helpers LocusGraph apps share: money in cents and micro-USD, counts, dates and usage status.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/locusgraph/design.git",
|
|
9
|
+
"directory": "packages/utils"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://design.locusgraph.com",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"registry": "https://registry.npmjs.org",
|
|
25
|
+
"access": "public",
|
|
26
|
+
"provenance": true
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@locusgraph/config": "0.1.0",
|
|
30
|
+
"@types/node": "^22.0.0",
|
|
31
|
+
"typescript": "^7.0.2",
|
|
32
|
+
"vitest": "^5.0.3"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && node ../../scripts/esm-extensions.mjs dist",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
37
|
+
"test": "vitest run"
|
|
38
|
+
}
|
|
6
39
|
}
|