minutool 1.0.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 +233 -0
- package/dist/index.d.ts +1178 -0
- package/dist/minutool.js +1181 -0
- package/dist/minutool.umd.cjs +4 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# minut
|
|
2
|
+
|
|
3
|
+
A lightweight collection of utility functions for JavaScript/TypeScript projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install minut
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
or
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add minut
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { formatDate, capitalize, deepClone } from 'minut'
|
|
21
|
+
|
|
22
|
+
// Time utilities
|
|
23
|
+
const now = new Date()
|
|
24
|
+
console.log(formatDate(now, 'YYYY-MM-DD')) // '2026-03-10'
|
|
25
|
+
|
|
26
|
+
// String utilities
|
|
27
|
+
console.log(capitalize('hello')) // 'Hello'
|
|
28
|
+
|
|
29
|
+
// Object utilities
|
|
30
|
+
const obj = { a: 1, b: { c: 2 } }
|
|
31
|
+
const cloned = deepClone(obj)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API Reference
|
|
35
|
+
|
|
36
|
+
### Time Utilities
|
|
37
|
+
|
|
38
|
+
**Constants:**
|
|
39
|
+
- `YEAR_NOW` - Current year
|
|
40
|
+
- `MONTH_NOW` - Current month (1-12)
|
|
41
|
+
- `DATE_NOW` - Current day of month
|
|
42
|
+
- `ONE_MINUTE` - Milliseconds in one minute
|
|
43
|
+
- `ONE_HOUR` - Milliseconds in one hour
|
|
44
|
+
- `ONE_DAY` - Milliseconds in one day
|
|
45
|
+
- `ONE_WEEK` - Milliseconds in one week
|
|
46
|
+
- `ONE_MONTH30` - Milliseconds in 30 days
|
|
47
|
+
- `ONE_MONTH31` - Milliseconds in 31 days
|
|
48
|
+
- `ONE_YEAR365` - Milliseconds in 365 days
|
|
49
|
+
- `ONE_YEAR366` - Milliseconds in 366 days
|
|
50
|
+
- `DAY_SUNDAY` - Sunday constant (0)
|
|
51
|
+
- `DAY_MONDAY` - Monday constant (1)
|
|
52
|
+
- `DAY_TUESDAY` - Tuesday constant (2)
|
|
53
|
+
- `DAY_WEDNESDAY` - Wednesday constant (3)
|
|
54
|
+
- `DAY_THURSDAY` - Thursday constant (4)
|
|
55
|
+
- `DAY_FRIDAY` - Friday constant (5)
|
|
56
|
+
- `DAY_SATURDAY` - Saturday constant (6)
|
|
57
|
+
- `MONTH_NAMES_CN` - Chinese month names array
|
|
58
|
+
- `MONTH_NAMES_SHORT_CN` - Short Chinese month names array
|
|
59
|
+
- `WEEK_DAY_NAMES_SHORT_CN` - Short Chinese weekday names array
|
|
60
|
+
- `WEEK_DAY_NAMES_CN` - Chinese weekday names array
|
|
61
|
+
|
|
62
|
+
**Functions:**
|
|
63
|
+
- `formatDate(format: string, date?: Date | number | string | null): string` - Format date
|
|
64
|
+
- `countDown(timeout: number, tickFunc?: Function, onFinish?: Function)` - Countdown timer
|
|
65
|
+
- `msToHMS(ms: number)` - Convert milliseconds to hours/minutes/seconds
|
|
66
|
+
|
|
67
|
+
### String Utilities
|
|
68
|
+
|
|
69
|
+
**Constants:**
|
|
70
|
+
- `TRIM_BOTH` - Trim both sides constant
|
|
71
|
+
- `TRIM_LEFT` - Trim left side constant
|
|
72
|
+
- `TRIM_RIGHT` - Trim right side constant
|
|
73
|
+
|
|
74
|
+
**Functions:**
|
|
75
|
+
- `capitalize(str: string): string` - Capitalize first letter
|
|
76
|
+
- `camelCase(str: string): string` - Convert to camelCase
|
|
77
|
+
- `kebabCase(str: string): string` - Convert to kebab-case
|
|
78
|
+
- `truncate(str: string, length: number, suffix?: string): string` - Truncate string
|
|
79
|
+
- `trim(str: string, chars?: string, dir?: number): string` - Trim string
|
|
80
|
+
- `stripSlashes(str: string): string` - Remove slashes
|
|
81
|
+
- `cutString(str: string, len: number, eclipse_text?: string): string` - Cut string with ellipsis
|
|
82
|
+
- `extract(es_template: string, params: Record<string, any>): string` - Extract with template
|
|
83
|
+
- `regQuote(str: string): string` - Escape regex special characters
|
|
84
|
+
- `utf8Decode(srcStr: string): string` - Decode UTF-8 string
|
|
85
|
+
- `isJSON(json: string): boolean` - Check if string is valid JSON
|
|
86
|
+
- `utf8Encode(srcStr: string): string` - Encode string to UTF-8
|
|
87
|
+
- `getUTF8StrLen(str: string): number` - Get UTF-8 string byte length
|
|
88
|
+
- `randomString(length?: number, sourceStr?: string): string` - Generate random string
|
|
89
|
+
- `randomWords(count?: number, letterMax?: number): string` - Generate random words
|
|
90
|
+
- `strToPascalCase(str: string, capitalize_first?: boolean): string` - Convert to PascalCase
|
|
91
|
+
|
|
92
|
+
### Object Utilities
|
|
93
|
+
|
|
94
|
+
**Functions:**
|
|
95
|
+
- `deepClone<T>(obj: T): T` - Deep clone object
|
|
96
|
+
- `isEmptyObject(obj: object): boolean` - Check if object is empty
|
|
97
|
+
- `objectKeyMapping(obj: Record<string, any>, mapping: Record<string, string>): Record<string, any>` - Map object keys
|
|
98
|
+
- `objectGet<T>(obj: any, path: string, defaultValue?: T): T` - Get value by path
|
|
99
|
+
- `objectSet(obj: any, path: string, value: any): void` - Set value by path
|
|
100
|
+
- `objectMerge<T>(target: T, ...sources: Partial<T>[]): T` - Deep merge objects
|
|
101
|
+
|
|
102
|
+
### Array Utilities
|
|
103
|
+
|
|
104
|
+
**Functions:**
|
|
105
|
+
- `arrayColumn<T>(arr: T[], col_name: keyof T): any[]` - Extract column from array of objects
|
|
106
|
+
- `arrayIndex<T>(arr: T[], val: T): string | null` - Find index of value
|
|
107
|
+
- `arrayDistinct<T>(arr: T[]): T[]` - Remove duplicates
|
|
108
|
+
- `arrayGroup<T>(arr: T[], by_key: keyof T, limit?: boolean): Record<string, T[] | T>` - Group array by key
|
|
109
|
+
- `arraySortByKey<T>(obj: T): T` - Sort object by keys
|
|
110
|
+
- `arrayChunk<T>(list: T[], size: number): T[][]` - Split array into chunks
|
|
111
|
+
|
|
112
|
+
### Math Utilities
|
|
113
|
+
|
|
114
|
+
**Constants:**
|
|
115
|
+
- `GOLDEN_RATIO` - Golden ratio constant
|
|
116
|
+
|
|
117
|
+
**Functions:**
|
|
118
|
+
- `between(val: number, min: number, max: number, includeEqual?: boolean): boolean` - Check if value is between range
|
|
119
|
+
- `randomInt(min: number, max: number): number` - Generate random integer
|
|
120
|
+
- `round(num: number, precision?: number): number` - Round number to precision
|
|
121
|
+
|
|
122
|
+
### Base64 Utilities
|
|
123
|
+
|
|
124
|
+
**Functions:**
|
|
125
|
+
- `base64Decode(text: string): string` - Decode base64 string
|
|
126
|
+
- `base64UrlSafeEncode(text: string): string` - URL-safe base64 encode
|
|
127
|
+
- `Base64Encode(text: string): string` - Encode to base64
|
|
128
|
+
- `blobToBase64(blob: Blob): Promise<unknown>` - Convert Blob to base64
|
|
129
|
+
|
|
130
|
+
### Browser Utilities
|
|
131
|
+
|
|
132
|
+
**Functions:**
|
|
133
|
+
- `enterFullScreen(element: any): Promise<void>` - Enter fullscreen mode
|
|
134
|
+
- `exitFullScreen(): void` - Exit fullscreen mode
|
|
135
|
+
- `toggleFullScreen(element: any): Promise<unknown>` - Toggle fullscreen
|
|
136
|
+
- `isInFullScreen(): boolean` - Check if in fullscreen
|
|
137
|
+
- `detectLanguage(supportedLngs: string[]): string` - Detect browser language
|
|
138
|
+
|
|
139
|
+
### Cookie Utilities
|
|
140
|
+
|
|
141
|
+
**Functions:**
|
|
142
|
+
- `setCookie(name: string, value: string, days: number, path?: string): void` - Set cookie
|
|
143
|
+
- `getCookie(name: string): string | null` - Get cookie value
|
|
144
|
+
- `deleteCookie(name: string): void` - Delete cookie
|
|
145
|
+
|
|
146
|
+
### DOM Utilities
|
|
147
|
+
|
|
148
|
+
**Functions:**
|
|
149
|
+
- `hide(dom: HTMLElement | string): void` - Hide element
|
|
150
|
+
- `show(dom: HTMLElement | string): void` - Show element
|
|
151
|
+
- `remove(dom: HTMLElement | string): Node | null` - Remove element
|
|
152
|
+
- `disabled(el: HTMLElement | string, disabledClass?: string): void` - Disable element
|
|
153
|
+
- `enabled(el: HTMLElement | string, disabledClass?: string): void` - Enable element
|
|
154
|
+
- `toggleDisabled(el: HTMLElement | string, disabledClass?: string, forceEnabled?: boolean | null): void` - Toggle disabled state
|
|
155
|
+
- `lockElementInteraction(el: HTMLElement | string, payload: Function): void` - Lock element interaction
|
|
156
|
+
- `nodeIndex(node: HTMLElement): number` - Get node index
|
|
157
|
+
- `findAll(selector: string | HTMLElement, parent?: Document | HTMLElement): HTMLElement[]` - Find all elements
|
|
158
|
+
- `findOne(selector: string | HTMLElement, parent?: Document | HTMLElement): HTMLElement` - Find one element
|
|
159
|
+
- `getNodeXPath(el: HTMLElement | null): string | null` - Get element XPath
|
|
160
|
+
- `onDomTreeChange(dom: HTMLElement, callback: Function, includeElementChanged?: boolean): void` - Watch DOM tree changes
|
|
161
|
+
- `mutationEffective(dom: HTMLElement, option: MutationObserverInit, payload: Function, minInterval?: number): void` - Mutation observer with throttle
|
|
162
|
+
- `keepRectInContainer(rect: Dimension, container: Dimension): Dimension` - Keep rectangle in container
|
|
163
|
+
- `rectAssoc(rect1: Dimension, rect2: Dimension): boolean` - Check if rectangles overlap
|
|
164
|
+
- `loadCss(file: string, forceReload?: boolean): Promise<void>` - Load CSS file
|
|
165
|
+
- `loadScript(src: string, forceReload?: boolean): Promise<void>` - Load script file
|
|
166
|
+
- `getDomDimension(dom: HTMLElement): { width: number; height: number }` - Get element dimensions
|
|
167
|
+
- `insertStyleSheet(styleSheetStr: string, id?: string, doc?: Document): HTMLStyleElement | null` - Insert stylesheet
|
|
168
|
+
- `rectInLayout(rect: Dimension, layout: Dimension): boolean` - Check if rect is in layout
|
|
169
|
+
- `createDomByHtml(html: string, parentNode?: HTMLElement | null): Node | Node[]` - Create DOM from HTML
|
|
170
|
+
- `isFocusable(el: HTMLElement): boolean` - Check if element is focusable
|
|
171
|
+
- `getBoundingClientRect(el: HTMLElement, autoFixInvisible?: boolean): RectObject` - Get element bounding rect
|
|
172
|
+
|
|
173
|
+
### HTML Utilities
|
|
174
|
+
|
|
175
|
+
**Constants:**
|
|
176
|
+
- `BLOCK_TAGS` - Array of block-level HTML tags
|
|
177
|
+
- `PAIR_TAGS` - Array of paired HTML tags
|
|
178
|
+
- `SELF_CLOSING_TAGS` - Array of self-closing HTML tags
|
|
179
|
+
- `REMOVABLE_TAGS` - Array of removable HTML tags
|
|
180
|
+
|
|
181
|
+
**Functions:**
|
|
182
|
+
- `html2Text(html: string): string` - Convert HTML to plain text
|
|
183
|
+
- `cssSelectorEscape(str: string): string` - Escape CSS selector
|
|
184
|
+
- `entityToString(entity: string): string` - Convert HTML entity to string
|
|
185
|
+
- `decodeHTMLEntities(str: string): string` - Decode HTML entities
|
|
186
|
+
- `buildHtmlHidden(maps: Record<string, any>): string` - Build hidden input fields
|
|
187
|
+
- `escapeHtml(str: string, tabSize?: number, allowLineBreaker?: boolean): string` - Escape HTML
|
|
188
|
+
- `unescapeHtml(html: string): string` - Unescape HTML
|
|
189
|
+
- `escapeAttr(s: string, preserveCR?: string): string` - Escape HTML attribute
|
|
190
|
+
- `stringToEntity(str: string, radix?: number): string` - Convert string to HTML entity
|
|
191
|
+
- `highlightText(text: string, kw: string, replaceTpl?: string): string` - Highlight text
|
|
192
|
+
|
|
193
|
+
### Image Utilities
|
|
194
|
+
|
|
195
|
+
**Functions:**
|
|
196
|
+
- `imgToBase64(img: HTMLImageElement): string | null` - Convert image to base64
|
|
197
|
+
- `srcToBase64(src: string): Promise<unknown>` - Convert image src to base64
|
|
198
|
+
|
|
199
|
+
### MD5 Utility
|
|
200
|
+
|
|
201
|
+
**Functions:**
|
|
202
|
+
- `MD5(string: string, key?: string, raw?: boolean): string` - Generate MD5 hash
|
|
203
|
+
|
|
204
|
+
### MIME Utilities
|
|
205
|
+
|
|
206
|
+
**Constants:**
|
|
207
|
+
- `MIME_BINARY_DEFAULT` - Default binary MIME type
|
|
208
|
+
- `MIME_EXTENSION_MAP` - Map of file extensions to MIME types
|
|
209
|
+
|
|
210
|
+
### General Utilities
|
|
211
|
+
|
|
212
|
+
**Functions:**
|
|
213
|
+
- `guid(prefix?: string): string` - Generate unique ID
|
|
214
|
+
- `throttle(fn: Function, intervalMiSec: number): Function` - Throttle function
|
|
215
|
+
- `throttleEffect(fn: Function, intervalMiSec: number): Function` - Throttle with effect
|
|
216
|
+
- `debounce(fn: Function, intervalMiSec: number): Function` - Debounce function
|
|
217
|
+
- `isPromise(obj: any): boolean` - Check if value is Promise
|
|
218
|
+
- `isObject(item: any): boolean` - Check if value is object
|
|
219
|
+
- `isFunction(value: any): boolean` - Check if value is function
|
|
220
|
+
- `isURL(str: string): boolean` - Check if string is valid URL
|
|
221
|
+
- `printStack(): void` - Print call stack
|
|
222
|
+
|
|
223
|
+
### File Utilities
|
|
224
|
+
|
|
225
|
+
**Functions:**
|
|
226
|
+
- `sanitizeFileName(name: string): string` - Sanitize file name
|
|
227
|
+
- `blobToDataURL(blob: Blob): Promise<string>` - Convert Blob to data URL
|
|
228
|
+
- `fileToBase64DataURL(file: File | Blob | string): Promise<string>` - Convert file to base64 data URL
|
|
229
|
+
- `downloadFile(uri: string, fileName: string): void` - Download file
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
MIT
|