@varlet/shared 2.18.1 → 2.18.2-alpha.1698939289602
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 +12 -1
- package/lib/index.js +40 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ declare const normalizeToArray: <T>(value: T | T[]) => T[];
|
|
|
3
3
|
declare const removeItem: (arr: Array<unknown>, item: unknown) => unknown[] | undefined;
|
|
4
4
|
declare const toggleItem: (arr: Array<unknown>, item: unknown) => void;
|
|
5
5
|
declare const find: <T>(arr: T[], callback: (item: T, index: number, array: T[]) => any, from?: 'start' | 'end') => [T, number] | [null, -1];
|
|
6
|
+
type ClassName = string | undefined | null;
|
|
7
|
+
type Classes = (ClassName | [any, ClassName, ClassName?])[];
|
|
8
|
+
declare const classes: (...classes: Classes) => any[];
|
|
6
9
|
|
|
7
10
|
declare const getGlobalThis: () => typeof globalThis;
|
|
8
11
|
declare const requestAnimationFrame: (fn: FrameRequestCallback) => number;
|
|
@@ -19,6 +22,7 @@ declare const getScrollLeft: (element: Element | Window) => number;
|
|
|
19
22
|
|
|
20
23
|
declare const debounce: (fn: any, delay?: number) => (this: unknown, ...args: any[]) => void;
|
|
21
24
|
declare const throttle: (fn: any, delay?: number) => (() => void);
|
|
25
|
+
declare function call<P extends any[], R>(fn?: ((...arg: P) => R) | ((...arg: P) => R)[] | null, ...args: P): R | R[] | undefined;
|
|
22
26
|
|
|
23
27
|
declare const isString: (val: unknown) => val is string;
|
|
24
28
|
declare const isBoolean: (val: unknown) => val is boolean;
|
|
@@ -32,6 +36,7 @@ declare const isEmpty: (val: unknown) => boolean;
|
|
|
32
36
|
declare const isWindow: (val: unknown) => val is Window;
|
|
33
37
|
declare const supportTouch: () => boolean;
|
|
34
38
|
declare const inBrowser: () => boolean;
|
|
39
|
+
declare const hasOwn: (val: object, key: string | symbol) => key is never;
|
|
35
40
|
|
|
36
41
|
declare const toNumber: (val: number | string | boolean | undefined | null) => number;
|
|
37
42
|
declare const clamp: (num: number, min: number, max: number) => number;
|
|
@@ -40,5 +45,11 @@ declare const clampArrayRange: (index: number, arr: Array<unknown>) => number;
|
|
|
40
45
|
declare const bigCamelize: (s: string) => string;
|
|
41
46
|
declare const camelize: (s: string) => string;
|
|
42
47
|
declare const kebabCase: (s: string) => string;
|
|
48
|
+
type BEM<S extends string | undefined, N extends string, NC extends string> = S extends undefined ? NC : S extends `$--${infer CM}` ? `${N}--${CM}` : S extends `--${infer M}` ? `${NC}--${M}` : `${NC}__${S}`;
|
|
49
|
+
declare function createNamespaceFn<N extends string>(namespace: N): <C extends string>(name: C) => {
|
|
50
|
+
name: string;
|
|
51
|
+
n: <S extends string | undefined = undefined>(suffix?: S | undefined) => BEM<S, N, `${N}-${C}`>;
|
|
52
|
+
classes: (...classes: Classes) => any[];
|
|
53
|
+
};
|
|
43
54
|
|
|
44
|
-
export { bigCamelize, camelize, cancelAnimationFrame, clamp, clampArrayRange, debounce, doubleRaf, find, getGlobalThis, getRect, getScrollLeft, getScrollTop, getStyle, inBrowser, inViewport, isArray, isBoolean, isEmpty, isFunction, isNumber, isObject, isPlainObject, isString, isURL, isWindow, kebabCase, normalizeToArray, preventDefault, raf, removeItem, requestAnimationFrame, supportTouch, throttle, toDataURL, toNumber, toggleItem, uniq };
|
|
55
|
+
export { BEM, ClassName, Classes, bigCamelize, call, camelize, cancelAnimationFrame, clamp, clampArrayRange, classes, createNamespaceFn, debounce, doubleRaf, find, getGlobalThis, getRect, getScrollLeft, getScrollTop, getStyle, hasOwn, inBrowser, inViewport, isArray, isBoolean, isEmpty, isFunction, isNumber, isObject, isPlainObject, isString, isURL, isWindow, kebabCase, normalizeToArray, preventDefault, raf, removeItem, requestAnimationFrame, supportTouch, throttle, toDataURL, toNumber, toggleItem, uniq };
|
package/lib/index.js
CHANGED
|
@@ -36,6 +36,8 @@ var isEmpty = (val) => val === void 0 || val === null || val === "" || Array.isA
|
|
|
36
36
|
var isWindow = (val) => val === window;
|
|
37
37
|
var supportTouch = () => inBrowser() && "ontouchstart" in window;
|
|
38
38
|
var inBrowser = () => typeof window !== "undefined";
|
|
39
|
+
var { hasOwnProperty } = Object.prototype;
|
|
40
|
+
var hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
39
41
|
|
|
40
42
|
// src/array.ts
|
|
41
43
|
var uniq = (arr) => [...new Set(arr)];
|
|
@@ -62,6 +64,13 @@ var find = (arr, callback, from = "start") => {
|
|
|
62
64
|
}
|
|
63
65
|
return [null, -1];
|
|
64
66
|
};
|
|
67
|
+
var classes = (...classes2) => classes2.map((className) => {
|
|
68
|
+
if (isArray(className)) {
|
|
69
|
+
const [condition, truthy, falsy = null] = className;
|
|
70
|
+
return condition ? truthy : falsy;
|
|
71
|
+
}
|
|
72
|
+
return className;
|
|
73
|
+
});
|
|
65
74
|
|
|
66
75
|
// src/elements.ts
|
|
67
76
|
var getGlobalThis = () => {
|
|
@@ -173,6 +182,14 @@ var throttle = (fn, delay = 200) => {
|
|
|
173
182
|
}
|
|
174
183
|
};
|
|
175
184
|
};
|
|
185
|
+
function call(fn, ...args) {
|
|
186
|
+
if (isArray(fn)) {
|
|
187
|
+
return fn.map((f) => f(...args));
|
|
188
|
+
}
|
|
189
|
+
if (fn) {
|
|
190
|
+
return fn(...args);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
176
193
|
|
|
177
194
|
// src/number.ts
|
|
178
195
|
var toNumber = (val) => {
|
|
@@ -197,12 +214,34 @@ var kebabCase = (s) => {
|
|
|
197
214
|
const ret = s.replace(/([A-Z])/g, " $1").trim();
|
|
198
215
|
return ret.split(" ").join("-").toLowerCase();
|
|
199
216
|
};
|
|
217
|
+
function createNamespaceFn(namespace) {
|
|
218
|
+
return (name) => {
|
|
219
|
+
const componentName = `${namespace}-${name}`;
|
|
220
|
+
const createBEM = (suffix) => {
|
|
221
|
+
if (!suffix) {
|
|
222
|
+
return componentName;
|
|
223
|
+
}
|
|
224
|
+
if (suffix[0] === "$") {
|
|
225
|
+
return suffix.replace("$", namespace);
|
|
226
|
+
}
|
|
227
|
+
return suffix.startsWith("--") ? `${componentName}${suffix}` : `${componentName}__${suffix}`;
|
|
228
|
+
};
|
|
229
|
+
return {
|
|
230
|
+
name: bigCamelize(componentName),
|
|
231
|
+
n: createBEM,
|
|
232
|
+
classes
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
}
|
|
200
236
|
export {
|
|
201
237
|
bigCamelize,
|
|
238
|
+
call,
|
|
202
239
|
camelize,
|
|
203
240
|
cancelAnimationFrame,
|
|
204
241
|
clamp,
|
|
205
242
|
clampArrayRange,
|
|
243
|
+
classes,
|
|
244
|
+
createNamespaceFn,
|
|
206
245
|
debounce,
|
|
207
246
|
doubleRaf,
|
|
208
247
|
find,
|
|
@@ -211,6 +250,7 @@ export {
|
|
|
211
250
|
getScrollLeft,
|
|
212
251
|
getScrollTop,
|
|
213
252
|
getStyle,
|
|
253
|
+
hasOwn,
|
|
214
254
|
inBrowser,
|
|
215
255
|
inViewport,
|
|
216
256
|
isArray,
|