@ztimson/utils 0.15.6 → 0.16.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/dist/string.d.ts CHANGED
@@ -1,12 +1,23 @@
1
- export declare function countChars(text: string, pattern: RegExp): number;
2
- export declare function createHex(length: number): string;
1
+ /**
2
+ * Convert number of bytes into a human-readable size
3
+ *
4
+ * @param {number} bytes Number of bytes
5
+ * @param {number} decimals Decimal places to preserve
6
+ * @return {string} Formated size
7
+ */
3
8
  export declare function formatBytes(bytes: number, decimals?: number): string;
9
+ /**
10
+ * Extract numbers from a string & create a formated phone number: +1 (123) 456-7890
11
+ *
12
+ * @param {string} number String that will be parsed for numbers
13
+ * @return {string} Formated phone number
14
+ */
4
15
  export declare function formatPhoneNumber(number: string): string;
5
16
  /**
6
17
  * Insert a string into another string at a given position
7
18
  *
8
19
  * @example
9
- * ```
20
+ * ```js
10
21
  * console.log(insertAt('Hello world!', ' glorious', 5);
11
22
  * // Output: Hello glorious world!
12
23
  * ```
@@ -17,7 +28,31 @@ export declare function formatPhoneNumber(number: string): string;
17
28
  * @returns {string} - New string
18
29
  */
19
30
  export declare function insertAt(target: string, str: string, index: number): String;
20
- export declare function pad(text: any, length: number, char: string, start?: boolean): any;
31
+ /**
32
+ * Add padding to string
33
+ *
34
+ * @example
35
+ * ```js
36
+ * const now = new Date();
37
+ * const padded = now.getHours() + ':' + pad(now.getMinutes(), 2, '0');
38
+ * console.log(padded); // Output: "2:05"
39
+ * ```
40
+ *
41
+ * @param text Text that will be padded
42
+ * @param {number} length Target length
43
+ * @param {string} char Character to use as padding, defaults to space
44
+ * @param {boolean} start Will pad start of text if true, or the end if false
45
+ * @return {string} Padded string
46
+ * @deprecated Please use `String.padStart` & `String.padEnd`
47
+ */
48
+ export declare function pad(text: any, length: number, char?: string, start?: boolean): any;
49
+ /**
50
+ * Generate a random hexadecimal value
51
+ *
52
+ * @param {number} length Number of hexadecimal place values
53
+ * @return {string} Hexadecimal number as a string
54
+ */
55
+ export declare function randomHex(length: number): string;
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
@@ -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.15.6",
3
+ "version": "0.16.1",
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"