@oviirup/utils 0.0.1-canary.2 → 0.0.1-canary.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/dist/clsx.d.ts +3 -4
- package/dist/number.d.ts +15 -7
- package/dist/number.js +1 -1
- package/dist/number.mjs +1 -1
- package/dist/object.d.ts +3 -2
- package/dist/promise.d.ts +11 -0
- package/dist/promise.js +1 -0
- package/dist/promise.mjs +1 -0
- package/dist/types.d.ts +12 -0
- package/package.json +9 -1
package/dist/clsx.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type ClassNameValue = ClassValue | ClassArray | ClassRecord;
|
|
1
|
+
import { ClassNameValue } from './types.js';
|
|
2
|
+
export { ClassNameValue } from './types.js';
|
|
3
|
+
|
|
5
4
|
/**
|
|
6
5
|
* Utility function to construct class names conditionally
|
|
7
6
|
* @category ClassNames
|
package/dist/number.d.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
|
+
import { AbbreviateOptions } from './types.js';
|
|
2
|
+
export { AbbreviateOptions, AbbreviationSymbols } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a number is within a range
|
|
6
|
+
* @param val The number to check
|
|
7
|
+
* @param min Minimum value
|
|
8
|
+
* @param max Maximum value
|
|
9
|
+
* @category Number
|
|
10
|
+
*/
|
|
11
|
+
declare function inRange(val: number, min: number, max: number): boolean;
|
|
1
12
|
/**
|
|
2
13
|
* Clamps a number between a minimum and maximum value
|
|
14
|
+
* @param val The number to clamp
|
|
15
|
+
* @param min Minimum value
|
|
16
|
+
* @param max Maximum value
|
|
3
17
|
* @category Number
|
|
4
18
|
*/
|
|
5
19
|
declare function clamp(val: number, min: number, max: number): number;
|
|
6
|
-
type AbbreviationSymbols = Record<string, number> | string[];
|
|
7
|
-
type AbbreviateOptions = {
|
|
8
|
-
symbols?: AbbreviationSymbols;
|
|
9
|
-
precision?: number;
|
|
10
|
-
};
|
|
11
20
|
/**
|
|
12
21
|
* Abbreviates a number to a string with a symbol and a precision
|
|
13
22
|
* @param value - The number to abbreviate
|
|
@@ -17,5 +26,4 @@ type AbbreviateOptions = {
|
|
|
17
26
|
declare function abbreviate(value: number, precision?: number): string;
|
|
18
27
|
declare function abbreviate(value: number, options?: AbbreviateOptions): string;
|
|
19
28
|
|
|
20
|
-
export { abbreviate, clamp };
|
|
21
|
-
export type { AbbreviateOptions, AbbreviationSymbols };
|
|
29
|
+
export { abbreviate, clamp, inRange };
|
package/dist/number.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./assertions.js");let t=["","K","M","B","T"];exports.abbreviate=function(r,o){if(!e.isNumber(r))return"0";let i=1,
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./assertions.js");let t=["","K","M","B","T"];exports.abbreviate=function(r,o){if(!e.isNumber(r))return"0";let i=1,n=t;"number"==typeof o?i=o:"object"==typeof o&&(i=o.precision??1,n=o.symbols??t);let s=e.isObject(n)?Object.entries(n):n.map((e,t)=>[e,10**(3*t)]);s.sort((e,t)=>t[1]-e[1]);let u=r<0?"-":"",a=Math.abs(r);for(let[e,t]of s){let r=(a/t).toFixed(i);if(!(1>Number(r)))return`${u}${r}${e}`}return Math.floor(a)===a?r.toString():`${u}${a.toFixed(i)}`},exports.clamp=function(e,t,r){return Math.min(Math.max(e,t),r)},exports.inRange=function(t,r,o){return!!e.isNumber(t)&&!!e.isNumber(r)&&!!e.isNumber(o)&&t>=r&&t<=o};
|
package/dist/number.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isNumber as t,isObject as e}from"./assertions.mjs";function r(t,e,r){return Math.min(Math.max(t,e),r)}let
|
|
1
|
+
import{isNumber as t,isObject as e}from"./assertions.mjs";function r(e,r,o){return!!t(e)&&!!t(r)&&!!t(o)&&e>=r&&e<=o}function o(t,e,r){return Math.min(Math.max(t,e),r)}let n=["","K","M","B","T"];function i(r,o){if(!t(r))return"0";let i=1,a=n;"number"==typeof o?i=o:"object"==typeof o&&(i=o.precision??1,a=o.symbols??n);let f=e(a)?Object.entries(a):a.map((t,e)=>[t,10**(3*e)]);f.sort((t,e)=>e[1]-t[1]);let m=r<0?"-":"",s=Math.abs(r);for(let[t,e]of f){let r=(s/e).toFixed(i);if(!(1>Number(r)))return`${m}${r}${t}`}return Math.floor(s)===s?r.toString():`${m}${s.toFixed(i)}`}export{i as abbreviate,o as clamp,r as inRange};
|
package/dist/object.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { Dictionary } from './types.js';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Checks if a given object has a specified key
|
|
4
5
|
* @category Object
|
|
5
6
|
*/
|
|
6
|
-
declare function keyInObject<T extends object =
|
|
7
|
+
declare function keyInObject<T extends object = Dictionary>(val: T, key: keyof T | (string & {})): boolean;
|
|
7
8
|
/**
|
|
8
9
|
* Picks a set of keys from an object
|
|
9
10
|
* @category Object
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare function sleep(delay: number): Promise<void>;
|
|
2
|
+
/**
|
|
3
|
+
* Retries a function until it succeeds or the maximum number of retries is reached
|
|
4
|
+
* @param func The function to retry
|
|
5
|
+
* @param retries The number of retries
|
|
6
|
+
* @param delay The delay between retries (optional)
|
|
7
|
+
* @returns The result of the function
|
|
8
|
+
*/
|
|
9
|
+
declare function retry<T>(func: () => Promise<T>, retries: number, delay?: number): Promise<T>;
|
|
10
|
+
|
|
11
|
+
export { retry, sleep };
|
package/dist/promise.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./assertions.js"),r=require("./number.js");function t(t){if(e.isNumber(t)){if(!r.inRange(t,0,1/0))throw RangeError("sleep: delay must be a positive finite number")}else throw TypeError("sleep: delay must be a number");return 0===t?Promise.resolve():new Promise(e=>setTimeout(e,t))}async function s(e,r,i=0){try{return await e()}catch(o){if(r>0)return await t(i),s(e,r-1,i);throw o}}exports.retry=s,exports.sleep=t;
|
package/dist/promise.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isNumber as e}from"./assertions.mjs";import{inRange as r}from"./number.mjs";function t(t){if(e(t)){if(!r(t,0,1/0))throw RangeError("sleep: delay must be a positive finite number")}else throw TypeError("sleep: delay must be a number");return 0===t?Promise.resolve():new Promise(e=>setTimeout(e,t))}async function o(e,r,i=0){try{return await e()}catch(s){if(r>0)return await t(i),o(e,r-1,i);throw s}}export{o as retry,t as sleep};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Dictionary<T = unknown> = Record<string, T>;
|
|
2
|
+
type ClassValue = string | number | bigint | null | boolean | undefined;
|
|
3
|
+
type ClassArray = ClassValue[];
|
|
4
|
+
type ClassRecord = Record<string, any>;
|
|
5
|
+
type ClassNameValue = ClassValue | ClassArray | ClassRecord;
|
|
6
|
+
type AbbreviationSymbols = Dictionary<number> | string[];
|
|
7
|
+
type AbbreviateOptions = {
|
|
8
|
+
symbols?: AbbreviationSymbols;
|
|
9
|
+
precision?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type { AbbreviateOptions, AbbreviationSymbols, ClassNameValue, Dictionary };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oviirup/utils",
|
|
3
|
-
"version": "0.0.1-canary.
|
|
3
|
+
"version": "0.0.1-canary.4",
|
|
4
4
|
"description": "Collection of common JavaScript / TypeScript utilities bt @oviirup",
|
|
5
5
|
"repository": "oviirup/utils",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,10 +41,18 @@
|
|
|
41
41
|
"import": "./dist/object.mjs",
|
|
42
42
|
"require": "./dist/object.js"
|
|
43
43
|
},
|
|
44
|
+
"./promise": {
|
|
45
|
+
"types": "./dist/promise.d.ts",
|
|
46
|
+
"import": "./dist/promise.mjs",
|
|
47
|
+
"require": "./dist/promise.js"
|
|
48
|
+
},
|
|
44
49
|
"./string": {
|
|
45
50
|
"types": "./dist/string.d.ts",
|
|
46
51
|
"import": "./dist/string.mjs",
|
|
47
52
|
"require": "./dist/string.js"
|
|
53
|
+
},
|
|
54
|
+
"./types": {
|
|
55
|
+
"types": "./dist/types.d.ts"
|
|
48
56
|
}
|
|
49
57
|
},
|
|
50
58
|
"scripts": {
|