@ztimson/utils 0.15.6 → 0.16.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.
- package/README.md +91 -0
- package/dist/array.d.ts +47 -2
- package/dist/files.d.ts +33 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +145 -149
- package/dist/index.mjs.map +1 -1
- package/dist/misc.d.ts +0 -19
- package/dist/objects.d.ts +34 -8
- package/dist/promise-progress.d.ts +20 -0
- package/dist/string.d.ts +60 -4
- package/dist/time.d.ts +8 -0
- package/dist/types.d.ts +18 -0
- package/package.json +4 -1
package/dist/string.d.ts
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generate a random hexadecimal value
|
|
3
|
+
*
|
|
4
|
+
* @param {number} length Number of hexadecimal place values
|
|
5
|
+
* @return {string} Hexadecimal number as a string
|
|
6
|
+
*/
|
|
7
|
+
export declare function randomHex(length: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* Convert number of bytes into a human-readable size
|
|
10
|
+
*
|
|
11
|
+
* @param {number} bytes Number of bytes
|
|
12
|
+
* @param {number} decimals Decimal places to preserve
|
|
13
|
+
* @return {string} Formated size
|
|
14
|
+
*/
|
|
3
15
|
export declare function formatBytes(bytes: number, decimals?: number): string;
|
|
16
|
+
/**
|
|
17
|
+
* Extract numbers from a string & create a formated phone number: +1 (123) 456-7890
|
|
18
|
+
*
|
|
19
|
+
* @param {string} number String that will be parsed for numbers
|
|
20
|
+
* @return {string} Formated phone number
|
|
21
|
+
*/
|
|
4
22
|
export declare function formatPhoneNumber(number: string): string;
|
|
5
23
|
/**
|
|
6
24
|
* Insert a string into another string at a given position
|
|
7
25
|
*
|
|
8
26
|
* @example
|
|
9
|
-
* ```
|
|
27
|
+
* ```js
|
|
10
28
|
* console.log(insertAt('Hello world!', ' glorious', 5);
|
|
11
29
|
* // Output: Hello glorious world!
|
|
12
30
|
* ```
|
|
@@ -17,7 +35,24 @@ export declare function formatPhoneNumber(number: string): string;
|
|
|
17
35
|
* @returns {string} - New string
|
|
18
36
|
*/
|
|
19
37
|
export declare function insertAt(target: string, str: string, index: number): String;
|
|
20
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Add padding to string
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```js
|
|
43
|
+
* const now = new Date();
|
|
44
|
+
* const padded = now.getHours() + ':' + pad(now.getMinutes(), 2, '0');
|
|
45
|
+
* console.log(padded); // Output: "2:05"
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @param text Text that will be padded
|
|
49
|
+
* @param {number} length Target length
|
|
50
|
+
* @param {string} char Character to use as padding, defaults to space
|
|
51
|
+
* @param {boolean} start Will pad start of text if true, or the end if false
|
|
52
|
+
* @return {string} Padded string
|
|
53
|
+
* @deprecated Please use `String.padStart` & `String.padEnd`
|
|
54
|
+
*/
|
|
55
|
+
export declare function pad(text: any, length: number, char?: string, start?: boolean): any;
|
|
21
56
|
/**
|
|
22
57
|
* Generate a string of random characters.
|
|
23
58
|
*
|
|
@@ -58,8 +93,29 @@ export declare function randomStringBuilder(length: number, letters?: boolean, n
|
|
|
58
93
|
* @return {RegExpExecArray[]} Found matches.
|
|
59
94
|
*/
|
|
60
95
|
export declare function matchAll(value: string, regex: RegExp | string): RegExpExecArray[];
|
|
96
|
+
/** Parts of a URL */
|
|
97
|
+
export type ParsedUrl = {
|
|
98
|
+
protocol?: string;
|
|
99
|
+
subdomain?: string;
|
|
100
|
+
domain: string;
|
|
101
|
+
host: string;
|
|
102
|
+
port?: number;
|
|
103
|
+
path?: string;
|
|
104
|
+
query?: {
|
|
105
|
+
[name: string]: string;
|
|
106
|
+
};
|
|
107
|
+
fragment?: string;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Break a URL string into its parts for easy parsing
|
|
111
|
+
*
|
|
112
|
+
* @param {string} url URL string that will be parsed
|
|
113
|
+
* @returns {RegExpExecArray} Parts of URL
|
|
114
|
+
*/
|
|
115
|
+
export declare function parseUrl(url: string): ParsedUrl;
|
|
61
116
|
/**
|
|
62
117
|
* Create MD5 hash using native javascript
|
|
118
|
+
*
|
|
63
119
|
* @param d String to hash
|
|
64
120
|
* @returns {string} Hashed string
|
|
65
121
|
*/
|
package/dist/time.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return date formated highest to lowest: YYYY-MM-DD H:mm AM
|
|
3
|
+
*
|
|
4
|
+
* @param {Date | number | string} date Date or timestamp to convert to string
|
|
5
|
+
* @return {string} Formated date
|
|
6
|
+
*/
|
|
1
7
|
export declare function formatDate(date: Date | number | string): string;
|
|
2
8
|
/**
|
|
3
9
|
* Use in conjunction with `await` to pause an async script
|
|
@@ -6,6 +12,7 @@ export declare function formatDate(date: Date | number | string): string;
|
|
|
6
12
|
* ```js
|
|
7
13
|
* await sleep(1000) // Pause for 1 second
|
|
8
14
|
* ```
|
|
15
|
+
*
|
|
9
16
|
* @param {number} ms - Time to pause for in milliseconds
|
|
10
17
|
* @returns {Promise<unknown>} - Resolves promise when it's time to resume
|
|
11
18
|
*/
|
|
@@ -19,6 +26,7 @@ export declare function sleep(ms: number): Promise<void>;
|
|
|
19
26
|
* setTimeout(() => wait = false, 1000);
|
|
20
27
|
* await sleepUntil(() => loading); // Won't continue until loading flag is false
|
|
21
28
|
* ```
|
|
29
|
+
*
|
|
22
30
|
* @param {() => boolean} fn Return true to continue
|
|
23
31
|
* @param {number} checkInterval Run function ever x milliseconds
|
|
24
32
|
* @return {Promise<void>} Callback when sleep is over
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return keys on a type as an array of strings
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* type Person = {
|
|
7
|
+
* firstName: string;
|
|
8
|
+
* lastName: string;
|
|
9
|
+
* age: number;
|
|
10
|
+
* }
|
|
11
|
+
*
|
|
12
|
+
* const keys = typeKeys<Person>();
|
|
13
|
+
* console.log(keys); // Output: ["firstName", "lastName", "age"]
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @return {Array<keyof T>} Available keys
|
|
17
|
+
*/
|
|
18
|
+
export declare function tyoeKeys<T extends object>(): (keyof T)[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ztimson/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Utility library",
|
|
5
5
|
"author": "Zak Timson",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "npx tsc && npx vite build",
|
|
24
|
+
"docs": "typedoc --plugin typedoc-plugin-markdown --cleanOutputDir false --outputFileStrategy modules --hidePageHeader --out ./docs --entryPoints src/**/*.ts --readme none --entryFileName Home",
|
|
24
25
|
"test": "npx jest",
|
|
25
26
|
"test:coverage": "npx jest --coverage",
|
|
26
27
|
"watch": "npx vite build --watch"
|
|
@@ -30,6 +31,8 @@
|
|
|
30
31
|
"jest": "^29.7.0",
|
|
31
32
|
"jest-junit": "^16.0.0",
|
|
32
33
|
"ts-jest": "^29.1.2",
|
|
34
|
+
"typedoc": "^0.26.7",
|
|
35
|
+
"typedoc-plugin-markdown": "^4.2.7",
|
|
33
36
|
"typescript": "^5.3.3",
|
|
34
37
|
"vite": "^5.0.12",
|
|
35
38
|
"vite-plugin-dts": "^3.7.2"
|