sunda-tracker 1.0.8

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.
@@ -0,0 +1,216 @@
1
+ /**
2
+ * 将中划线分割的字符串转换为驼峰命名
3
+ * @param {string} str 中划线分割的字符串,例如 "foo-bar-baz"
4
+ * @param {boolean} [isUpperCamelCase=false] 是否转换为大驼峰(首字母大写),默认为小驼峰
5
+ * @returns {string} 驼峰命名的字符串,例如 "fooBarBaz" 或 "FooBarBaz"
6
+ */
7
+ export function kebabToCamelCase(str, isUpperCamelCase = false) {
8
+ if (!str || typeof str !== 'string') {
9
+ return ''
10
+ }
11
+
12
+ const parts = str.split('-')
13
+
14
+ let result = isUpperCamelCase ?
15
+ parts[0].charAt(0).toUpperCase() + parts[0].slice(1) :
16
+ parts[0]
17
+
18
+ for (let i = 1; i < parts.length; i++) {
19
+ const part = parts[i]
20
+ if (part.length > 0) {
21
+ result += part.charAt(0).toUpperCase() + part.slice(1)
22
+ }
23
+ }
24
+
25
+ return result
26
+ }
27
+
28
+ /**
29
+ * 简易版的日期格式化函数,类似 moment().format()
30
+ * @param {Date|string|number} date - 日期对象、日期字符串或时间戳
31
+ * @param {string} formatStr - 格式化字符串,例如 'YYYY-MM-DD HH:mm:ss'
32
+ * @returns {string} 格式化后的日期字符串
33
+ */
34
+ function formatDate(date = new Date(), formatStr = 'YYYY-MM-DD') {
35
+ // 如果传入的不是 Date 对象,则转换为 Date 对象
36
+ const dateObj = date instanceof Date ? date : new Date(date)
37
+
38
+ // 获取日期的各个部分
39
+ const year = dateObj.getFullYear()
40
+ const month = dateObj.getMonth() + 1 // getMonth() 返回 0-11
41
+ const day = dateObj.getDate()
42
+ const hours = dateObj.getHours()
43
+ const minutes = dateObj.getMinutes()
44
+ const seconds = dateObj.getSeconds()
45
+
46
+ // 定义格式化规则
47
+ const formatRules = {
48
+ YYYY: year,
49
+ YY: String(year).slice(-2),
50
+ MM: month < 10 ? `0${month}` : month,
51
+ M: month,
52
+ DD: day < 10 ? `0${day}` : day,
53
+ D: day,
54
+ HH: hours < 10 ? `0${hours}` : hours,
55
+ H: hours,
56
+ mm: minutes < 10 ? `0${minutes}` : minutes,
57
+ m: minutes,
58
+ ss: seconds < 10 ? `0${seconds}` : seconds,
59
+ s: seconds
60
+ }
61
+
62
+ // 替换格式化字符串中的占位符
63
+ let result = formatStr
64
+ for (const [key, value] of Object.entries(formatRules)) {
65
+ result = result.replace(new RegExp(key, 'g'), value)
66
+ }
67
+
68
+ return result
69
+ }
70
+
71
+ export function moment(date) {
72
+ return {
73
+ _date: date ? new Date(date) : new Date(),
74
+ format(formatStr) {
75
+ return formatDate(this._date, formatStr)
76
+ }
77
+ }
78
+ }
79
+
80
+ /**
81
+ * 生成 UUID v4 (基于随机数)
82
+ * 格式: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
83
+ * 其中 x 是任何十六进制数字,y 是 8、9、A 或 B 中的一个
84
+ * @returns {string} 生成的 UUID v4 字符串
85
+ */
86
+ export function uuidv4() {
87
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
88
+ // 生成 0-15 的随机数
89
+ const r = (Math.random() * 16) | 0
90
+ // 如果当前字符是 'x',使用 r,否则使用 (r & 0x3 | 0x8)
91
+ // 这确保了 UUID v4 规范中的特定位模式
92
+ const v = c === 'x' ? r : (r & 0x3) | 0x8
93
+ return v.toString(16)
94
+ })
95
+ }
96
+
97
+ /**
98
+ * 节流函数 - 限制函数在指定时间内最多执行一次
99
+ * 类似于 Lodash 的 throttle 函数
100
+ *
101
+ * @param {Function} func - 需要节流的函数
102
+ * @param {number} wait - 等待时间,单位为毫秒
103
+ * @param {Object} options - 配置选项
104
+ * @param {boolean} [options.leading=true] - 是否在开始时立即执行一次
105
+ * @param {boolean} [options.trailing=true] - 是否在结束时再执行一次
106
+ * @returns {Function} 返回节流后的函数
107
+ */
108
+ export function throttle(func, wait, options = {}) {
109
+ // 默认配置
110
+ const { leading = true, trailing = true } = options
111
+
112
+ let timeout = null // 定时器
113
+ let lastArgs = null // 最后一次调用的参数
114
+ let lastThis = null // 最后一次调用的 this 上下文
115
+ let lastCallTime = 0 // 上次执行的时间
116
+ let result // 函数执行结果
117
+
118
+ // 检查是否可以执行函数
119
+ function shouldInvoke(time) {
120
+ const timeSinceLastCall = time - lastCallTime
121
+
122
+ // 首次调用 或 超过等待时间
123
+ return lastCallTime === 0 || timeSinceLastCall >= wait
124
+ }
125
+
126
+ // 实际执行函数
127
+ function invokeFunc(time) {
128
+ const args = lastArgs
129
+ const thisArg = lastThis
130
+
131
+ // 重置
132
+ lastArgs = lastThis = null
133
+ lastCallTime = time
134
+
135
+ // 执行原函数
136
+ result = func.apply(thisArg, args)
137
+ return result
138
+ }
139
+
140
+ // 开始定时器
141
+ function startTimer(pendingFunc, wait) {
142
+ return setTimeout(pendingFunc, wait)
143
+ }
144
+
145
+ // 尾随调用
146
+ function trailingEdge(time) {
147
+ timeout = null
148
+
149
+ // 如果有尾随调用并且有参数,则执行函数
150
+ if (trailing && lastArgs) {
151
+ return invokeFunc(time)
152
+ }
153
+
154
+ // 重置
155
+ lastArgs = lastThis = null
156
+ return result
157
+ }
158
+
159
+ // 取消执行
160
+ function cancel() {
161
+ if (timeout !== null) {
162
+ clearTimeout(timeout)
163
+ }
164
+ lastCallTime = 0
165
+ timeout = lastArgs = lastThis = null
166
+ }
167
+
168
+ // 立即执行
169
+ function flush() {
170
+ return timeout === null ? result : trailingEdge(Date.now())
171
+ }
172
+
173
+ // 检查是否正在等待执行
174
+ function pending() {
175
+ return timeout !== null
176
+ }
177
+
178
+ // 主函数
179
+ function throttled(...args) {
180
+ const time = Date.now()
181
+ const isInvoking = shouldInvoke(time)
182
+
183
+ // 保存当前调用的上下文和参数
184
+ lastArgs = args
185
+ // eslint-disable-next-line no-invalid-this
186
+ lastThis = this
187
+
188
+ // 如果可以执行
189
+ if (isInvoking) {
190
+ // 如果是首次调用且不需要立即执行,则设置调用时间但不执行
191
+ if (lastCallTime === 0 && !leading) {
192
+ lastCallTime = time
193
+ return result
194
+ }
195
+
196
+ // 正常情况,立即执行
197
+ return invokeFunc(time)
198
+ }
199
+
200
+ // 如果定时器不存在且需要尾随调用,则启动定时器
201
+ if (timeout === null && trailing) {
202
+ // 计算剩余等待时间
203
+ const remainingWait = wait - (time - lastCallTime)
204
+ timeout = startTimer(() => trailingEdge(Date.now()), remainingWait)
205
+ }
206
+
207
+ return result
208
+ }
209
+
210
+ // 添加方法
211
+ throttled.cancel = cancel
212
+ throttled.flush = flush
213
+ throttled.pending = pending
214
+
215
+ return throttled
216
+ }
package/vite.config.js ADDED
@@ -0,0 +1,33 @@
1
+ import { defineConfig } from 'vite'
2
+ import { resolve } from 'path'
3
+ import babel from 'vite-plugin-babel';
4
+
5
+ export default defineConfig({
6
+ plugins: [
7
+ babel()
8
+ ],
9
+ resolve: {
10
+ alias: {
11
+ '~': resolve(__dirname, './'),
12
+ '@': resolve(__dirname, './src')
13
+ },
14
+ extensions: ['.js']
15
+ },
16
+ build: {
17
+ lib: {
18
+ // 设置入口文件
19
+ entry: resolve(__dirname, 'src/index.js'),
20
+ name: 'sundaTracker',
21
+ fileName: (format) => `sunda-tracker.${format}.js`
22
+ },
23
+
24
+ rollupOptions: {
25
+ external: ['axios'],
26
+ output: {
27
+ globals: {
28
+ // vue: 'Vue'
29
+ }
30
+ }
31
+ }
32
+ }
33
+ })