@daysnap/utils 0.0.52 → 0.0.54

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/es/clone.d.ts CHANGED
@@ -2,4 +2,4 @@
2
2
  * 深拷贝
3
3
  * @param source 需要转换的值
4
4
  * */
5
- export declare function clone<T>(source: T): T;
5
+ export declare function clone<T extends Record<string, any>>(source: T): T;
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * 压缩图片
3
3
  */
4
- export declare function compressImage(image: CanvasImageSource): HTMLCanvasElement;
4
+ export declare function compressImage(image: HTMLImageElement): HTMLCanvasElement;
package/es/index.d.ts CHANGED
@@ -34,6 +34,7 @@ export * from './insertStyle';
34
34
  export * from './isAndroid';
35
35
  export * from './isArray';
36
36
  export * from './isBoolean';
37
+ export * from './isDate';
37
38
  export * from './isEmail';
38
39
  export * from './isEmpty';
39
40
  export * from './isEmptyObject';
@@ -57,6 +58,7 @@ export * from './isUndefined';
57
58
  export * from './isWeixin';
58
59
  export * from './isWindow';
59
60
  export * from './kebabCase';
61
+ export * from './mousewheel';
60
62
  export * from './normalizePath';
61
63
  export * from './omit';
62
64
  export * from './padding';
@@ -69,6 +71,7 @@ export * from './pascalCase';
69
71
  export * from './pick';
70
72
  export * from './replaceCrlf';
71
73
  export * from './reserve';
74
+ export * from './rgbToHex';
72
75
  export * from './round';
73
76
  export * from './sleep';
74
77
  export * from './storage';
package/es/index.js CHANGED
@@ -35,6 +35,7 @@ export * from './insertStyle';
35
35
  export * from './isAndroid';
36
36
  export * from './isArray';
37
37
  export * from './isBoolean';
38
+ export * from './isDate';
38
39
  export * from './isEmail';
39
40
  export * from './isEmpty';
40
41
  export * from './isEmptyObject';
@@ -58,6 +59,7 @@ export * from './isUndefined';
58
59
  export * from './isWeixin';
59
60
  export * from './isWindow';
60
61
  export * from './kebabCase';
62
+ export * from './mousewheel';
61
63
  export * from './normalizePath';
62
64
  export * from './omit';
63
65
  export * from './padding';
@@ -70,6 +72,7 @@ export * from './pascalCase';
70
72
  export * from './pick';
71
73
  export * from './replaceCrlf';
72
74
  export * from './reserve';
75
+ export * from './rgbToHex';
73
76
  export * from './round';
74
77
  export * from './sleep';
75
78
  export * from './storage';
package/es/isDate.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 判断是否日期 Date
3
+ */
4
+ export declare function isDate(val: unknown): val is Date;
package/es/isDate.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 判断是否日期 Date
3
+ */
4
+ export function isDate(val) {
5
+ return val instanceof Date;
6
+ }
@@ -0,0 +1,14 @@
1
+ interface MousewheelScrolling {
2
+ (result: {
3
+ direction: 'down' | 'up';
4
+ delta: number;
5
+ }): void;
6
+ }
7
+ interface MousewheelOptions {
8
+ scrollingSpeed?: number;
9
+ scrollDelay?: number;
10
+ }
11
+ export declare function mousewheel(scrolling: MousewheelScrolling, options?: MousewheelOptions): {
12
+ listener: (e: any) => false | undefined;
13
+ };
14
+ export {};
@@ -0,0 +1,61 @@
1
+ function getAverage(elements, number) {
2
+ let sum = 0;
3
+ //taking `number` elements from the end to make the average, if there are not enought, 1
4
+ const lastElements = elements.slice(Math.max(elements.length - number, 1));
5
+ for (let i = 0; i < lastElements.length; i++) {
6
+ sum = sum + lastElements[i];
7
+ }
8
+ return Math.ceil(sum / number);
9
+ }
10
+ export function mousewheel(scrolling, options) {
11
+ const { scrollingSpeed = 700, scrollDelay = 50 } = Object.assign({}, options);
12
+ let scrollings = [];
13
+ let prevTime = Date.now();
14
+ let lastAnimation = 0;
15
+ function isMoving() {
16
+ const timeNow = new Date().getTime();
17
+ // Cancel scroll if currently animating or within quiet period
18
+ if (timeNow - lastAnimation < scrollDelay + scrollingSpeed) {
19
+ return true;
20
+ }
21
+ return false;
22
+ }
23
+ const listener = (e) => {
24
+ const curTime = Date.now();
25
+ const delta = e.wheelDelta || -e.deltaY || -e.detail;
26
+ // const delta = Math.max(-1, Math.min(1, value))
27
+ const horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined';
28
+ const isScrollingVertically = Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta) ||
29
+ Math.abs(e.deltaX) < Math.abs(e.deltaY) ||
30
+ !horizontalDetection;
31
+ //Limiting the array to 150 (lets not waste memory!)
32
+ if (scrollings.length > 149) {
33
+ scrollings.shift();
34
+ }
35
+ //keeping record of the previous scrollings
36
+ scrollings.push(Math.abs(delta));
37
+ //time difference between the last scroll and the current one
38
+ const timeDiff = curTime - prevTime;
39
+ prevTime = curTime;
40
+ //haven't they scrolled in a while?
41
+ //(enough to be consider a different scrolling action to scroll another section)
42
+ if (timeDiff > 200) {
43
+ //emptying the array, we dont care about old scrollings for our averages
44
+ scrollings = [];
45
+ }
46
+ if (!isMoving()) {
47
+ const averageEnd = getAverage(scrollings, 10);
48
+ const averageMiddle = getAverage(scrollings, 70);
49
+ const isAccelerating = averageEnd >= averageMiddle;
50
+ if (isAccelerating && isScrollingVertically) {
51
+ scrolling({
52
+ direction: delta < 0 ? 'up' : 'down',
53
+ delta,
54
+ });
55
+ lastAnimation = Date.now();
56
+ }
57
+ return false;
58
+ }
59
+ };
60
+ return { listener };
61
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 说明 rgb 转 十六进制值
3
+ */
4
+ export declare function rgbToHex(r: string | number, g: string | number, b: string | number): string;
package/es/rgbToHex.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 说明 rgb 转 十六进制值
3
+ */
4
+ export function rgbToHex(r, g, b) {
5
+ return ('#' +
6
+ [r, g, b]
7
+ .map((x) => {
8
+ const hex = x.toString(16);
9
+ return hex.length === 1 ? '0' + hex : hex;
10
+ })
11
+ .join(''));
12
+ }
package/lib/clone.d.ts CHANGED
@@ -2,4 +2,4 @@
2
2
  * 深拷贝
3
3
  * @param source 需要转换的值
4
4
  * */
5
- export declare function clone<T>(source: T): T;
5
+ export declare function clone<T extends Record<string, any>>(source: T): T;
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * 压缩图片
3
3
  */
4
- export declare function compressImage(image: CanvasImageSource): HTMLCanvasElement;
4
+ export declare function compressImage(image: HTMLImageElement): HTMLCanvasElement;
package/lib/index.d.ts CHANGED
@@ -34,6 +34,7 @@ export * from './insertStyle';
34
34
  export * from './isAndroid';
35
35
  export * from './isArray';
36
36
  export * from './isBoolean';
37
+ export * from './isDate';
37
38
  export * from './isEmail';
38
39
  export * from './isEmpty';
39
40
  export * from './isEmptyObject';
@@ -57,6 +58,7 @@ export * from './isUndefined';
57
58
  export * from './isWeixin';
58
59
  export * from './isWindow';
59
60
  export * from './kebabCase';
61
+ export * from './mousewheel';
60
62
  export * from './normalizePath';
61
63
  export * from './omit';
62
64
  export * from './padding';
@@ -69,6 +71,7 @@ export * from './pascalCase';
69
71
  export * from './pick';
70
72
  export * from './replaceCrlf';
71
73
  export * from './reserve';
74
+ export * from './rgbToHex';
72
75
  export * from './round';
73
76
  export * from './sleep';
74
77
  export * from './storage';
package/lib/index.js CHANGED
@@ -51,6 +51,7 @@ __exportStar(require("./insertStyle"), exports);
51
51
  __exportStar(require("./isAndroid"), exports);
52
52
  __exportStar(require("./isArray"), exports);
53
53
  __exportStar(require("./isBoolean"), exports);
54
+ __exportStar(require("./isDate"), exports);
54
55
  __exportStar(require("./isEmail"), exports);
55
56
  __exportStar(require("./isEmpty"), exports);
56
57
  __exportStar(require("./isEmptyObject"), exports);
@@ -74,6 +75,7 @@ __exportStar(require("./isUndefined"), exports);
74
75
  __exportStar(require("./isWeixin"), exports);
75
76
  __exportStar(require("./isWindow"), exports);
76
77
  __exportStar(require("./kebabCase"), exports);
78
+ __exportStar(require("./mousewheel"), exports);
77
79
  __exportStar(require("./normalizePath"), exports);
78
80
  __exportStar(require("./omit"), exports);
79
81
  __exportStar(require("./padding"), exports);
@@ -86,6 +88,7 @@ __exportStar(require("./pascalCase"), exports);
86
88
  __exportStar(require("./pick"), exports);
87
89
  __exportStar(require("./replaceCrlf"), exports);
88
90
  __exportStar(require("./reserve"), exports);
91
+ __exportStar(require("./rgbToHex"), exports);
89
92
  __exportStar(require("./round"), exports);
90
93
  __exportStar(require("./sleep"), exports);
91
94
  __exportStar(require("./storage"), exports);
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 判断是否日期 Date
3
+ */
4
+ export declare function isDate(val: unknown): val is Date;
package/lib/isDate.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDate = void 0;
4
+ /**
5
+ * 判断是否日期 Date
6
+ */
7
+ function isDate(val) {
8
+ return val instanceof Date;
9
+ }
10
+ exports.isDate = isDate;
@@ -0,0 +1,14 @@
1
+ interface MousewheelScrolling {
2
+ (result: {
3
+ direction: 'down' | 'up';
4
+ delta: number;
5
+ }): void;
6
+ }
7
+ interface MousewheelOptions {
8
+ scrollingSpeed?: number;
9
+ scrollDelay?: number;
10
+ }
11
+ export declare function mousewheel(scrolling: MousewheelScrolling, options?: MousewheelOptions): {
12
+ listener: (e: any) => false | undefined;
13
+ };
14
+ export {};
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mousewheel = void 0;
4
+ function getAverage(elements, number) {
5
+ let sum = 0;
6
+ //taking `number` elements from the end to make the average, if there are not enought, 1
7
+ const lastElements = elements.slice(Math.max(elements.length - number, 1));
8
+ for (let i = 0; i < lastElements.length; i++) {
9
+ sum = sum + lastElements[i];
10
+ }
11
+ return Math.ceil(sum / number);
12
+ }
13
+ function mousewheel(scrolling, options) {
14
+ const { scrollingSpeed = 700, scrollDelay = 50 } = Object.assign({}, options);
15
+ let scrollings = [];
16
+ let prevTime = Date.now();
17
+ let lastAnimation = 0;
18
+ function isMoving() {
19
+ const timeNow = new Date().getTime();
20
+ // Cancel scroll if currently animating or within quiet period
21
+ if (timeNow - lastAnimation < scrollDelay + scrollingSpeed) {
22
+ return true;
23
+ }
24
+ return false;
25
+ }
26
+ const listener = (e) => {
27
+ const curTime = Date.now();
28
+ const delta = e.wheelDelta || -e.deltaY || -e.detail;
29
+ // const delta = Math.max(-1, Math.min(1, value))
30
+ const horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined';
31
+ const isScrollingVertically = Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta) ||
32
+ Math.abs(e.deltaX) < Math.abs(e.deltaY) ||
33
+ !horizontalDetection;
34
+ //Limiting the array to 150 (lets not waste memory!)
35
+ if (scrollings.length > 149) {
36
+ scrollings.shift();
37
+ }
38
+ //keeping record of the previous scrollings
39
+ scrollings.push(Math.abs(delta));
40
+ //time difference between the last scroll and the current one
41
+ const timeDiff = curTime - prevTime;
42
+ prevTime = curTime;
43
+ //haven't they scrolled in a while?
44
+ //(enough to be consider a different scrolling action to scroll another section)
45
+ if (timeDiff > 200) {
46
+ //emptying the array, we dont care about old scrollings for our averages
47
+ scrollings = [];
48
+ }
49
+ if (!isMoving()) {
50
+ const averageEnd = getAverage(scrollings, 10);
51
+ const averageMiddle = getAverage(scrollings, 70);
52
+ const isAccelerating = averageEnd >= averageMiddle;
53
+ if (isAccelerating && isScrollingVertically) {
54
+ scrolling({
55
+ direction: delta < 0 ? 'up' : 'down',
56
+ delta,
57
+ });
58
+ lastAnimation = Date.now();
59
+ }
60
+ return false;
61
+ }
62
+ };
63
+ return { listener };
64
+ }
65
+ exports.mousewheel = mousewheel;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 说明 rgb 转 十六进制值
3
+ */
4
+ export declare function rgbToHex(r: string | number, g: string | number, b: string | number): string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rgbToHex = void 0;
4
+ /**
5
+ * 说明 rgb 转 十六进制值
6
+ */
7
+ function rgbToHex(r, g, b) {
8
+ return ('#' +
9
+ [r, g, b]
10
+ .map((x) => {
11
+ const hex = x.toString(16);
12
+ return hex.length === 1 ? '0' + hex : hex;
13
+ })
14
+ .join(''));
15
+ }
16
+ exports.rgbToHex = rgbToHex;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daysnap/utils",
3
- "version": "0.0.52",
3
+ "version": "0.0.54",
4
4
  "description": "通用的工具库",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",