@bouzu/shared 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -25,9 +25,6 @@ Import:
25
25
  ```js
26
26
  // ESM
27
27
  import * as shared from '@bouzu/shared'
28
-
29
- // CommonJS
30
- const shared = require('@bouzu/shared')
31
28
  ```
32
29
 
33
30
  ## Development
@@ -44,7 +41,6 @@ Made with 💛
44
41
 
45
42
  Published under [MIT License](./LICENSE).
46
43
 
47
-
48
44
  <!-- Badges -->
49
45
 
50
46
  [npm-version-src]: https://img.shields.io/npm/v/@bouzu/shared?style=flat&colorA=18181B&colorB=F0DB4F
package/dist/index.cjs CHANGED
@@ -1,42 +1,5 @@
1
1
  'use strict';
2
2
 
3
- function noop() {
4
- }
5
-
6
- function usePrevious(val) {
7
- const value = {
8
- curr: val,
9
- prev: val
10
- };
11
- Object.defineProperty(value, "curr", { writable: false });
12
- Object.defineProperty(value, "prev", { writable: false });
13
- return [
14
- value,
15
- (nextValue) => {
16
- Object.defineProperty(value, "prev", { value: value.curr, writable: false });
17
- Object.defineProperty(value, "curr", { value: nextValue, writable: false });
18
- }
19
- ];
20
- }
21
-
22
- function execLastTick(tickFn, fn, ...tickFnArgs) {
23
- let taskId = 0;
24
- let lastArgs;
25
- return (...fnArgs) => {
26
- lastArgs = fnArgs;
27
- taskId++;
28
- const currId = taskId;
29
- tickFn(
30
- () => {
31
- if (currId !== taskId)
32
- return;
33
- fn.apply(null, lastArgs ?? []);
34
- },
35
- ...tickFnArgs
36
- );
37
- };
38
- }
39
-
40
3
  function createPoint(x = 0, y = 0) {
41
4
  return { x, y };
42
5
  }
@@ -63,6 +26,11 @@ function getPointDistance(p1, p2) {
63
26
  function clonePoint(a) {
64
27
  return createPoint(a.x, a.y);
65
28
  }
29
+ function createPointByAxis(value, axis) {
30
+ const point = createPoint();
31
+ point[axis] = value;
32
+ return point;
33
+ }
66
34
 
67
35
  function createSize(width = 0, height = 0) {
68
36
  return { width, height };
@@ -163,10 +131,6 @@ function toPoint(rect) {
163
131
  return createPoint(rect.x, rect.y);
164
132
  }
165
133
 
166
- function isObject(val) {
167
- return Object.prototype.toString.call(val) === "[object Object]";
168
- }
169
-
170
134
  const Axis = {
171
135
  X: "x",
172
136
  Y: "y"
@@ -226,6 +190,36 @@ function checkRectIntersectsByAxis(a, b, axis, reverse = false) {
226
190
  }
227
191
  }
228
192
 
193
+ function execLastTick(tickFn, fn, ...tickFnArgs) {
194
+ let taskId = 0;
195
+ let lastArgs;
196
+ return (...fnArgs) => {
197
+ lastArgs = fnArgs;
198
+ taskId++;
199
+ const currId = taskId;
200
+ tickFn(
201
+ () => {
202
+ if (currId !== taskId)
203
+ return;
204
+ fn.apply(null, lastArgs ?? []);
205
+ },
206
+ ...tickFnArgs
207
+ );
208
+ };
209
+ }
210
+
211
+ function isObject(val) {
212
+ return Object.prototype.toString.call(val) === "[object Object]";
213
+ }
214
+ const isClient = typeof window !== "undefined" && typeof document !== "undefined";
215
+
216
+ function clamp(value, min, max) {
217
+ return Math.min(Math.max(value, min), max);
218
+ }
219
+
220
+ function noop() {
221
+ }
222
+
229
223
  function registerRaf(handler, methods) {
230
224
  const raf = methods?.raf ?? window.requestAnimationFrame;
231
225
  const caf = methods?.caf ?? window.cancelAnimationFrame;
@@ -238,14 +232,10 @@ function registerRaf(handler, methods) {
238
232
  };
239
233
  }
240
234
 
241
- const perf = window?.performance ?? null;
235
+ const perf = isClient ? window?.performance ?? null : null;
242
236
  const perfNowFn = perf?.now ?? perf?.webkitNow ?? perf?.msNow ?? perf?.mozNow;
243
237
  const getTime = perfNowFn ? perfNowFn.bind(perf) : Date.now ?? (() => (/* @__PURE__ */ new Date()).getTime());
244
238
 
245
- function clamp(value, min, max) {
246
- return Math.min(Math.max(value, min), max);
247
- }
248
-
249
239
  function runTransition(options) {
250
240
  const {
251
241
  start,
@@ -299,6 +289,22 @@ const easeLinear = (t) => t;
299
289
  const easeOut = (t) => Math.sin(t * Math.PI / 2);
300
290
  const easeOutCubic = (t) => 1 - (1 - t) ** 3;
301
291
 
292
+ function usePrevious(val) {
293
+ const value = {
294
+ curr: val,
295
+ prev: val
296
+ };
297
+ Object.defineProperty(value, "curr", { writable: false });
298
+ Object.defineProperty(value, "prev", { writable: false });
299
+ return [
300
+ value,
301
+ (nextValue) => {
302
+ Object.defineProperty(value, "prev", { value: value.curr, writable: false });
303
+ Object.defineProperty(value, "curr", { value: nextValue, writable: false });
304
+ }
305
+ ];
306
+ }
307
+
302
308
  exports.Axis = Axis;
303
309
  exports.RectCorner = RectCorner;
304
310
  exports.checkAxis = checkAxis;
@@ -320,6 +326,7 @@ exports.clamp = clamp;
320
326
  exports.clonePoint = clonePoint;
321
327
  exports.cloneRect = cloneRect;
322
328
  exports.createPoint = createPoint;
329
+ exports.createPointByAxis = createPointByAxis;
323
330
  exports.createRect = createRect;
324
331
  exports.createSize = createSize;
325
332
  exports.easeLinear = easeLinear;
@@ -341,6 +348,7 @@ exports.getRectTopLeft = getRectTopLeft;
341
348
  exports.getRectTopRight = getRectTopRight;
342
349
  exports.getSizeByAxis = getSizeByAxis;
343
350
  exports.getTime = getTime;
351
+ exports.isClient = isClient;
344
352
  exports.isObject = isObject;
345
353
  exports.noop = noop;
346
354
  exports.registerRaf = registerRaf;
package/dist/index.d.cts CHANGED
@@ -1,19 +1,5 @@
1
1
  import { ValueOf } from 'type-fest';
2
2
 
3
- declare function noop(): void;
4
-
5
- type Previous<T> = [
6
- {
7
- readonly curr: T | undefined;
8
- readonly prev: T | undefined;
9
- },
10
- (val: T) => void
11
- ];
12
- declare function usePrevious<T>(val?: T): Previous<T>;
13
-
14
- type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
15
- declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
16
-
17
3
  interface Point {
18
4
  x: number;
19
5
  y: number;
@@ -25,6 +11,7 @@ declare function checkPointEqualWithTolerance(a: Point, b: Point, tolerance?: nu
25
11
  declare function getPointCenter(p1: Point, p2: Point): Point;
26
12
  declare function getPointDistance(p1: Point, p2: Point): number;
27
13
  declare function clonePoint(a: Point): Point;
14
+ declare function createPointByAxis(value: number, axis: AxisValue): Point;
28
15
 
29
16
  interface Size {
30
17
  width: number;
@@ -64,8 +51,6 @@ declare function checkLayoutInvalidate(newRect: Rect, oldRect: Rect): boolean;
64
51
  declare function toSize(rect: Rect): Size;
65
52
  declare function toPoint(rect: Rect): Point;
66
53
 
67
- declare function isObject(val: unknown): val is Record<PropertyKey, any>;
68
-
69
54
  declare const Axis: {
70
55
  readonly X: "x";
71
56
  readonly Y: "y";
@@ -80,6 +65,16 @@ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number,
80
65
  declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
81
66
  declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
82
67
 
68
+ type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
69
+ declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
70
+
71
+ declare function isObject(val: unknown): val is Record<PropertyKey, any>;
72
+ declare const isClient: boolean;
73
+
74
+ declare function clamp(value: number, min: number, max: number): number;
75
+
76
+ declare function noop(): void;
77
+
83
78
  interface RegisterRafMethods {
84
79
  /**
85
80
  * requestAnimationFrame
@@ -96,8 +91,6 @@ declare function registerRaf(handler: (time: number) => void, methods?: Register
96
91
  type GetTimeFn = () => number;
97
92
  declare const getTime: GetTimeFn;
98
93
 
99
- declare function clamp(value: number, min: number, max: number): number;
100
-
101
94
  interface RunTransitionOptions {
102
95
  start: number;
103
96
  end: number;
@@ -120,4 +113,14 @@ declare const easeLinear: EaseFn;
120
113
  declare const easeOut: EaseFn;
121
114
  declare const easeOutCubic: EaseFn;
122
115
 
123
- export { Axis, type AxisValue, type CancelRaf, type EaseFn, type GetTimeFn, type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type RegisterRafMethods, type RunTransitionOptions, type Size, type TransitionRunner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
116
+ type Previous<T> = [
117
+ {
118
+ readonly curr: T | undefined;
119
+ readonly prev: T | undefined;
120
+ },
121
+ (val: T) => void
122
+ ];
123
+ declare function usePrevious<T>(val?: T): Previous<T>;
124
+
125
+ export { Axis, RectCorner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createPointByAxis, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isClient, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
126
+ export type { AxisValue, CancelRaf, EaseFn, GetTimeFn, Point, Previous, Rect, RectCornerValue, RegisterRafMethods, RunTransitionOptions, Size, TransitionRunner };
package/dist/index.d.mts CHANGED
@@ -1,19 +1,5 @@
1
1
  import { ValueOf } from 'type-fest';
2
2
 
3
- declare function noop(): void;
4
-
5
- type Previous<T> = [
6
- {
7
- readonly curr: T | undefined;
8
- readonly prev: T | undefined;
9
- },
10
- (val: T) => void
11
- ];
12
- declare function usePrevious<T>(val?: T): Previous<T>;
13
-
14
- type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
15
- declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
16
-
17
3
  interface Point {
18
4
  x: number;
19
5
  y: number;
@@ -25,6 +11,7 @@ declare function checkPointEqualWithTolerance(a: Point, b: Point, tolerance?: nu
25
11
  declare function getPointCenter(p1: Point, p2: Point): Point;
26
12
  declare function getPointDistance(p1: Point, p2: Point): number;
27
13
  declare function clonePoint(a: Point): Point;
14
+ declare function createPointByAxis(value: number, axis: AxisValue): Point;
28
15
 
29
16
  interface Size {
30
17
  width: number;
@@ -64,8 +51,6 @@ declare function checkLayoutInvalidate(newRect: Rect, oldRect: Rect): boolean;
64
51
  declare function toSize(rect: Rect): Size;
65
52
  declare function toPoint(rect: Rect): Point;
66
53
 
67
- declare function isObject(val: unknown): val is Record<PropertyKey, any>;
68
-
69
54
  declare const Axis: {
70
55
  readonly X: "x";
71
56
  readonly Y: "y";
@@ -80,6 +65,16 @@ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number,
80
65
  declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
81
66
  declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
82
67
 
68
+ type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
69
+ declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
70
+
71
+ declare function isObject(val: unknown): val is Record<PropertyKey, any>;
72
+ declare const isClient: boolean;
73
+
74
+ declare function clamp(value: number, min: number, max: number): number;
75
+
76
+ declare function noop(): void;
77
+
83
78
  interface RegisterRafMethods {
84
79
  /**
85
80
  * requestAnimationFrame
@@ -96,8 +91,6 @@ declare function registerRaf(handler: (time: number) => void, methods?: Register
96
91
  type GetTimeFn = () => number;
97
92
  declare const getTime: GetTimeFn;
98
93
 
99
- declare function clamp(value: number, min: number, max: number): number;
100
-
101
94
  interface RunTransitionOptions {
102
95
  start: number;
103
96
  end: number;
@@ -120,4 +113,14 @@ declare const easeLinear: EaseFn;
120
113
  declare const easeOut: EaseFn;
121
114
  declare const easeOutCubic: EaseFn;
122
115
 
123
- export { Axis, type AxisValue, type CancelRaf, type EaseFn, type GetTimeFn, type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type RegisterRafMethods, type RunTransitionOptions, type Size, type TransitionRunner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
116
+ type Previous<T> = [
117
+ {
118
+ readonly curr: T | undefined;
119
+ readonly prev: T | undefined;
120
+ },
121
+ (val: T) => void
122
+ ];
123
+ declare function usePrevious<T>(val?: T): Previous<T>;
124
+
125
+ export { Axis, RectCorner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createPointByAxis, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isClient, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
126
+ export type { AxisValue, CancelRaf, EaseFn, GetTimeFn, Point, Previous, Rect, RectCornerValue, RegisterRafMethods, RunTransitionOptions, Size, TransitionRunner };
package/dist/index.d.ts CHANGED
@@ -1,19 +1,5 @@
1
1
  import { ValueOf } from 'type-fest';
2
2
 
3
- declare function noop(): void;
4
-
5
- type Previous<T> = [
6
- {
7
- readonly curr: T | undefined;
8
- readonly prev: T | undefined;
9
- },
10
- (val: T) => void
11
- ];
12
- declare function usePrevious<T>(val?: T): Previous<T>;
13
-
14
- type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
15
- declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
16
-
17
3
  interface Point {
18
4
  x: number;
19
5
  y: number;
@@ -25,6 +11,7 @@ declare function checkPointEqualWithTolerance(a: Point, b: Point, tolerance?: nu
25
11
  declare function getPointCenter(p1: Point, p2: Point): Point;
26
12
  declare function getPointDistance(p1: Point, p2: Point): number;
27
13
  declare function clonePoint(a: Point): Point;
14
+ declare function createPointByAxis(value: number, axis: AxisValue): Point;
28
15
 
29
16
  interface Size {
30
17
  width: number;
@@ -64,8 +51,6 @@ declare function checkLayoutInvalidate(newRect: Rect, oldRect: Rect): boolean;
64
51
  declare function toSize(rect: Rect): Size;
65
52
  declare function toPoint(rect: Rect): Point;
66
53
 
67
- declare function isObject(val: unknown): val is Record<PropertyKey, any>;
68
-
69
54
  declare const Axis: {
70
55
  readonly X: "x";
71
56
  readonly Y: "y";
@@ -80,6 +65,16 @@ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number,
80
65
  declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
81
66
  declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
82
67
 
68
+ type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
69
+ declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
70
+
71
+ declare function isObject(val: unknown): val is Record<PropertyKey, any>;
72
+ declare const isClient: boolean;
73
+
74
+ declare function clamp(value: number, min: number, max: number): number;
75
+
76
+ declare function noop(): void;
77
+
83
78
  interface RegisterRafMethods {
84
79
  /**
85
80
  * requestAnimationFrame
@@ -96,8 +91,6 @@ declare function registerRaf(handler: (time: number) => void, methods?: Register
96
91
  type GetTimeFn = () => number;
97
92
  declare const getTime: GetTimeFn;
98
93
 
99
- declare function clamp(value: number, min: number, max: number): number;
100
-
101
94
  interface RunTransitionOptions {
102
95
  start: number;
103
96
  end: number;
@@ -120,4 +113,14 @@ declare const easeLinear: EaseFn;
120
113
  declare const easeOut: EaseFn;
121
114
  declare const easeOutCubic: EaseFn;
122
115
 
123
- export { Axis, type AxisValue, type CancelRaf, type EaseFn, type GetTimeFn, type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type RegisterRafMethods, type RunTransitionOptions, type Size, type TransitionRunner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
116
+ type Previous<T> = [
117
+ {
118
+ readonly curr: T | undefined;
119
+ readonly prev: T | undefined;
120
+ },
121
+ (val: T) => void
122
+ ];
123
+ declare function usePrevious<T>(val?: T): Previous<T>;
124
+
125
+ export { Axis, RectCorner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createPointByAxis, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isClient, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
126
+ export type { AxisValue, CancelRaf, EaseFn, GetTimeFn, Point, Previous, Rect, RectCornerValue, RegisterRafMethods, RunTransitionOptions, Size, TransitionRunner };
package/dist/index.mjs CHANGED
@@ -1,40 +1,3 @@
1
- function noop() {
2
- }
3
-
4
- function usePrevious(val) {
5
- const value = {
6
- curr: val,
7
- prev: val
8
- };
9
- Object.defineProperty(value, "curr", { writable: false });
10
- Object.defineProperty(value, "prev", { writable: false });
11
- return [
12
- value,
13
- (nextValue) => {
14
- Object.defineProperty(value, "prev", { value: value.curr, writable: false });
15
- Object.defineProperty(value, "curr", { value: nextValue, writable: false });
16
- }
17
- ];
18
- }
19
-
20
- function execLastTick(tickFn, fn, ...tickFnArgs) {
21
- let taskId = 0;
22
- let lastArgs;
23
- return (...fnArgs) => {
24
- lastArgs = fnArgs;
25
- taskId++;
26
- const currId = taskId;
27
- tickFn(
28
- () => {
29
- if (currId !== taskId)
30
- return;
31
- fn.apply(null, lastArgs ?? []);
32
- },
33
- ...tickFnArgs
34
- );
35
- };
36
- }
37
-
38
1
  function createPoint(x = 0, y = 0) {
39
2
  return { x, y };
40
3
  }
@@ -61,6 +24,11 @@ function getPointDistance(p1, p2) {
61
24
  function clonePoint(a) {
62
25
  return createPoint(a.x, a.y);
63
26
  }
27
+ function createPointByAxis(value, axis) {
28
+ const point = createPoint();
29
+ point[axis] = value;
30
+ return point;
31
+ }
64
32
 
65
33
  function createSize(width = 0, height = 0) {
66
34
  return { width, height };
@@ -161,10 +129,6 @@ function toPoint(rect) {
161
129
  return createPoint(rect.x, rect.y);
162
130
  }
163
131
 
164
- function isObject(val) {
165
- return Object.prototype.toString.call(val) === "[object Object]";
166
- }
167
-
168
132
  const Axis = {
169
133
  X: "x",
170
134
  Y: "y"
@@ -224,6 +188,36 @@ function checkRectIntersectsByAxis(a, b, axis, reverse = false) {
224
188
  }
225
189
  }
226
190
 
191
+ function execLastTick(tickFn, fn, ...tickFnArgs) {
192
+ let taskId = 0;
193
+ let lastArgs;
194
+ return (...fnArgs) => {
195
+ lastArgs = fnArgs;
196
+ taskId++;
197
+ const currId = taskId;
198
+ tickFn(
199
+ () => {
200
+ if (currId !== taskId)
201
+ return;
202
+ fn.apply(null, lastArgs ?? []);
203
+ },
204
+ ...tickFnArgs
205
+ );
206
+ };
207
+ }
208
+
209
+ function isObject(val) {
210
+ return Object.prototype.toString.call(val) === "[object Object]";
211
+ }
212
+ const isClient = typeof window !== "undefined" && typeof document !== "undefined";
213
+
214
+ function clamp(value, min, max) {
215
+ return Math.min(Math.max(value, min), max);
216
+ }
217
+
218
+ function noop() {
219
+ }
220
+
227
221
  function registerRaf(handler, methods) {
228
222
  const raf = methods?.raf ?? window.requestAnimationFrame;
229
223
  const caf = methods?.caf ?? window.cancelAnimationFrame;
@@ -236,14 +230,10 @@ function registerRaf(handler, methods) {
236
230
  };
237
231
  }
238
232
 
239
- const perf = window?.performance ?? null;
233
+ const perf = isClient ? window?.performance ?? null : null;
240
234
  const perfNowFn = perf?.now ?? perf?.webkitNow ?? perf?.msNow ?? perf?.mozNow;
241
235
  const getTime = perfNowFn ? perfNowFn.bind(perf) : Date.now ?? (() => (/* @__PURE__ */ new Date()).getTime());
242
236
 
243
- function clamp(value, min, max) {
244
- return Math.min(Math.max(value, min), max);
245
- }
246
-
247
237
  function runTransition(options) {
248
238
  const {
249
239
  start,
@@ -297,4 +287,20 @@ const easeLinear = (t) => t;
297
287
  const easeOut = (t) => Math.sin(t * Math.PI / 2);
298
288
  const easeOutCubic = (t) => 1 - (1 - t) ** 3;
299
289
 
300
- export { Axis, RectCorner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
290
+ function usePrevious(val) {
291
+ const value = {
292
+ curr: val,
293
+ prev: val
294
+ };
295
+ Object.defineProperty(value, "curr", { writable: false });
296
+ Object.defineProperty(value, "prev", { writable: false });
297
+ return [
298
+ value,
299
+ (nextValue) => {
300
+ Object.defineProperty(value, "prev", { value: value.curr, writable: false });
301
+ Object.defineProperty(value, "curr", { value: nextValue, writable: false });
302
+ }
303
+ ];
304
+ }
305
+
306
+ export { Axis, RectCorner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createPointByAxis, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isClient, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@bouzu/shared",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "author": "zhong666 <hi@zhong666.me>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://bouzu.zhong666.me",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/aa900031/bouzu"
10
+ "url": "https://github.com/aa900031/bouzu",
11
+ "directory": "packages/shared"
11
12
  },
12
13
  "bugs": {
13
14
  "url": "https://github.com/aa900031/bouzu/issues"
@@ -33,7 +34,6 @@
33
34
  "type-fest": "^3.13.1"
34
35
  },
35
36
  "devDependencies": {
36
- "@bouzu/release-it-config": "0.1.0",
37
37
  "@bouzu/tsconfig": "0.1.0"
38
38
  },
39
39
  "scripts": {