소스 검색

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

胡家华 5 년 전
부모
커밋
516080177f
4개의 변경된 파일73개의 추가작업 그리고 29개의 파일을 삭제
  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,21 +1,34 @@
1
+
1 2
 #### 主要记录一些常用方法
2 3
 
4
+> tips: 第一个不成熟的库,谨慎使用!成熟后将迁移至github
5
+
3 6
 #### 安装 INSTALL
4
-npm install mall_utils
7
+`npm install mall_utils`
5 8
 
9
+or
10
+
11
+`yarn add mall_utils_pro`
6 12
 #### 方法 METHODS
7 13
 
8 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 20
 **debounce** | (function,wait) | 防抖 | void
14 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 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

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

+ 1 - 1
package.json

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