Pārlūkot izejas kodu

新增index.d.ts,修改导出方式

胡家华 5 gadi atpakaļ
vecāks
revīzija
516080177f
4 mainītis faili ar 73 papildinājumiem un 29 dzēšanām
  1. 23 10
      README.md
  2. 23 0
      index.d.ts
  3. 26 18
      index.js
  4. 1 1
      package.json

+ 23 - 10
README.md

1
+
1
 #### 主要记录一些常用方法
2
 #### 主要记录一些常用方法
2
 
3
 
4
+> tips: 第一个不成熟的库,谨慎使用!成熟后将迁移至github
5
+
3
 #### 安装 INSTALL
6
 #### 安装 INSTALL
4
-npm install mall_utils
7
+`npm install mall_utils`
5
 
8
 
9
+or
10
+
11
+`yarn add mall_utils_pro`
6
 #### 方法 METHODS
12
 #### 方法 METHODS
7
 
13
 
8
 方法名 Methods Name | 参数 Params | 描述 Description | 返回类型
14
 方法名 Methods Name | 参数 Params | 描述 Description | 返回类型
9
 ---|--- | --- | ---
15
 ---|--- | --- | ---
10
-**MathRand** | number | 获取随机数 | string
11
-**Base64** | string | 转为Base64的字符串 | string
12
-**MD5** | string | MD5加密 | string
16
+**MathRandom** | range: number | 获取随机数 | number
17
+**MathRand** | len: number | 获取len长度的随机数之和 | number
18
+**Base64** | data: any | 转为Base64的字符串 | string
19
+**MD5** | data: any | MD5加密 | string
13
 **debounce** | (function,wait) | 防抖 | void
20
 **debounce** | (function,wait) | 防抖 | void
14
 **throttle** | (function,delay) | 节流 | void
21
 **throttle** | (function,delay) | 节流 | void
15
-**keepTwoDecimal** | number | 四舍五入保留2位小数(若第二位小数为0,则保留一位小数) | void
16
-**keepTwoDecimalFull** | number | 四舍五入保留2位小数(不够位数,则用0替补) | void
17
-**showSaleNumText** | number | 显示销量的文本 | string
18
-**ScanSensitiveWords** | string | 查找敏感词 屏蔽为 **** | string
19
-**getDiffTime** | Date | 计算时差| 包含年月日时分秒的Object
22
+**keepTwoDecimal** | num: number | 四舍五入保留2位小数(若第二位小数为0,则保留一位小数) | void
23
+**keepTwoDecimalFull** | num: number | 四舍五入保留2位小数(不够位数,则用0替补) | void
24
+**showSaleNumText** | num: number | 显示销量的文本 | string
25
+**ScanSensitiveWords** | content: string | 查找敏感词 屏蔽为 **** | string
26
+**getDiffTime** | endDate: Date | 计算时差| 包含年月日时分秒的Object
20
 **RandomColor** |  | 随机颜色 | string
27
 **RandomColor** |  | 随机颜色 | string
21
- 
28
+ 
29
+#### Usage
30
+
31
+```
32
+import { showSaleNumText } from 'mall_utils_pro'
33
+<span>showSaleNumText(100000000)</span> // "10000.00万"
34
+```

+ 23 - 0
index.d.ts

1
+export interface diffTimeDate {
2
+    years: number
3
+    days: number
4
+    hours: number
5
+    min: number
6
+    sec: number
7
+    millisec: number
8
+}
9
+
10
+declare function MathRand(len: number): number {}
11
+declare function MathRandom(range: number): number {}
12
+declare function Base64(data: any): string {}
13
+declare function MD5(data: any): string {}
14
+declare function DESencrypt(data: any, key: any): strng {}
15
+declare function DESdecrypt(data: any, key: any): strng {}
16
+declare function debounce(fn: Function, wait: number): void {}
17
+declare function throttle(fn: Function, wait: number): void {}
18
+declare function keepTwoDecimal(num: number): number {}
19
+declare function keepTwoDecimalFull(num: number): number {}
20
+declare function showSaleNumText(num: number): string {}
21
+declare function ScanSensitiveWords(content: string): string {}
22
+declare function getDiffTime(endDate: Date): diffTimeDate {}
23
+declare function RandomColor(): string {} // rgb(0,0,0)

+ 26 - 18
index.js

3
 
3
 
4
 /**
4
 /**
5
  * 随机数
5
  * 随机数
6
- * @param len 随机数长度,默认为1
7
- * @returns {string}
6
+ * @param range 范围
7
+ * @returns {number}
8
  */
8
  */
9
-function MathRand(len = 1) {
9
+export const MathRandom = (range = 10) => {
10
+    return Math.floor(Math.random() * range);
11
+};
12
+/**
13
+ * 
14
+ * @param {number} len 随机数求和长度
15
+ */
16
+export const MathRand = (len = 1) => {
10
     let num = "";
17
     let num = "";
11
     for (let i = 0; i < len; i++) {
18
     for (let i = 0; i < len; i++) {
12
         num += Math.floor(Math.random() * 10);
19
         num += Math.floor(Math.random() * 10);
19
  * CryptoJS.enc.Utf8.parse  转为128bit的字符串
26
  * CryptoJS.enc.Utf8.parse  转为128bit的字符串
20
  * @returns {string}
27
  * @returns {string}
21
  */
28
  */
22
-function Base64(data) {
29
+export const Base64 = (data) => {
23
     return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data));
30
     return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data));
24
 };
31
 };
25
 
32
 
27
  * md5加密
34
  * md5加密
28
  * @returns {string}
35
  * @returns {string}
29
  */
36
  */
30
-function MD5(data) {
37
+export const MD5 = (data) => {
31
     return CryptoJS.MD5(data).toString();
38
     return CryptoJS.MD5(data).toString();
32
 };
39
 };
33
 
40
 
35
  * 3des加密
42
  * 3des加密
36
  * @returns {string}
43
  * @returns {string}
37
  */
44
  */
38
-function DESencrypt(data, key) {
45
+export const DESencrypt = (data, key) =>{
39
     return CryptoJS.TripleDES.encrypt(
46
     return CryptoJS.TripleDES.encrypt(
40
         data,
47
         data,
41
         CryptoJS.enc.Utf8.parse(key),
48
         CryptoJS.enc.Utf8.parse(key),
50
  * 3des解密
57
  * 3des解密
51
  * @returns {string}
58
  * @returns {string}
52
  */
59
  */
53
-function DESdecrypt(data, key) {
60
+export const DESdecrypt = (data, key) => {
54
     return CryptoJS.TripleDES.decrypt(
61
     return CryptoJS.TripleDES.decrypt(
55
         {
62
         {
56
             ciphertext: CryptoJS.enc.Base64.parse(data)
63
             ciphertext: CryptoJS.enc.Base64.parse(data)
63
     ).toString(CryptoJS.enc.Utf8);
70
     ).toString(CryptoJS.enc.Utf8);
64
 };
71
 };
65
 // 防抖
72
 // 防抖
66
-const debounce = (fn, wait) => {
73
+export const debounce = (fn, wait) => {
67
     var timeout = null;
74
     var timeout = null;
68
     return function() {
75
     return function() {
69
         if(timeout !== null) clearTimeout(timeout);
76
         if(timeout !== null) clearTimeout(timeout);
71
     }
78
     }
72
 }
79
 }
73
 // 节流
80
 // 节流
74
-const throttle = (func, delay) => {
81
+export const throttle = (fn, delay) => {
75
     var prev = Date.now();
82
     var prev = Date.now();
76
     return function() {
83
     return function() {
77
         var context = this;
84
         var context = this;
78
         var args = arguments;
85
         var args = arguments;
79
         var now = Date.now();
86
         var now = Date.now();
80
         if (now - prev >= delay) {
87
         if (now - prev >= delay) {
81
-            func.apply(context, args);
88
+            fn.apply(context, args);
82
             prev = Date.now();
89
             prev = Date.now();
83
         }
90
         }
84
     }
91
     }
85
 }
92
 }
86
 
93
 
87
 //四舍五入保留2位小数(若第二位小数为0,则保留一位小数)
94
 //四舍五入保留2位小数(若第二位小数为0,则保留一位小数)
88
-function keepTwoDecimal(num) {
95
+export const keepTwoDecimal = (num) => {
89
     var result = parseFloat(num);
96
     var result = parseFloat(num);
90
     if (isNaN(result)) {
97
     if (isNaN(result)) {
91
         console.log('传递参数错误,请检查!');
98
         console.log('传递参数错误,请检查!');
96
 }
103
 }
97
 
104
 
98
 //四舍五入保留2位小数(不够位数,则用0替补)
105
 //四舍五入保留2位小数(不够位数,则用0替补)
99
-function keepTwoDecimalFull(num) {
106
+export const keepTwoDecimalFull = (num) => {
100
     var result = parseFloat(num);
107
     var result = parseFloat(num);
101
     if (isNaN(result)) {
108
     if (isNaN(result)) {
102
         console.log('传递参数错误,请检查!');
109
         console.log('传递参数错误,请检查!');
116
 }
123
 }
117
 
124
 
118
 // 显示销量的文本
125
 // 显示销量的文本
119
-function showSaleNumText(num) {
126
+export const showSaleNumText = (num) => {
120
     let baseNum_wan = 10000
127
     let baseNum_wan = 10000
121
     let baseNum_sw = 100000
128
     let baseNum_sw = 100000
122
     let baseNum_bw = 1000000
129
     let baseNum_bw = 1000000
166
 }
173
 }
167
 
174
 
168
 // 查找敏感词
175
 // 查找敏感词
169
-const ScanSensitiveWords = (content = '') => {
176
+export const ScanSensitiveWords = (content = '') => {
170
     let _content = content
177
     let _content = content
171
     let scanner = new FastScanner(sensitiveDectionary)
178
     let scanner = new FastScanner(sensitiveDectionary)
172
     let offWords = scanner.search(_content)
179
     let offWords = scanner.search(_content)
175
 }
182
 }
176
 // 计算时差
183
 // 计算时差
177
 
184
 
178
-const getDiffTime = (endDate) =>{
185
+export const getDiffTime = (endDate) =>{
179
     let diff = (Date.parse(new Date(endDate)) - Date.parse(new Date())) / 1000;
186
     let diff = (Date.parse(new Date(endDate)) - Date.parse(new Date())) / 1000;
180
   
187
   
181
     if (diff <= 0) {
188
     if (diff <= 0) {
209
     }
216
     }
210
     timeLeft.sec = diff;
217
     timeLeft.sec = diff;
211
     return timeLeft;
218
     return timeLeft;
212
-  }
213
-const RandomColor = function randomColor(){
219
+}
220
+export const RandomColor = () => {
214
   var r = Math.floor(Math.random()*256);
221
   var r = Math.floor(Math.random()*256);
215
   var g = Math.floor(Math.random()*256);
222
   var g = Math.floor(Math.random()*256);
216
   var b = Math.floor(Math.random()*256);
223
   var b = Math.floor(Math.random()*256);
228
     DESencrypt, // 3des加密
235
     DESencrypt, // 3des加密
229
     MD5, // md5加密
236
     MD5, // md5加密
230
     Base64, // 转为Base64的字符串v
237
     Base64, // 转为Base64的字符串v
231
-    MathRand, // 获取随机数
238
+    MathRand, // 获取一定长度的随机数和
239
+    MathRandom, // 获取随机数
232
     getDiffTime, // 计算时差
240
     getDiffTime, // 计算时差
233
     RandomColor, // 随机颜色
241
     RandomColor, // 随机颜色
234
 }
242
 }

+ 1 - 1
package.json

1
 {
1
 {
2
   "name": "mall_utils_pro",
2
   "name": "mall_utils_pro",
3
-  "version": "1.0.5",
3
+  "version": "1.0.7",
4
   "description": "主要记录一些常用方法",
4
   "description": "主要记录一些常用方法",
5
   "main": "index.js",
5
   "main": "index.js",
6
   "scripts": {
6
   "scripts": {