@varlet/shared 2.16.2 → 2.16.3-alpha.1694361535255
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/lib/index.d.ts +34 -32
- package/lib/index.js +172 -118
- package/package.json +4 -4
package/lib/index.d.ts
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
declare const isString: (val: unknown) => val is string;
|
|
2
|
+
declare const isBoolean: (val: unknown) => val is boolean;
|
|
3
|
+
declare const isNumber: (val: unknown) => val is number;
|
|
4
|
+
declare const isPlainObject: (val: unknown) => val is Record<string, any>;
|
|
5
|
+
declare const isObject: (val: unknown) => val is Record<string, any>;
|
|
6
|
+
declare const isFunction: (val: unknown) => val is Function;
|
|
7
|
+
declare const isArray: (val: unknown) => val is any[];
|
|
8
|
+
declare const isURL: (val: string | undefined | null) => boolean;
|
|
9
|
+
declare const isEmpty: (val: unknown) => boolean;
|
|
10
|
+
declare const isWindow: (val: unknown) => val is Window;
|
|
11
|
+
declare const supportTouch: () => boolean;
|
|
12
|
+
declare const toNumber: (val: number | string | boolean | undefined | null) => number;
|
|
13
|
+
declare const removeItem: (arr: Array<unknown>, item: unknown) => unknown[] | undefined;
|
|
14
|
+
declare const toggleItem: (arr: Array<unknown>, item: unknown) => void;
|
|
15
|
+
declare const throttle: (method: any, mustRunDelay?: number) => (() => void);
|
|
16
|
+
declare const inBrowser: () => boolean;
|
|
17
|
+
declare const uniq: (arr: Array<any>) => any[];
|
|
18
|
+
declare const bigCamelize: (s: string) => string;
|
|
19
|
+
declare const camelize: (s: string) => string;
|
|
20
|
+
declare const kebabCase: (s: string) => string;
|
|
21
|
+
declare const find: <T>(arr: T[], callback: (item: T, index: number, array: T[]) => any, from?: 'start' | 'end') => [T, number] | [null, -1];
|
|
22
|
+
declare const normalizeToArray: <T>(value: T | T[]) => T[];
|
|
23
|
+
declare const clamp: (num: number, min: number, max: number) => number;
|
|
24
|
+
declare const clampArrayRange: (index: number, arr: Array<unknown>) => number;
|
|
25
|
+
declare const getGlobalThis: () => typeof globalThis;
|
|
26
|
+
declare const requestAnimationFrame: (fn: FrameRequestCallback) => number;
|
|
27
|
+
declare const cancelAnimationFrame: (handle: number) => void;
|
|
28
|
+
declare const raf: () => Promise<unknown>;
|
|
29
|
+
declare const doubleRaf: () => Promise<unknown>;
|
|
30
|
+
declare const getStyle: (element: Element) => CSSStyleDeclaration;
|
|
31
|
+
declare const getRect: (element: Element | Window) => DOMRect;
|
|
32
|
+
declare const inViewport: (element: HTMLElement) => boolean;
|
|
33
|
+
|
|
34
|
+
export { bigCamelize, camelize, cancelAnimationFrame, clamp, clampArrayRange, doubleRaf, find, getGlobalThis, getRect, getStyle, inBrowser, inViewport, isArray, isBoolean, isEmpty, isFunction, isNumber, isObject, isPlainObject, isString, isURL, isWindow, kebabCase, normalizeToArray, raf, removeItem, requestAnimationFrame, supportTouch, throttle, toNumber, toggleItem, uniq };
|
package/lib/index.js
CHANGED
|
@@ -1,137 +1,191 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (
|
|
11
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
16
|
}
|
|
13
|
-
|
|
17
|
+
return a;
|
|
14
18
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var isString = (val) => typeof val === "string";
|
|
23
|
+
var isBoolean = (val) => typeof val === "boolean";
|
|
24
|
+
var isNumber = (val) => typeof val === "number";
|
|
25
|
+
var isPlainObject = (val) => Object.prototype.toString.call(val) === "[object Object]";
|
|
26
|
+
var isObject = (val) => typeof val === "object" && val !== null;
|
|
27
|
+
var isFunction = (val) => typeof val === "function";
|
|
28
|
+
var isArray = (val) => Array.isArray(val);
|
|
29
|
+
var isURL = (val) => {
|
|
30
|
+
if (!val) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return /^(http)|(\.*\/)/.test(val);
|
|
34
|
+
};
|
|
35
|
+
var isEmpty = (val) => val === void 0 || val === null || val === "" || Array.isArray(val) && !val.length;
|
|
36
|
+
var isWindow = (val) => val === window;
|
|
37
|
+
var supportTouch = () => inBrowser() && "ontouchstart" in window;
|
|
38
|
+
var toNumber = (val) => {
|
|
39
|
+
if (val == null)
|
|
40
|
+
return 0;
|
|
41
|
+
if (isString(val)) {
|
|
42
|
+
val = parseFloat(val);
|
|
43
|
+
val = Number.isNaN(val) ? 0 : val;
|
|
28
44
|
return val;
|
|
45
|
+
}
|
|
46
|
+
if (isBoolean(val))
|
|
47
|
+
return Number(val);
|
|
48
|
+
return val;
|
|
29
49
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
50
|
+
var removeItem = (arr, item) => {
|
|
51
|
+
if (arr.length) {
|
|
52
|
+
const index = arr.indexOf(item);
|
|
53
|
+
if (index > -1) {
|
|
54
|
+
return arr.splice(index, 1);
|
|
36
55
|
}
|
|
56
|
+
}
|
|
37
57
|
};
|
|
38
|
-
|
|
39
|
-
|
|
58
|
+
var toggleItem = (arr, item) => {
|
|
59
|
+
arr.includes(item) ? removeItem(arr, item) : arr.push(item);
|
|
40
60
|
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
};
|
|
61
|
+
var throttle = (method, mustRunDelay = 200) => {
|
|
62
|
+
let timer;
|
|
63
|
+
let start = 0;
|
|
64
|
+
return function loop(...args) {
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
const elapsed = now - start;
|
|
67
|
+
if (!start) {
|
|
68
|
+
start = now;
|
|
69
|
+
}
|
|
70
|
+
if (timer) {
|
|
71
|
+
window.clearTimeout(timer);
|
|
72
|
+
}
|
|
73
|
+
if (elapsed >= mustRunDelay) {
|
|
74
|
+
method.apply(this, args);
|
|
75
|
+
start = now;
|
|
76
|
+
} else {
|
|
77
|
+
timer = window.setTimeout(() => {
|
|
78
|
+
loop.apply(this, args);
|
|
79
|
+
}, mustRunDelay - elapsed);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
63
82
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
var inBrowser = () => typeof window !== "undefined";
|
|
84
|
+
var uniq = (arr) => [...new Set(arr)];
|
|
85
|
+
var bigCamelize = (s) => camelize(s).replace(s.charAt(0), s.charAt(0).toUpperCase());
|
|
86
|
+
var camelize = (s) => s.replace(/-(\w)/g, (_, p) => p.toUpperCase());
|
|
87
|
+
var kebabCase = (s) => {
|
|
88
|
+
const ret = s.replace(/([A-Z])/g, " $1").trim();
|
|
89
|
+
return ret.split(" ").join("-").toLowerCase();
|
|
71
90
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
from === 'start' ? i++ : i--;
|
|
91
|
+
var find = (arr, callback, from = "start") => {
|
|
92
|
+
let i = from === "start" ? 0 : arr.length - 1;
|
|
93
|
+
while (arr.length > 0 && i >= 0 && i <= arr.length - 1) {
|
|
94
|
+
const flag = callback(arr[i], i, arr);
|
|
95
|
+
if (flag) {
|
|
96
|
+
return [arr[i], i];
|
|
80
97
|
}
|
|
81
|
-
|
|
98
|
+
from === "start" ? i++ : i--;
|
|
99
|
+
}
|
|
100
|
+
return [null, -1];
|
|
82
101
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
102
|
+
var normalizeToArray = (value) => isArray(value) ? value : [value];
|
|
103
|
+
var clamp = (num, min, max) => Math.min(max, Math.max(min, num));
|
|
104
|
+
var clampArrayRange = (index, arr) => clamp(index, 0, arr.length - 1);
|
|
105
|
+
var getGlobalThis = () => {
|
|
106
|
+
if (typeof globalThis !== "undefined") {
|
|
107
|
+
return globalThis;
|
|
108
|
+
}
|
|
109
|
+
if (inBrowser()) {
|
|
110
|
+
return window;
|
|
111
|
+
}
|
|
112
|
+
return typeof global !== "undefined" ? global : self;
|
|
94
113
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
114
|
+
var requestAnimationFrame = (fn) => {
|
|
115
|
+
const globalThis2 = getGlobalThis();
|
|
116
|
+
return globalThis2.requestAnimationFrame ? globalThis2.requestAnimationFrame(fn) : globalThis2.setTimeout(fn);
|
|
98
117
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
118
|
+
var cancelAnimationFrame = (handle) => {
|
|
119
|
+
const globalThis2 = getGlobalThis();
|
|
120
|
+
globalThis2.cancelAnimationFrame ? globalThis2.cancelAnimationFrame(handle) : globalThis2.clearTimeout(handle);
|
|
102
121
|
};
|
|
103
|
-
|
|
122
|
+
var raf = () => new Promise((resolve) => {
|
|
123
|
+
requestAnimationFrame(resolve);
|
|
124
|
+
});
|
|
125
|
+
var doubleRaf = () => new Promise((resolve) => {
|
|
126
|
+
requestAnimationFrame(() => {
|
|
104
127
|
requestAnimationFrame(resolve);
|
|
128
|
+
});
|
|
105
129
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
130
|
+
var getStyle = (element) => window.getComputedStyle(element);
|
|
131
|
+
var getRect = (element) => {
|
|
132
|
+
if (isWindow(element)) {
|
|
133
|
+
const width = element.innerWidth;
|
|
134
|
+
const height = element.innerHeight;
|
|
135
|
+
const rect = {
|
|
136
|
+
x: 0,
|
|
137
|
+
y: 0,
|
|
138
|
+
top: 0,
|
|
139
|
+
left: 0,
|
|
140
|
+
right: width,
|
|
141
|
+
bottom: height,
|
|
142
|
+
width,
|
|
143
|
+
height
|
|
144
|
+
};
|
|
145
|
+
return __spreadProps(__spreadValues({}, rect), {
|
|
146
|
+
toJSON: () => rect
|
|
109
147
|
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
y: 0,
|
|
120
|
-
top: 0,
|
|
121
|
-
left: 0,
|
|
122
|
-
right: width,
|
|
123
|
-
bottom: height,
|
|
124
|
-
width,
|
|
125
|
-
height,
|
|
126
|
-
};
|
|
127
|
-
return Object.assign(Object.assign({}, rect), { toJSON: () => rect });
|
|
128
|
-
}
|
|
129
|
-
return element.getBoundingClientRect();
|
|
148
|
+
}
|
|
149
|
+
return element.getBoundingClientRect();
|
|
150
|
+
};
|
|
151
|
+
var inViewport = (element) => {
|
|
152
|
+
const { top, bottom, left, right } = getRect(element);
|
|
153
|
+
const { width, height } = getRect(window);
|
|
154
|
+
const xInViewport = left <= width && right >= 0;
|
|
155
|
+
const yInViewport = top <= height && bottom >= 0;
|
|
156
|
+
return xInViewport && yInViewport;
|
|
130
157
|
};
|
|
131
|
-
export
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
158
|
+
export {
|
|
159
|
+
bigCamelize,
|
|
160
|
+
camelize,
|
|
161
|
+
cancelAnimationFrame,
|
|
162
|
+
clamp,
|
|
163
|
+
clampArrayRange,
|
|
164
|
+
doubleRaf,
|
|
165
|
+
find,
|
|
166
|
+
getGlobalThis,
|
|
167
|
+
getRect,
|
|
168
|
+
getStyle,
|
|
169
|
+
inBrowser,
|
|
170
|
+
inViewport,
|
|
171
|
+
isArray,
|
|
172
|
+
isBoolean,
|
|
173
|
+
isEmpty,
|
|
174
|
+
isFunction,
|
|
175
|
+
isNumber,
|
|
176
|
+
isObject,
|
|
177
|
+
isPlainObject,
|
|
178
|
+
isString,
|
|
179
|
+
isURL,
|
|
180
|
+
isWindow,
|
|
181
|
+
kebabCase,
|
|
182
|
+
normalizeToArray,
|
|
183
|
+
raf,
|
|
184
|
+
removeItem,
|
|
185
|
+
requestAnimationFrame,
|
|
186
|
+
supportTouch,
|
|
187
|
+
throttle,
|
|
188
|
+
toNumber,
|
|
189
|
+
toggleItem,
|
|
190
|
+
uniq
|
|
137
191
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@varlet/shared",
|
|
3
|
-
"version": "2.16.
|
|
3
|
+
"version": "2.16.3-alpha.1694361535255",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^18.7.18",
|
|
28
28
|
"typescript": "^5.1.5",
|
|
29
|
-
"
|
|
29
|
+
"tsup": "7.2.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"dev": "
|
|
33
|
-
"build": "
|
|
32
|
+
"dev": "tsup src/index.ts --format esm --out-dir=lib --watch --dts",
|
|
33
|
+
"build": "tsup src/index.ts --format esm --out-dir=lib --dts --clean"
|
|
34
34
|
}
|
|
35
35
|
}
|