@ox0/utils 0.1.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.
Files changed (2) hide show
  1. package/dist/index.js +29 -0
  2. package/package.json +23 -0
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ function invariant(condition, message = "Invariant violation") {
2
+ if (!condition) throw new Error(message);
3
+ }
4
+ function clamp(value, min, max) {
5
+ return Math.min(Math.max(value, min), max);
6
+ }
7
+ function groupBy(items, getKey) {
8
+ return items.reduce((accumulator, item)=>{
9
+ const key = getKey(item);
10
+ const bucket = accumulator[key] ?? [];
11
+ bucket.push(item);
12
+ accumulator[key] = bucket;
13
+ return accumulator;
14
+ }, {});
15
+ }
16
+ function formatBytes(bytes) {
17
+ if (0 === bytes) return "0 B";
18
+ const units = [
19
+ "B",
20
+ "KB",
21
+ "MB",
22
+ "GB",
23
+ "TB"
24
+ ];
25
+ const index = Math.min(Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024)), units.length - 1);
26
+ const value = bytes / 1024 ** index;
27
+ return `${value.toFixed(value >= 10 || 0 === index ? 0 : 1)} ${units[index]}`;
28
+ }
29
+ export { clamp, formatBytes, groupBy, invariant };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@ox0/utils",
3
+ "version": "0.1.0",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "scripts": {
18
+ "build": "rslib build -c ../../rslib.config.ts",
19
+ "typecheck": "tsc -p tsconfig.json",
20
+ "lint": "oxlint .",
21
+ "test": "vitest run"
22
+ }
23
+ }