lyb-js 1.1.0 → 1.1.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/README.md CHANGED
@@ -61,6 +61,8 @@ libJsCalculateExpression("(1+2)-(3*4)/5"); //0.6
61
61
 
62
62
  \- [LibJsTagTitleTip-网站标题交互](#LibJsTagTitleTip-网站标题交互)
63
63
 
64
+ \- [LibJsObjToUrlParams-对象转Url参数](#LibJsObjToUrlParams-对象转Url参数)
65
+
64
66
 
65
67
  ### Data-数据
66
68
 
@@ -76,6 +78,8 @@ libJsCalculateExpression("(1+2)-(3*4)/5"); //0.6
76
78
 
77
79
  \- [LibJsStepArray-数组偏移](#LibJsStepArray-数组偏移)
78
80
 
81
+ \- [LibReverseArrayFromIndex-数组定位翻转](#LibReverseArrayFromIndex-数组定位翻转)
82
+
79
83
 
80
84
  ### File-文件
81
85
 
@@ -117,6 +121,8 @@ libJsCalculateExpression("(1+2)-(3*4)/5"); //0.6
117
121
 
118
122
  \- [LibJsRetryRequest-请求重连](#LibJsRetryRequest-请求重连)
119
123
 
124
+ \- [LibNumerStepper-数字步进器](#LibNumerStepper-数字步进器)
125
+
120
126
 
121
127
  ### Random-随机
122
128
 
@@ -217,6 +223,15 @@ libJsSetTitleIcon("我的网站", "https://example.com/favicon.ico");
217
223
  libJsTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
218
224
  ```
219
225
 
226
+ ### LibJsObjToUrlParams-对象转Url参数
227
+
228
+ > 将对象转为地址栏参数
229
+
230
+ ```js
231
+ libJsObjToParams({ name: "John", age: 30, active: true });
232
+ // "name=John&age=30&active=true"
233
+ ```
234
+
220
235
  ## Data-数据
221
236
 
222
237
  ### LibJsChunkArray-数组拆分
@@ -273,6 +288,15 @@ const moved = libJsStepArray([1, 2, 3, 4, 5], 2);
273
288
  console.log(moved); //[4, 5, 1, 2, 3]
274
289
  ```
275
290
 
291
+ ### LibReverseArrayFromIndex-数组定位翻转
292
+
293
+ > 翻转指定索引后面的数组
294
+
295
+ ```ts
296
+ libReverseArrayFromIndex([1, 2, 3, 4, 5], 1);
297
+ // [1, 2, 5, 4, 3]
298
+ ```
299
+
276
300
  ## File-文件
277
301
 
278
302
  ### LibJsDownloadImageLink-图片下载
@@ -308,8 +332,8 @@ libJsImageOptimizerOptionsParams({
308
332
  > 保存`JSON`文件到本地,也支持保存纯文本的`txt`文件
309
333
 
310
334
  ```ts
311
- libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");、
312
- libJsSaveJson("Hellow World!", "example.txt");
335
+ libJsSaveJson("example.json", JSON.stringify({ key: "value" }));
336
+ libJsSaveJson("example.txt", "Hellow World!");
313
337
  ```
314
338
 
315
339
  ## Formatter-格式化
@@ -417,7 +441,7 @@ libJsDecimal(10, 3, "-"); //7
417
441
  libJsDecimal(10, 3, "/", 2); //3.33
418
442
  ```
419
443
 
420
- ## Misc
444
+ ## Misc-杂项
421
445
 
422
446
  ### LibJsRegFormValidate-表单验证
423
447
 
@@ -457,6 +481,17 @@ libJsRetryRequest({
457
481
  .catch(err => console.error(err));
458
482
  ```
459
483
 
484
+ ### LibNumerStepper-数字步进器
485
+
486
+ > 通过调用方法来增加和减少数字索引
487
+
488
+ ```ts
489
+ const stepper = new libNumerStepper(10, (index) => console.log(index));
490
+ stepper.down("add"); // 索引加1
491
+ stepper.updateIndex(5); // 更新索引为5
492
+ stepper.down("sub"); // 索引减1
493
+ ```
494
+
460
495
  ## Random-随机
461
496
 
462
497
  ### LibJsProbabilityResult-概率触发
@@ -0,0 +1,7 @@
1
+ /** @description 将对象转为地址栏参数
2
+ * @param params 对象参数
3
+ * @example
4
+ * libJsObjToUrlParams({ name: "John", age: 30, active: true });
5
+ * // "name=John&age=30&active=true"
6
+ */
7
+ export declare const libJsObjToUrlParams: (params: Record<string, string | number | boolean>) => string;
@@ -0,0 +1,11 @@
1
+ /** @description 将对象转为地址栏参数
2
+ * @param params 对象参数
3
+ * @example
4
+ * libJsObjToUrlParams({ name: "John", age: 30, active: true });
5
+ * // "name=John&age=30&active=true"
6
+ */
7
+ export const libJsObjToUrlParams = (params) => {
8
+ return Object.entries(params)
9
+ .map(([key, value]) => `${key}=${value}`)
10
+ .join("&");
11
+ };
@@ -0,0 +1,8 @@
1
+ /** @description 翻转指定索引后面的数组
2
+ * @param arr 数组
3
+ * @param index 开始索引
4
+ * @example
5
+ * libReverseArrayFromIndex([1, 2, 3, 4, 5], 1);
6
+ * // [1, 2, 5, 4, 3]
7
+ */
8
+ export declare const libReverseArrayFromIndex: <T>(arr: T[], index: number) => T[];
@@ -0,0 +1,14 @@
1
+ /** @description 翻转指定索引后面的数组
2
+ * @param arr 数组
3
+ * @param index 开始索引
4
+ * @example
5
+ * libReverseArrayFromIndex([1, 2, 3, 4, 5], 1);
6
+ * // [1, 2, 5, 4, 3]
7
+ */
8
+ export const libReverseArrayFromIndex = (arr, index) => {
9
+ if (index < 0 || index >= arr.length) {
10
+ throw new Error("Index out of bounds");
11
+ }
12
+ const subArray = arr.slice(index + 1).reverse();
13
+ return [...arr.slice(0, index + 1), ...subArray];
14
+ };
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * @description 保存文件到本地
3
- * @param data 要保存的数据
4
3
  * @param name 文件名
4
+ * @param data 要保存的数据
5
5
  * @example
6
- * libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");
7
- * libJsSaveJson("Hellow World!", "example.txt");
6
+ * libJsSaveJson("example.json", JSON.stringify({ key: "value" }));
7
+ * libJsSaveJson("example.txt", "Hellow World!");
8
8
  */
9
- export declare const libJsSaveJson: (data: BlobPart, name: string) => void;
9
+ export declare const libJsSaveJson: (name: string, data: BlobPart) => void;
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * @description 保存文件到本地
3
- * @param data 要保存的数据
4
3
  * @param name 文件名
4
+ * @param data 要保存的数据
5
5
  * @example
6
- * libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");
7
- * libJsSaveJson("Hellow World!", "example.txt");
6
+ * libJsSaveJson("example.json", JSON.stringify({ key: "value" }));
7
+ * libJsSaveJson("example.txt", "Hellow World!");
8
8
  */
9
- export const libJsSaveJson = (data, name) => {
9
+ export const libJsSaveJson = (name, data) => {
10
10
  const urlObject = window.URL || window.webkitURL;
11
11
  const exportBlob = new Blob([data]);
12
12
  const saveLink = document.createElement("a");
@@ -0,0 +1,34 @@
1
+ /** @description 数字步进器
2
+ * @example
3
+ * const stepper = new libNumerStepper(10, (index) => console.log(index));
4
+ * stepper.down("add"); // 索引加1
5
+ * stepper.updateIndex(5); // 更新索引为5
6
+ * stepper.down("sub"); // 索引减1
7
+ */
8
+ export declare class libNumerStepper {
9
+ /** 数字变动时触发 */
10
+ private _onChange;
11
+ /** 当前按下状态 */
12
+ private _isDown;
13
+ /** 当前数字索引 */
14
+ private _currentIndex;
15
+ /** 定时器ID */
16
+ private _timerId;
17
+ /** 计时器ID */
18
+ private _intervalId;
19
+ /** 金额数 */
20
+ private _numsLength;
21
+ /**
22
+ * @param numsLength 数字长度
23
+ * @param onChange 数字变动时触发
24
+ */
25
+ constructor(numsLength: number, onChange: (index: number) => void);
26
+ /** @description 按下 */
27
+ down(type: "add" | "sub"): void;
28
+ /** @description 更新索引 */
29
+ updateIndex(index: number): void;
30
+ /** @description 抬起 */
31
+ private _up;
32
+ /** @description 处理数字变化 */
33
+ private _handleChange;
34
+ }
@@ -0,0 +1,69 @@
1
+ /** @description 数字步进器
2
+ * @example
3
+ * const stepper = new libNumerStepper(10, (index) => console.log(index));
4
+ * stepper.down("add"); // 索引加1
5
+ * stepper.updateIndex(5); // 更新索引为5
6
+ * stepper.down("sub"); // 索引减1
7
+ */
8
+ export class libNumerStepper {
9
+ /** 数字变动时触发 */
10
+ _onChange;
11
+ /** 当前按下状态 */
12
+ _isDown = false;
13
+ /** 当前数字索引 */
14
+ _currentIndex = 0;
15
+ /** 定时器ID */
16
+ _timerId;
17
+ /** 计时器ID */
18
+ _intervalId;
19
+ /** 金额数 */
20
+ _numsLength = 0;
21
+ /**
22
+ * @param numsLength 数字长度
23
+ * @param onChange 数字变动时触发
24
+ */
25
+ constructor(numsLength, onChange) {
26
+ this._onChange = onChange;
27
+ this._numsLength = numsLength;
28
+ window.addEventListener("pointerup", () => {
29
+ this._isDown && this._up();
30
+ });
31
+ }
32
+ /** @description 按下 */
33
+ down(type) {
34
+ this._isDown = true;
35
+ this._handleChange(type);
36
+ this._timerId = setTimeout(() => {
37
+ if (this._isDown) {
38
+ this._intervalId = setInterval(() => {
39
+ this._handleChange(type);
40
+ }, 100);
41
+ }
42
+ }, 100);
43
+ }
44
+ /** @description 更新索引 */
45
+ updateIndex(index) {
46
+ this._currentIndex = index;
47
+ }
48
+ /** @description 抬起 */
49
+ _up() {
50
+ this._isDown = false;
51
+ clearTimeout(this._timerId);
52
+ clearInterval(this._intervalId);
53
+ }
54
+ /** @description 处理数字变化 */
55
+ _handleChange(type) {
56
+ if (type === "add") {
57
+ if (this._currentIndex < this._numsLength - 1) {
58
+ this._currentIndex++;
59
+ this._onChange(this._currentIndex);
60
+ }
61
+ }
62
+ else if (type === "sub") {
63
+ if (this._currentIndex > 0) {
64
+ this._currentIndex--;
65
+ this._onChange(this._currentIndex);
66
+ }
67
+ }
68
+ }
69
+ }
package/dist/libJs.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { libNumerStepper } from "./Misc/LibNumerStepper";
1
2
  /** @description 基础方法 */
2
3
  export declare const Base: {
3
4
  /**
@@ -69,6 +70,13 @@ export declare const Browser: {
69
70
  * libJsTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
70
71
  */
71
72
  libJsTagTitleTip: (backTitle: string, leaveTitle: string) => void;
73
+ /** @description 对象转为url参数
74
+ * @param params 对象参数
75
+ * @example
76
+ * libJsObjToUrlParams({ name: "John", age: 30, active: true });
77
+ * // "name=John&age=30&active=true"
78
+ */
79
+ libJsObjToUrlParams: (params: Record<string, string | number | boolean>) => string;
72
80
  };
73
81
  /** @description 数据相关方法 */
74
82
  export declare const Data: {
@@ -122,6 +130,14 @@ export declare const Data: {
122
130
  * console.log(moved); //[4, 5, 1, 2, 3]
123
131
  */
124
132
  libJsStepArray: <T>(arr: T[], step: number) => T[];
133
+ /** @description 翻转指定索引后面的数组
134
+ * @param arr 数组
135
+ * @param index 开始索引
136
+ * @example
137
+ * libReverseArrayFromIndex([1, 2, 3, 4, 5], 1);
138
+ * // [1, 2, 5, 4, 3]
139
+ */
140
+ libReverseArrayFromIndex: <T>(arr: T[], index: number) => T[];
125
141
  };
126
142
  /** @description 文件相关方法 */
127
143
  export declare const File: {
@@ -155,9 +171,9 @@ export declare const File: {
155
171
  * @param data 要保存的数据
156
172
  * @param name 文件名
157
173
  * @example
158
- * libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");
174
+ * libJsSaveJson("example.json", JSON.stringify({ key: "value" }));
159
175
  */
160
- libJsSaveJson: (data: BlobPart, name: string) => void;
176
+ libJsSaveJson: (name: string, data: BlobPart) => void;
161
177
  };
162
178
  /** @description 格式化相关方法 */
163
179
  export declare const Formatter: {
@@ -326,6 +342,14 @@ export declare const Misc: {
326
342
  retryDelay?: number;
327
343
  onRetry?: () => void;
328
344
  }) => Promise<T>;
345
+ /** @description 数字步进器
346
+ * @example
347
+ * const stepper = new libNumerStepper(10, (index) => console.log(index));
348
+ * stepper.down("add"); // 索引加1
349
+ * stepper.updateIndex(5); // 更新索引为5
350
+ * stepper.down("sub"); // 索引减1
351
+ */
352
+ libNumerStepper: typeof libNumerStepper;
329
353
  };
330
354
  /** @description 随机相关方法 */
331
355
  export declare const Random: {
package/dist/libJs.js CHANGED
@@ -6,12 +6,14 @@ import { libJsIsPad } from "./Browser/LibJsIsPad";
6
6
  import { libJsPathParams } from "./Browser/LibJsPathParams";
7
7
  import { libJsSetTitleIcon } from "./Browser/LibJsSetTitleIcon";
8
8
  import { libJsTagTitleTip } from "./Browser/LibJsTagTitleTip";
9
+ import { libJsObjToUrlParams } from "./Browser/LibJsObjToUrlParams";
9
10
  import { libJsChunkArray } from "./Data/LibJsChunkArray";
10
11
  import { libJsDeepJSONParse } from "./Data/LibJsDeepJSONParse";
11
12
  import { libJsGroupArrayByKey } from "./Data/LibJsGroupArrayByKey";
12
13
  import { libJsMatchEmail } from "./Data/LibJsMatchEmail";
13
14
  import { libJsShuffleArray } from "./Data/LibJsShuffleArray";
14
15
  import { libJsStepArray } from "./Data/LibJsStepArray";
16
+ import { libReverseArrayFromIndex } from "./Data/LibReverseArrayFromIndex";
15
17
  import { libJsDownloadImageLink } from "./File/LibJsDownloadImageLink";
16
18
  import { libJsImageOptimizer } from "./File/LibJsImageOptimizer";
17
19
  import { libJsSaveJson } from "./File/LibJsSaveJson";
@@ -34,6 +36,7 @@ import { libJsUniqueRandomNumbers } from "./Random/LibJsUniqueRandomNumbers";
34
36
  import { libJsSameTimeCheck } from "./Time/LibJsSameTimeCheck";
35
37
  import { libJsTimeAgo } from "./Time/LibJsTimeAgo";
36
38
  import { libJsTimeGreeting } from "./Time/LibJsTimeGreeting";
39
+ import { libNumerStepper } from "./Misc/LibNumerStepper";
37
40
  /** @description 基础方法 */
38
41
  export const Base = {
39
42
  /**
@@ -102,6 +105,13 @@ export const Browser = {
102
105
  * libJsTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
103
106
  */
104
107
  libJsTagTitleTip,
108
+ /** @description 对象转为url参数
109
+ * @param params 对象参数
110
+ * @example
111
+ * libJsObjToUrlParams({ name: "John", age: 30, active: true });
112
+ * // "name=John&age=30&active=true"
113
+ */
114
+ libJsObjToUrlParams,
105
115
  };
106
116
  /** @description 数据相关方法 */
107
117
  export const Data = {
@@ -155,6 +165,14 @@ export const Data = {
155
165
  * console.log(moved); //[4, 5, 1, 2, 3]
156
166
  */
157
167
  libJsStepArray,
168
+ /** @description 翻转指定索引后面的数组
169
+ * @param arr 数组
170
+ * @param index 开始索引
171
+ * @example
172
+ * libReverseArrayFromIndex([1, 2, 3, 4, 5], 1);
173
+ * // [1, 2, 5, 4, 3]
174
+ */
175
+ libReverseArrayFromIndex,
158
176
  };
159
177
  /** @description 文件相关方法 */
160
178
  export const File = {
@@ -188,7 +206,7 @@ export const File = {
188
206
  * @param data 要保存的数据
189
207
  * @param name 文件名
190
208
  * @example
191
- * libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");
209
+ * libJsSaveJson("example.json", JSON.stringify({ key: "value" }));
192
210
  */
193
211
  libJsSaveJson,
194
212
  };
@@ -336,6 +354,14 @@ export const Misc = {
336
354
  * .catch(err => console.error(err));
337
355
  */
338
356
  libJsRetryRequest,
357
+ /** @description 数字步进器
358
+ * @example
359
+ * const stepper = new libNumerStepper(10, (index) => console.log(index));
360
+ * stepper.down("add"); // 索引加1
361
+ * stepper.updateIndex(5); // 更新索引为5
362
+ * stepper.down("sub"); // 索引减1
363
+ */
364
+ libNumerStepper,
339
365
  };
340
366
  /** @description 随机相关方法 */
341
367
  export const Random = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "author": "冷弋白",
5
5
  "description": "lyb自用JS方法库",
6
6
  "license": "ISC",