@varlet/shared 2.18.2-alpha.1698728560863 → 2.18.2

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 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;
@@ -41,5 +45,11 @@ declare const clampArrayRange: (index: number, arr: Array<unknown>) => number;
41
45
  declare const bigCamelize: (s: string) => string;
42
46
  declare const camelize: (s: string) => string;
43
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
+ };
44
54
 
45
- export { bigCamelize, camelize, cancelAnimationFrame, clamp, clampArrayRange, 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 };
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
@@ -64,6 +64,13 @@ var find = (arr, callback, from = "start") => {
64
64
  }
65
65
  return [null, -1];
66
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
+ });
67
74
 
68
75
  // src/elements.ts
69
76
  var getGlobalThis = () => {
@@ -175,6 +182,14 @@ var throttle = (fn, delay = 200) => {
175
182
  }
176
183
  };
177
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
+ }
178
193
 
179
194
  // src/number.ts
180
195
  var toNumber = (val) => {
@@ -199,12 +214,34 @@ var kebabCase = (s) => {
199
214
  const ret = s.replace(/([A-Z])/g, " $1").trim();
200
215
  return ret.split(" ").join("-").toLowerCase();
201
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
+ }
202
236
  export {
203
237
  bigCamelize,
238
+ call,
204
239
  camelize,
205
240
  cancelAnimationFrame,
206
241
  clamp,
207
242
  clampArrayRange,
243
+ classes,
244
+ createNamespaceFn,
208
245
  debounce,
209
246
  doubleRaf,
210
247
  find,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/shared",
3
- "version": "2.18.2-alpha.1698728560863",
3
+ "version": "2.18.2",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",