@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.
Files changed (3) hide show
  1. package/lib/index.d.ts +34 -32
  2. package/lib/index.js +172 -118
  3. package/package.json +4 -4
package/lib/index.d.ts CHANGED
@@ -1,32 +1,34 @@
1
- export declare const isString: (val: unknown) => val is string;
2
- export declare const isBoolean: (val: unknown) => val is boolean;
3
- export declare const isNumber: (val: unknown) => val is number;
4
- export declare const isPlainObject: (val: unknown) => val is Record<string, any>;
5
- export declare const isObject: (val: unknown) => val is Record<string, any>;
6
- export declare const isFunction: (val: unknown) => val is Function;
7
- export declare const isArray: (val: unknown) => val is any[];
8
- export declare const isURL: (val: string | undefined | null) => boolean;
9
- export declare const isEmpty: (val: unknown) => boolean;
10
- export declare const isWindow: (val: unknown) => val is Window;
11
- export declare const supportTouch: () => boolean;
12
- export declare const toNumber: (val: number | string | boolean | undefined | null) => number;
13
- export declare const removeItem: (arr: Array<unknown>, item: unknown) => unknown[] | undefined;
14
- export declare const toggleItem: (arr: Array<unknown>, item: unknown) => void;
15
- export declare const throttle: (method: any, mustRunDelay?: number) => (() => void);
16
- export declare const inBrowser: () => boolean;
17
- export declare const uniq: (arr: Array<any>) => any[];
18
- export declare const bigCamelize: (s: string) => string;
19
- export declare const camelize: (s: string) => string;
20
- export declare const kebabCase: (s: string) => string;
21
- export declare const find: <T>(arr: T[], callback: (item: T, index: number, array: T[]) => any, from?: 'start' | 'end') => [T, number] | [null, -1];
22
- export declare const normalizeToArray: <T>(value: T | T[]) => T[];
23
- export declare const clamp: (num: number, min: number, max: number) => number;
24
- export declare const clampArrayRange: (index: number, arr: Array<unknown>) => number;
25
- export declare const getGlobalThis: () => typeof globalThis;
26
- export declare const requestAnimationFrame: (fn: FrameRequestCallback) => number;
27
- export declare const cancelAnimationFrame: (handle: number) => void;
28
- export declare const raf: () => Promise<unknown>;
29
- export declare const doubleRaf: () => Promise<unknown>;
30
- export declare const getStyle: (element: Element) => CSSStyleDeclaration;
31
- export declare const getRect: (element: Element | Window) => DOMRect;
32
- export declare const inViewport: (element: HTMLElement) => boolean;
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
- export const isString = (val) => typeof val === 'string';
2
- export const isBoolean = (val) => typeof val === 'boolean';
3
- export const isNumber = (val) => typeof val === 'number';
4
- export const isPlainObject = (val) => Object.prototype.toString.call(val) === '[object Object]';
5
- export const isObject = (val) => typeof val === 'object' && val !== null;
6
- // eslint-disable-next-line
7
- export const isFunction = (val) => typeof val === 'function';
8
- export const isArray = (val) => Array.isArray(val);
9
- export const isURL = (val) => {
10
- if (!val) {
11
- return false;
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
- return /^(http)|(\.*\/)/.test(val);
17
+ return a;
14
18
  };
15
- export const isEmpty = (val) => val === undefined || val === null || val === '' || (Array.isArray(val) && !val.length);
16
- export const isWindow = (val) => val === window;
17
- export const supportTouch = () => inBrowser() && 'ontouchstart' in window;
18
- export const toNumber = (val) => {
19
- if (val == null)
20
- return 0;
21
- if (isString(val)) {
22
- val = parseFloat(val);
23
- val = Number.isNaN(val) ? 0 : val;
24
- return val;
25
- }
26
- if (isBoolean(val))
27
- return Number(val);
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
- export const removeItem = (arr, item) => {
31
- if (arr.length) {
32
- const index = arr.indexOf(item);
33
- if (index > -1) {
34
- return arr.splice(index, 1);
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
- export const toggleItem = (arr, item) => {
39
- arr.includes(item) ? removeItem(arr, item) : arr.push(item);
58
+ var toggleItem = (arr, item) => {
59
+ arr.includes(item) ? removeItem(arr, item) : arr.push(item);
40
60
  };
41
- export const throttle = (method, mustRunDelay = 200) => {
42
- let timer;
43
- let start = 0;
44
- return function loop(...args) {
45
- const now = Date.now();
46
- const elapsed = now - start;
47
- if (!start) {
48
- start = now;
49
- }
50
- if (timer) {
51
- window.clearTimeout(timer);
52
- }
53
- if (elapsed >= mustRunDelay) {
54
- method.apply(this, args);
55
- start = now;
56
- }
57
- else {
58
- timer = window.setTimeout(() => {
59
- loop.apply(this, args);
60
- }, mustRunDelay - elapsed);
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
- export const inBrowser = () => typeof window !== 'undefined';
65
- export const uniq = (arr) => [...new Set(arr)];
66
- export const bigCamelize = (s) => camelize(s).replace(s.charAt(0), s.charAt(0).toUpperCase());
67
- export const camelize = (s) => s.replace(/-(\w)/g, (_, p) => p.toUpperCase());
68
- export const kebabCase = (s) => {
69
- const ret = s.replace(/([A-Z])/g, ' $1').trim();
70
- return ret.split(' ').join('-').toLowerCase();
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
- export const find = (arr, callback, from = 'start') => {
73
- let i = from === 'start' ? 0 : arr.length - 1;
74
- while (arr.length > 0 && i >= 0 && i <= arr.length - 1) {
75
- const flag = callback(arr[i], i, arr);
76
- if (flag) {
77
- return [arr[i], i];
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
- return [null, -1];
98
+ from === "start" ? i++ : i--;
99
+ }
100
+ return [null, -1];
82
101
  };
83
- export const normalizeToArray = (value) => (isArray(value) ? value : [value]);
84
- export const clamp = (num, min, max) => Math.min(max, Math.max(min, num));
85
- export const clampArrayRange = (index, arr) => clamp(index, 0, arr.length - 1);
86
- export const getGlobalThis = () => {
87
- if (typeof globalThis !== 'undefined') {
88
- return globalThis;
89
- }
90
- if (inBrowser()) {
91
- return window;
92
- }
93
- return typeof global !== 'undefined' ? global : self;
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
- export const requestAnimationFrame = (fn) => {
96
- const globalThis = getGlobalThis();
97
- return globalThis.requestAnimationFrame ? globalThis.requestAnimationFrame(fn) : globalThis.setTimeout(fn);
114
+ var requestAnimationFrame = (fn) => {
115
+ const globalThis2 = getGlobalThis();
116
+ return globalThis2.requestAnimationFrame ? globalThis2.requestAnimationFrame(fn) : globalThis2.setTimeout(fn);
98
117
  };
99
- export const cancelAnimationFrame = (handle) => {
100
- const globalThis = getGlobalThis();
101
- globalThis.cancelAnimationFrame ? globalThis.cancelAnimationFrame(handle) : globalThis.clearTimeout(handle);
118
+ var cancelAnimationFrame = (handle) => {
119
+ const globalThis2 = getGlobalThis();
120
+ globalThis2.cancelAnimationFrame ? globalThis2.cancelAnimationFrame(handle) : globalThis2.clearTimeout(handle);
102
121
  };
103
- export const raf = () => new Promise((resolve) => {
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
- export const doubleRaf = () => new Promise((resolve) => {
107
- requestAnimationFrame(() => {
108
- requestAnimationFrame(resolve);
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
- // shorthand only
112
- export const getStyle = (element) => window.getComputedStyle(element);
113
- export const getRect = (element) => {
114
- if (isWindow(element)) {
115
- const width = element.innerWidth;
116
- const height = element.innerHeight;
117
- const rect = {
118
- x: 0,
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 const inViewport = (element) => {
132
- const { top, bottom, left, right } = getRect(element);
133
- const { width, height } = getRect(window);
134
- const xInViewport = left <= width && right >= 0;
135
- const yInViewport = top <= height && bottom >= 0;
136
- return xInViewport && yInViewport;
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.2",
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
- "rimraf": "^5.0.1"
29
+ "tsup": "7.2.0"
30
30
  },
31
31
  "scripts": {
32
- "dev": "tsc --watch",
33
- "build": "rimraf ./lib && tsc"
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
  }