@syzlm/function 1.0.0 → 1.0.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.
Files changed (98) hide show
  1. package/dist/arrays/arrayUtils.d.ts +13 -1
  2. package/dist/arrays/arrayUtils.d.ts.map +1 -1
  3. package/dist/index.cjs.js +1 -1
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/objects/objectUtils.d.ts +7 -0
  8. package/dist/objects/objectUtils.d.ts.map +1 -1
  9. package/dist/storage/index.d.ts +2 -0
  10. package/dist/storage/index.d.ts.map +1 -0
  11. package/dist/storage/storage.d.ts +13 -0
  12. package/dist/storage/storage.d.ts.map +1 -0
  13. package/dist/strings/stringUtils.d.ts.map +1 -1
  14. package/docs/README.md +32 -0
  15. package/docs/functions/capitalize.md +27 -0
  16. package/docs/functions/chunk.md +39 -0
  17. package/docs/functions/clamp.md +39 -0
  18. package/docs/functions/debounce.md +49 -0
  19. package/docs/functions/deepClone.md +33 -0
  20. package/docs/functions/delay.md +27 -0
  21. package/docs/functions/get.md +39 -0
  22. package/docs/functions/getFontLength.md +27 -0
  23. package/docs/functions/getFontWidth.md +33 -0
  24. package/docs/functions/isPlainObject.md +27 -0
  25. package/docs/functions/merge.md +39 -0
  26. package/docs/functions/parseStrEmpty.md +27 -0
  27. package/docs/functions/randomString.md +27 -0
  28. package/docs/functions/range.md +39 -0
  29. package/docs/functions/round.md +33 -0
  30. package/docs/functions/set.md +45 -0
  31. package/docs/functions/shuffle.md +33 -0
  32. package/docs/functions/simpleUnique.md +40 -0
  33. package/docs/functions/thousandSign.md +27 -0
  34. package/docs/functions/thousandSignZero.md +33 -0
  35. package/docs/functions/throttle.md +49 -0
  36. package/docs/functions/toCamelCase.md +27 -0
  37. package/docs/functions/toFixed.md +33 -0
  38. package/docs/functions/unique.md +39 -0
  39. package/package.json +22 -4
  40. package/src/arrays/arrayUtils.ts +33 -3
  41. package/src/arrays/index.ts +1 -1
  42. package/src/numbers/index.ts +1 -1
  43. package/src/numbers/numberUtils.ts +7 -7
  44. package/src/objects/index.ts +1 -1
  45. package/src/objects/objectUtils.ts +18 -13
  46. package/src/storage/storage.ts +5 -4
  47. package/src/strings/index.ts +1 -1
  48. package/src/strings/stringUtils.ts +11 -9
  49. package/src/utils/index.ts +1 -1
  50. package/src/utils/utilFunctions.ts +3 -3
  51. package/README.md +0 -111
  52. package/docs/.nojekyll +0 -1
  53. package/docs/assets/hierarchy.js +0 -1
  54. package/docs/assets/highlight.css +0 -85
  55. package/docs/assets/icons.js +0 -18
  56. package/docs/assets/icons.svg +0 -1
  57. package/docs/assets/main.js +0 -60
  58. package/docs/assets/navigation.js +0 -1
  59. package/docs/assets/search.js +0 -1
  60. package/docs/assets/style.css +0 -1633
  61. package/docs/functions/capitalize.html +0 -4
  62. package/docs/functions/chunk.html +0 -5
  63. package/docs/functions/clamp.html +0 -6
  64. package/docs/functions/debounce.html +0 -5
  65. package/docs/functions/deepClone.html +0 -4
  66. package/docs/functions/delay.html +0 -4
  67. package/docs/functions/get.html +0 -6
  68. package/docs/functions/getFontLength.html +0 -4
  69. package/docs/functions/getFontWidth.html +0 -5
  70. package/docs/functions/isPlainObject.html +0 -4
  71. package/docs/functions/merge.html +0 -5
  72. package/docs/functions/parseStrEmpty.html +0 -4
  73. package/docs/functions/randomString.html +0 -4
  74. package/docs/functions/range.html +0 -6
  75. package/docs/functions/round.html +0 -5
  76. package/docs/functions/set.html +0 -6
  77. package/docs/functions/shuffle.html +0 -4
  78. package/docs/functions/thousandSign.html +0 -4
  79. package/docs/functions/thousandSignZero.html +0 -5
  80. package/docs/functions/throttle.html +0 -5
  81. package/docs/functions/toCamelCase.html +0 -4
  82. package/docs/functions/toFixed.html +0 -5
  83. package/docs/functions/unique.html +0 -4
  84. package/docs/hierarchy.html +0 -1
  85. package/docs/index.html +0 -53
  86. package/docs/modules.html +0 -1
  87. package/jest.config.js +0 -8
  88. package/rollup.config.js +0 -29
  89. package/scripts/deploy-docs.sh +0 -40
  90. package/test-output.txt +0 -23
  91. package/test-tofixed.js +0 -36
  92. package/tests/arrays.test.ts +0 -89
  93. package/tests/numbers.test.ts +0 -140
  94. package/tests/objects.test.ts +0 -189
  95. package/tests/strings.test.ts +0 -197
  96. package/tests/utils.test.ts +0 -121
  97. package/tsconfig.json +0 -21
  98. package/typedoc.json +0 -15
@@ -1,140 +0,0 @@
1
- import { range, round, clamp, toFixed } from '../src/numbers/numberUtils';
2
-
3
- describe('数字工具函数测试', () => {
4
- describe('range 函数', () => {
5
- it('应该正确生成数字范围', () => {
6
- expect(range(1, 5)).toEqual([1, 2, 3, 4, 5]);
7
- expect(range(0, 3)).toEqual([0, 1, 2, 3]);
8
- });
9
-
10
- it('应该正确生成带步长的数字范围', () => {
11
- expect(range(1, 10, 2)).toEqual([1, 3, 5, 7, 9]);
12
- expect(range(0, 10, 3)).toEqual([0, 3, 6, 9]);
13
- });
14
-
15
- it('应该正确处理开始等于结束的情况', () => {
16
- expect(range(5, 5)).toEqual([5]);
17
- expect(range(10, 10, 2)).toEqual([10]);
18
- });
19
-
20
- it('应该正确处理负数范围', () => {
21
- expect(range(-5, 0)).toEqual([-5, -4, -3, -2, -1, 0]);
22
- expect(range(-3, 3, 2)).toEqual([-3, -1, 1, 3]);
23
- });
24
- });
25
-
26
- describe('round 函数', () => {
27
- it('应该正确四舍五入到整数', () => {
28
- expect(round(1.234)).toBe(1);
29
- expect(round(1.567)).toBe(2);
30
- expect(round(1.499)).toBe(1);
31
- });
32
-
33
- it('应该正确保留指定小数位数', () => {
34
- expect(round(1.234, 2)).toBe(1.23);
35
- expect(round(1.567, 2)).toBe(1.57);
36
- expect(round(1.499, 2)).toBe(1.5);
37
- });
38
-
39
- it('应该正确处理负数', () => {
40
- expect(round(-1.234)).toBe(-1);
41
- expect(round(-1.567)).toBe(-2);
42
- expect(round(-1.499, 2)).toBe(-1.5);
43
- });
44
-
45
- it('应该正确处理零', () => {
46
- expect(round(0)).toBe(0);
47
- expect(round(0.499)).toBe(0);
48
- expect(round(0.5)).toBe(1);
49
- });
50
- });
51
-
52
- describe('clamp 函数', () => {
53
- it('应该正确限制数字在指定范围内', () => {
54
- expect(clamp(5, 1, 10)).toBe(5);
55
- expect(clamp(0, 1, 10)).toBe(1);
56
- expect(clamp(15, 1, 10)).toBe(10);
57
- });
58
-
59
- it('应该正确处理等于边界的情况', () => {
60
- expect(clamp(1, 1, 10)).toBe(1);
61
- expect(clamp(10, 1, 10)).toBe(10);
62
- });
63
-
64
- it('应该正确处理负数范围', () => {
65
- expect(clamp(-5, -10, 0)).toBe(-5);
66
- expect(clamp(-15, -10, 0)).toBe(-10);
67
- expect(clamp(5, -10, 0)).toBe(0);
68
- });
69
-
70
- it('应该正确处理零', () => {
71
- expect(clamp(0, -5, 5)).toBe(0);
72
- expect(clamp(0, 1, 10)).toBe(1);
73
- expect(clamp(0, -10, -1)).toBe(-1);
74
- });
75
- });
76
-
77
- describe('toFixed 函数', () => {
78
- it('应该正确格式化数字到指定小数位数', () => {
79
- expect(toFixed(1.234, 2)).toBe('1.23');
80
- expect(toFixed(1.567, 2)).toBe('1.57');
81
- expect(toFixed(1.499, 2)).toBe('1.50');
82
- });
83
-
84
- it('应该正确处理零', () => {
85
- expect(toFixed(0, 2)).toBe('0.00');
86
- expect(toFixed(0.499, 1)).toBe('0.5');
87
- expect(toFixed(0.5, 0)).toBe('1');
88
- });
89
-
90
- it('应该正确处理负数', () => {
91
- expect(toFixed(-1.234, 2)).toBe('-1.23');
92
- expect(toFixed(-1.567, 2)).toBe('-1.57');
93
- expect(toFixed(-1.499, 2)).toBe('-1.50');
94
- });
95
-
96
- it('应该正确处理整数', () => {
97
- expect(toFixed(123, 2)).toBe('123.00');
98
- expect(toFixed(123456, 3)).toBe('123456.000');
99
- });
100
-
101
- it('应该正确处理高精度小数', () => {
102
- // 测试经典的精度问题
103
- expect(toFixed(1.005, 2)).toBe('1.01');
104
- expect(toFixed(0.1 + 0.2, 1)).toBe('0.3');
105
- expect(toFixed(2.005, 2)).toBe('2.01');
106
- expect(toFixed(1.0005, 3)).toBe('1.001');
107
- });
108
-
109
- it('应该正确处理更多精度的情况', () => {
110
- expect(toFixed(1.23456789, 6)).toBe('1.234568');
111
- expect(toFixed(0.0000005, 6)).toBe('0.000001');
112
- });
113
-
114
- it('应该正确处理科学计数法', () => {
115
- expect(toFixed(1.234e10, 2)).toBe('12340000000.00');
116
- expect(toFixed(1.234e-3, 5)).toBe('0.00123');
117
- expect(toFixed(1.23456789e-5, 8)).toBe('0.00001235');
118
- });
119
-
120
- it('应该正确处理默认小数位数', () => {
121
- expect(toFixed(1.234)).toBe('1');
122
- expect(toFixed(1.567)).toBe('2');
123
- });
124
-
125
- it('应该正确处理负数小数位数', () => {
126
- expect(toFixed(1.234, -2)).toBe('1');
127
- expect(toFixed(1.567, -1)).toBe('2');
128
- });
129
-
130
- it('应该正确处理大量小数位数', () => {
131
- expect(toFixed(1.23456789, 10)).toBe('1.2345678900');
132
- });
133
-
134
- it('应该正确处理需要进位到整数部分的情况', () => {
135
- expect(toFixed(9.999, 2)).toBe('10.00');
136
- expect(toFixed(99.999, 1)).toBe('100.0');
137
- expect(toFixed(999.999, 0)).toBe('1000');
138
- });
139
- });
140
- });
@@ -1,189 +0,0 @@
1
- import { deepClone, merge, get, set, isPlainObject } from '../src/objects/objectUtils';
2
-
3
- describe('对象工具函数测试', () => {
4
- describe('deepClone 函数', () => {
5
- it('应该正确深拷贝普通对象', () => {
6
- const obj = { a: 1, b: { c: 2, d: [3, 4] } };
7
- const cloned = deepClone(obj);
8
-
9
- expect(cloned).toEqual(obj);
10
- expect(cloned).not.toBe(obj);
11
- expect(cloned.b).not.toBe(obj.b);
12
- expect(cloned.b.d).not.toBe(obj.b.d);
13
- });
14
-
15
- it('应该正确深拷贝数组', () => {
16
- const arr = [1, { a: 2 }, [3, 4]];
17
- const cloned = deepClone(arr);
18
-
19
- expect(cloned).toEqual(arr);
20
- expect(cloned).not.toBe(arr);
21
- expect(cloned[1]).not.toBe(arr[1]);
22
- expect(cloned[2]).not.toBe(arr[2]);
23
- });
24
-
25
- it('应该正确深拷贝日期对象', () => {
26
- const date = new Date();
27
- const cloned = deepClone(date);
28
-
29
- expect(cloned).toEqual(date);
30
- expect(cloned).not.toBe(date);
31
- expect(cloned instanceof Date).toBe(true);
32
- });
33
-
34
- it('应该正确处理null和基本类型', () => {
35
- expect(deepClone(null)).toBeNull();
36
- expect(deepClone(42)).toBe(42);
37
- expect(deepClone('string')).toBe('string');
38
- expect(deepClone(true)).toBe(true);
39
- });
40
-
41
- it('应该正确处理嵌套的复杂对象', () => {
42
- const complexObj = {
43
- a: 1,
44
- b: {
45
- c: [2, 3, { d: 4 }],
46
- e: new Date(),
47
- f: { g: { h: 5 } }
48
- }
49
- };
50
-
51
- const cloned = deepClone(complexObj);
52
- expect(cloned).toEqual(complexObj);
53
- expect(cloned).not.toBe(complexObj);
54
- expect(cloned.b).not.toBe(complexObj.b);
55
- expect(cloned.b.c).not.toBe(complexObj.b.c);
56
- expect(cloned.b.c[2]).not.toBe(complexObj.b.c[2]);
57
- expect(cloned.b.e).not.toBe(complexObj.b.e);
58
- expect(cloned.b.f).not.toBe(complexObj.b.f);
59
- expect(cloned.b.f.g).not.toBe(complexObj.b.f.g);
60
- });
61
- });
62
-
63
- describe('merge 函数', () => {
64
- it('应该正确合并多个对象', () => {
65
- const obj1 = { a: 1, b: 2 };
66
- const obj2 = { b: 3, c: 4 };
67
- const obj3 = { d: 5 };
68
-
69
- const result = merge(obj1, obj2, obj3);
70
-
71
- expect(result).toEqual({ a: 1, b: 3, c: 4, d: 5 });
72
- expect(result).not.toBe(obj1);
73
- expect(result).not.toBe(obj2);
74
- expect(result).not.toBe(obj3);
75
- });
76
-
77
- it('应该正确处理空对象', () => {
78
- const obj = { a: 1, b: 2 };
79
- expect(merge(obj, {})).toEqual(obj);
80
- expect(merge({}, obj)).toEqual(obj);
81
- expect(merge({}, {})).toEqual({});
82
- });
83
-
84
- it('应该正确处理单个对象', () => {
85
- const obj = { a: 1, b: 2 };
86
- expect(merge(obj)).toEqual(obj);
87
- expect(merge(obj)).not.toBe(obj); // 应该返回新对象
88
- });
89
-
90
- it('应该正确合并嵌套对象', () => {
91
- const obj1 = { a: { b: 1 } };
92
- const obj2 = { a: { c: 2 } };
93
-
94
- const result = merge(obj1, obj2);
95
-
96
- // 注意:merge函数使用Object.assign,只做浅合并
97
- expect(result).toEqual({ a: { c: 2 } });
98
- });
99
- });
100
-
101
- describe('get 函数', () => {
102
- const obj = { a: { b: { c: 1, d: [2, 3] } }, e: 4 };
103
-
104
- it('应该正确获取嵌套属性', () => {
105
- expect(get(obj, 'a.b.c')).toBe(1);
106
- expect(get(obj, 'e')).toBe(4);
107
- });
108
-
109
- it('应该正确获取数组元素', () => {
110
- expect(get(obj, 'a.b.d.0')).toBe(2);
111
- expect(get(obj, 'a.b.d.1')).toBe(3);
112
- });
113
-
114
- it('应该在属性不存在时返回默认值', () => {
115
- expect(get(obj, 'a.b.x', 'default')).toBe('default');
116
- expect(get(obj, 'x', null)).toBeNull();
117
- });
118
-
119
- it('应该在路径中断时返回默认值', () => {
120
- expect(get(obj, 'a.x.c', 'default')).toBe('default');
121
- expect(get(null, 'a.b.c', 'default')).toBe('default');
122
- });
123
-
124
- it('应该在未提供默认值时返回undefined', () => {
125
- expect(get(obj, 'a.b.x')).toBeUndefined();
126
- expect(get(obj, 'x')).toBeUndefined();
127
- });
128
- });
129
-
130
- describe('set 函数', () => {
131
- it('应该正确设置嵌套属性', () => {
132
- const obj = { a: { b: { c: 1 } } };
133
- set(obj, 'a.b.c', 2);
134
- expect(obj.a.b.c).toBe(2);
135
- });
136
-
137
- it('应该正确创建不存在的路径', () => {
138
- const obj = {};
139
- set(obj, 'a.b.c', 1);
140
- expect(obj).toEqual({ a: { b: { c: 1 } } });
141
- });
142
-
143
- it('应该正确设置数组元素', () => {
144
- const obj = { a: { b: [1, 2, 3] } };
145
- set(obj, 'a.b.1', 4);
146
- expect(obj.a.b).toEqual([1, 4, 3]);
147
- });
148
-
149
- it('应该在路径中断时创建对象', () => {
150
- const obj = { a: null };
151
- set(obj, 'a.b.c', 1);
152
- expect(obj).toEqual({ a: { b: { c: 1 } } });
153
- });
154
-
155
- it('应该返回修改后的对象', () => {
156
- const obj = { a: 1 };
157
- const result = set(obj, 'b', 2);
158
- expect(result).toBe(obj);
159
- expect(result).toEqual({ a: 1, b: 2 });
160
- });
161
- });
162
-
163
- describe('isPlainObject 函数', () => {
164
- it('应该正确识别普通对象', () => {
165
- expect(isPlainObject({})).toBe(true);
166
- expect(isPlainObject({ a: 1 })).toBe(true);
167
- expect(isPlainObject(Object.create(null))).toBe(true);
168
- });
169
-
170
- it('应该正确识别非普通对象', () => {
171
- expect(isPlainObject(null)).toBe(false);
172
- expect(isPlainObject([])).toBe(false);
173
- expect(isPlainObject(new Date())).toBe(false);
174
- expect(isPlainObject(new RegExp(''))).toBe(false);
175
- expect(isPlainObject(function() {})).toBe(false);
176
- expect(isPlainObject(42)).toBe(false);
177
- expect(isPlainObject('string')).toBe(false);
178
- expect(isPlainObject(true)).toBe(false);
179
- });
180
-
181
- it('应该正确识别自定义类的实例', () => {
182
- class CustomClass {
183
- constructor(public value: number) {}
184
- }
185
-
186
- expect(isPlainObject(new CustomClass(42))).toBe(false);
187
- });
188
- });
189
- });
@@ -1,197 +0,0 @@
1
- import { toCamelCase, capitalize, randomString, parseStrEmpty, getFontLength, getFontWidth, thousandSign, thousandSignZero } from '../src/strings/stringUtils';
2
-
3
- describe('字符串工具函数测试', () => {
4
- describe('toCamelCase 函数', () => {
5
- it('应该正确将连字符分隔的字符串转换为驼峰命名', () => {
6
- expect(toCamelCase('hello-world')).toBe('helloWorld');
7
- expect(toCamelCase('hello-world-test')).toBe('helloWorldTest');
8
- });
9
-
10
- it('应该正确将下划线分隔的字符串转换为驼峰命名', () => {
11
- expect(toCamelCase('hello_world')).toBe('helloWorld');
12
- expect(toCamelCase('hello_world_test')).toBe('helloWorldTest');
13
- });
14
-
15
- it('应该正确将空格分隔的字符串转换为驼峰命名', () => {
16
- expect(toCamelCase('hello world')).toBe('helloWorld');
17
- expect(toCamelCase('hello world test')).toBe('helloWorldTest');
18
- });
19
-
20
- it('应该正确处理混合分隔符的字符串', () => {
21
- expect(toCamelCase('hello-world_test')).toBe('helloWorldTest');
22
- expect(toCamelCase('hello world-test')).toBe('helloWorldTest');
23
- });
24
-
25
- it('应该正确处理空字符串', () => {
26
- expect(toCamelCase('')).toBe('');
27
- });
28
-
29
- it('应该正确处理单字符字符串', () => {
30
- expect(toCamelCase('a')).toBe('a');
31
- });
32
- });
33
-
34
- describe('capitalize 函数', () => {
35
- it('应该正确将字符串首字母大写', () => {
36
- expect(capitalize('hello')).toBe('Hello');
37
- expect(capitalize('hello world')).toBe('Hello world');
38
- });
39
-
40
- it('应该正确处理空字符串', () => {
41
- expect(capitalize('')).toBe('');
42
- });
43
-
44
- it('应该正确处理首字母已经大写的字符串', () => {
45
- expect(capitalize('Hello')).toBe('Hello');
46
- });
47
-
48
- it('应该正确处理全大写的字符串', () => {
49
- expect(capitalize('HELLO')).toBe('HELLO');
50
- });
51
- });
52
-
53
- describe('randomString 函数', () => {
54
- it('应该生成指定长度的随机字符串', () => {
55
- expect(randomString(5)).toHaveLength(5);
56
- expect(randomString(10)).toHaveLength(10);
57
- expect(randomString(0)).toHaveLength(0);
58
- });
59
-
60
- it('应该生成只包含指定字符集的字符串', () => {
61
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
62
- const str = randomString(20);
63
- for (const char of str) {
64
- expect(chars).toContain(char);
65
- }
66
- });
67
-
68
- it('应该生成随机的字符串', () => {
69
- const results = new Set();
70
- for (let i = 0; i < 10; i++) {
71
- results.add(randomString(10));
72
- }
73
- expect(results.size).toBeGreaterThan(1);
74
- });
75
- });
76
-
77
- describe('parseStrEmpty 函数', () => {
78
- it('应该将undefined、null转换为空字符串', () => {
79
- expect(parseStrEmpty(undefined as any)).toBe('');
80
- expect(parseStrEmpty(null as any)).toBe('');
81
- });
82
-
83
- it('应该将字符串"undefined"、"null"转换为空字符串', () => {
84
- expect(parseStrEmpty('undefined')).toBe('');
85
- expect(parseStrEmpty('null')).toBe('');
86
- });
87
-
88
- it('应该保持正常字符串不变', () => {
89
- expect(parseStrEmpty('hello')).toBe('hello');
90
- expect(parseStrEmpty('')).toBe('');
91
- expect(parseStrEmpty('0')).toBe('0');
92
- expect(parseStrEmpty('false')).toBe('false');
93
- });
94
- });
95
-
96
- describe('getFontLength 函数', () => {
97
- it('应该正确计算纯英文字符串的长度', () => {
98
- expect(getFontLength('hello')).toBe(5);
99
- expect(getFontLength('hello world')).toBe(11);
100
- });
101
-
102
- it('应该正确计算包含中文的字符串的长度', () => {
103
- expect(getFontLength('你好')).toBe(4);
104
- expect(getFontLength('hello你好')).toBe(5 + 4);
105
- });
106
-
107
- it('应该正确处理空字符串', () => {
108
- expect(getFontLength('')).toBe(0);
109
- });
110
-
111
- it('应该正确处理特殊字符', () => {
112
- expect(getFontLength('hello@#$%^&*()')).toBe(15);
113
- expect(getFontLength('你好@#$%^&*()')).toBe(4 + 10);
114
- });
115
- });
116
-
117
- describe('getFontWidth 函数', () => {
118
- it('应该在有DOM环境时正确计算文字宽度', () => {
119
- // 由于这个函数依赖DOM环境,在Node.js测试中会失败
120
- // 我们可以添加一个简单的检查
121
- if (typeof document !== 'undefined') {
122
- expect(getFontWidth('hello')).toBeGreaterThan(0);
123
- expect(getFontWidth('hello world')).toBeGreaterThan(getFontWidth('hello'));
124
- } else {
125
- // 在Node.js环境中跳过这个测试
126
- console.log('跳过getFontWidth测试,因为没有DOM环境');
127
- }
128
- });
129
- });
130
-
131
- describe('thousandSign 函数', () => {
132
- it('应该正确格式化整数', () => {
133
- expect(thousandSign(1234)).toBe('1,234');
134
- expect(thousandSign(1234567)).toBe('1,234,567');
135
- });
136
-
137
- it('应该正确格式化小数', () => {
138
- expect(thousandSign(1234.56)).toBe('1,234.56');
139
- expect(thousandSign(1234567.89)).toBe('1,234,567.89');
140
- });
141
-
142
- it('应该正确格式化字符串类型的数字', () => {
143
- expect(thousandSign('1234')).toBe('1,234');
144
- expect(thousandSign('1234.56')).toBe('1,234.56');
145
- });
146
-
147
- it('应该正确处理0', () => {
148
- expect(thousandSign(0)).toBe('0');
149
- });
150
-
151
- it('应该正确处理null和undefined', () => {
152
- expect(thousandSign(null as any)).toBe('0');
153
- expect(thousandSign(undefined as any)).toBe('0');
154
- });
155
-
156
- it('应该正确处理负数', () => {
157
- expect(thousandSign(-1234)).toBe('-1,234');
158
- expect(thousandSign(-1234.56)).toBe('-1,234.56');
159
- });
160
- });
161
-
162
- describe('thousandSignZero 函数', () => {
163
- it('应该正确格式化并补零', () => {
164
- expect(thousandSignZero(1234)).toBe('1,234.00');
165
- expect(thousandSignZero(1234.5)).toBe('1,234.50');
166
- });
167
-
168
- it('应该使用指定的小数位数', () => {
169
- expect(thousandSignZero(1234, 3)).toBe('1,234.000');
170
- expect(thousandSignZero(1234.5678, 3)).toBe('1,234.568');
171
- });
172
-
173
- it('应该正确处理精度问题', () => {
174
- // 测试1.005这个经典的精度问题
175
- expect(thousandSignZero(1.005, 2)).toBe('1.01');
176
- });
177
-
178
- it('应该正确格式化字符串类型的数字', () => {
179
- expect(thousandSignZero('1234')).toBe('1,234.00');
180
- expect(thousandSignZero('1234.5')).toBe('1,234.50');
181
- });
182
-
183
- it('应该正确处理0', () => {
184
- expect(thousandSignZero(0)).toBe('0.00');
185
- });
186
-
187
- it('应该正确处理null和undefined', () => {
188
- expect(thousandSignZero(null as any)).toBe('0.00');
189
- expect(thousandSignZero(undefined as any)).toBe('0.00');
190
- });
191
-
192
- it('应该正确处理负数', () => {
193
- expect(thousandSignZero(-1234)).toBe('-1,234.00');
194
- expect(thousandSignZero(-1234.5)).toBe('-1,234.50');
195
- });
196
- });
197
- });
@@ -1,121 +0,0 @@
1
- import { debounce, throttle, delay } from '../src/utils/utilFunctions';
2
-
3
- describe('工具函数测试', () => {
4
- // 清除所有定时器
5
- beforeEach(() => {
6
- jest.useFakeTimers();
7
- });
8
-
9
- afterEach(() => {
10
- jest.useRealTimers();
11
- });
12
-
13
- describe('debounce 函数', () => {
14
- it('应该在指定时间后只执行一次函数', () => {
15
- const mockFn = jest.fn();
16
- const debouncedFn = debounce(mockFn, 100);
17
-
18
- // 连续调用三次
19
- debouncedFn('arg1', 'arg2');
20
- debouncedFn('arg1', 'arg2');
21
- debouncedFn('arg1', 'arg2');
22
-
23
- // 快速调用后检查是否有调用
24
- expect(mockFn).not.toHaveBeenCalled();
25
-
26
- // 等待指定时间后检查是否只调用了一次
27
- jest.advanceTimersByTime(100);
28
- expect(mockFn).toHaveBeenCalledTimes(1);
29
- expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2');
30
- });
31
-
32
- it('应该在每次调用后重置定时器', () => {
33
- const mockFn = jest.fn();
34
- const debouncedFn = debounce(mockFn, 100);
35
-
36
- // 第一次调用
37
- debouncedFn('arg1', 'arg2');
38
- jest.advanceTimersByTime(50);
39
-
40
- // 第二次调用(重置定时器)
41
- debouncedFn('arg1', 'arg2');
42
- jest.advanceTimersByTime(50);
43
-
44
- // 检查是否还没有调用
45
- expect(mockFn).not.toHaveBeenCalled();
46
-
47
- // 等待剩余时间
48
- jest.advanceTimersByTime(50);
49
-
50
- // 检查是否只调用了一次
51
- expect(mockFn).toHaveBeenCalledTimes(1);
52
- });
53
- });
54
-
55
- describe('throttle 函数', () => {
56
- it('应该在指定时间内只执行一次函数', () => {
57
- const mockFn = jest.fn();
58
- const throttledFn = throttle(mockFn, 100);
59
-
60
- // 第一次调用
61
- throttledFn('arg1', 'arg2');
62
- expect(mockFn).toHaveBeenCalledTimes(1);
63
-
64
- // 在限制时间内再次调用
65
- throttledFn('arg1', 'arg2');
66
- throttledFn('arg1', 'arg2');
67
- jest.advanceTimersByTime(50);
68
-
69
- // 检查是否只调用了一次
70
- expect(mockFn).toHaveBeenCalledTimes(1);
71
-
72
- // 等待限制时间后再次调用
73
- jest.advanceTimersByTime(50);
74
- throttledFn('arg1', 'arg2');
75
-
76
- // 检查是否调用了第二次
77
- expect(mockFn).toHaveBeenCalledTimes(2);
78
- });
79
-
80
- it('应该在限制时间后恢复执行', () => {
81
- const mockFn = jest.fn();
82
- const throttledFn = throttle(mockFn, 100);
83
-
84
- // 第一次调用
85
- throttledFn('arg1', 'arg2');
86
- expect(mockFn).toHaveBeenCalledTimes(1);
87
-
88
- // 等待限制时间
89
- jest.advanceTimersByTime(100);
90
-
91
- // 再次调用
92
- throttledFn('arg1', 'arg2');
93
- expect(mockFn).toHaveBeenCalledTimes(2);
94
- });
95
- });
96
-
97
- describe('delay 函数', () => {
98
- it('应该在指定时间后resolve', async () => {
99
- const start = Date.now();
100
-
101
- // 使用假定时器
102
- const delayPromise = delay(100);
103
-
104
- // 检查是否还没有resolve
105
- expect(delayPromise).toBeInstanceOf(Promise);
106
-
107
- // 快速检查是否有调用
108
- const result = await Promise.race([delayPromise, Promise.resolve('early')]);
109
- expect(result).toBe('early');
110
-
111
- // 使用真实定时器进行测试
112
- jest.useRealTimers();
113
- const realStart = Date.now();
114
- await delay(100);
115
- const elapsed = Date.now() - realStart;
116
-
117
- // 检查是否至少等待了指定时间
118
- expect(elapsed).toBeGreaterThanOrEqual(90);
119
- });
120
- });
121
- });
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2018",
4
- "module": "ESNext",
5
- "lib": ["ES2018", "dom", "dom.iterable"],
6
- "moduleResolution": "node",
7
- "strict": true,
8
- "esModuleInterop": true,
9
- "skipLibCheck": true,
10
- "forceConsistentCasingInFileNames": true,
11
- "declaration": true,
12
- "declarationMap": true,
13
- "sourceMap": true,
14
- "outDir": "./dist",
15
- "rootDir": "./src",
16
- "resolveJsonModule": true,
17
- "allowSyntheticDefaultImports": true
18
- },
19
- "include": ["src/**/*"],
20
- "exclude": ["node_modules", "dist", "**/*.spec.ts"]
21
- }
package/typedoc.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "$schema": "https://typedoc.org/schema.json",
3
- "entryPoints": ["src/index.ts"],
4
- "out": "docs",
5
- "name": "Function - TypeScript 工具函数库",
6
- "includeVersion": true,
7
- "readme": "README.md",
8
- "theme": "default",
9
- "sort": [
10
- "source-order"
11
- ],
12
- "navigationLinks": {
13
- "GitHub": "https://github.com/yourusername/function"
14
- }
15
- }