@bouzu/shared 0.1.1 → 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 +0 -4
- package/dist/index.cjs +52 -46
- package/dist/index.d.cts +22 -20
- package/dist/index.d.mts +22 -20
- package/dist/index.d.ts +22 -20
- package/dist/index.mjs +52 -47
- package/package.json +3 -3
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,11 +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
|
-
const isClient = typeof window !== "undefined" && typeof document !== "undefined";
|
|
170
|
-
|
|
171
134
|
const Axis = {
|
|
172
135
|
X: "x",
|
|
173
136
|
Y: "y"
|
|
@@ -227,6 +190,36 @@ function checkRectIntersectsByAxis(a, b, axis, reverse = false) {
|
|
|
227
190
|
}
|
|
228
191
|
}
|
|
229
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
|
+
|
|
230
223
|
function registerRaf(handler, methods) {
|
|
231
224
|
const raf = methods?.raf ?? window.requestAnimationFrame;
|
|
232
225
|
const caf = methods?.caf ?? window.cancelAnimationFrame;
|
|
@@ -243,10 +236,6 @@ const perf = isClient ? window?.performance ?? null : null;
|
|
|
243
236
|
const perfNowFn = perf?.now ?? perf?.webkitNow ?? perf?.msNow ?? perf?.mozNow;
|
|
244
237
|
const getTime = perfNowFn ? perfNowFn.bind(perf) : Date.now ?? (() => (/* @__PURE__ */ new Date()).getTime());
|
|
245
238
|
|
|
246
|
-
function clamp(value, min, max) {
|
|
247
|
-
return Math.min(Math.max(value, min), max);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
239
|
function runTransition(options) {
|
|
251
240
|
const {
|
|
252
241
|
start,
|
|
@@ -300,6 +289,22 @@ const easeLinear = (t) => t;
|
|
|
300
289
|
const easeOut = (t) => Math.sin(t * Math.PI / 2);
|
|
301
290
|
const easeOutCubic = (t) => 1 - (1 - t) ** 3;
|
|
302
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
|
+
|
|
303
308
|
exports.Axis = Axis;
|
|
304
309
|
exports.RectCorner = RectCorner;
|
|
305
310
|
exports.checkAxis = checkAxis;
|
|
@@ -321,6 +326,7 @@ exports.clamp = clamp;
|
|
|
321
326
|
exports.clonePoint = clonePoint;
|
|
322
327
|
exports.cloneRect = cloneRect;
|
|
323
328
|
exports.createPoint = createPoint;
|
|
329
|
+
exports.createPointByAxis = createPointByAxis;
|
|
324
330
|
exports.createRect = createRect;
|
|
325
331
|
exports.createSize = createSize;
|
|
326
332
|
exports.easeLinear = easeLinear;
|
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,9 +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
|
-
declare const isClient: boolean;
|
|
69
|
-
|
|
70
54
|
declare const Axis: {
|
|
71
55
|
readonly X: "x";
|
|
72
56
|
readonly Y: "y";
|
|
@@ -81,6 +65,16 @@ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number,
|
|
|
81
65
|
declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
|
|
82
66
|
declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
|
|
83
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
|
+
|
|
84
78
|
interface RegisterRafMethods {
|
|
85
79
|
/**
|
|
86
80
|
* requestAnimationFrame
|
|
@@ -97,8 +91,6 @@ declare function registerRaf(handler: (time: number) => void, methods?: Register
|
|
|
97
91
|
type GetTimeFn = () => number;
|
|
98
92
|
declare const getTime: GetTimeFn;
|
|
99
93
|
|
|
100
|
-
declare function clamp(value: number, min: number, max: number): number;
|
|
101
|
-
|
|
102
94
|
interface RunTransitionOptions {
|
|
103
95
|
start: number;
|
|
104
96
|
end: number;
|
|
@@ -121,4 +113,14 @@ declare const easeLinear: EaseFn;
|
|
|
121
113
|
declare const easeOut: EaseFn;
|
|
122
114
|
declare const easeOutCubic: EaseFn;
|
|
123
115
|
|
|
124
|
-
|
|
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,9 +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
|
-
declare const isClient: boolean;
|
|
69
|
-
|
|
70
54
|
declare const Axis: {
|
|
71
55
|
readonly X: "x";
|
|
72
56
|
readonly Y: "y";
|
|
@@ -81,6 +65,16 @@ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number,
|
|
|
81
65
|
declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
|
|
82
66
|
declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
|
|
83
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
|
+
|
|
84
78
|
interface RegisterRafMethods {
|
|
85
79
|
/**
|
|
86
80
|
* requestAnimationFrame
|
|
@@ -97,8 +91,6 @@ declare function registerRaf(handler: (time: number) => void, methods?: Register
|
|
|
97
91
|
type GetTimeFn = () => number;
|
|
98
92
|
declare const getTime: GetTimeFn;
|
|
99
93
|
|
|
100
|
-
declare function clamp(value: number, min: number, max: number): number;
|
|
101
|
-
|
|
102
94
|
interface RunTransitionOptions {
|
|
103
95
|
start: number;
|
|
104
96
|
end: number;
|
|
@@ -121,4 +113,14 @@ declare const easeLinear: EaseFn;
|
|
|
121
113
|
declare const easeOut: EaseFn;
|
|
122
114
|
declare const easeOutCubic: EaseFn;
|
|
123
115
|
|
|
124
|
-
|
|
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,9 +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
|
-
declare const isClient: boolean;
|
|
69
|
-
|
|
70
54
|
declare const Axis: {
|
|
71
55
|
readonly X: "x";
|
|
72
56
|
readonly Y: "y";
|
|
@@ -81,6 +65,16 @@ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number,
|
|
|
81
65
|
declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
|
|
82
66
|
declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
|
|
83
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
|
+
|
|
84
78
|
interface RegisterRafMethods {
|
|
85
79
|
/**
|
|
86
80
|
* requestAnimationFrame
|
|
@@ -97,8 +91,6 @@ declare function registerRaf(handler: (time: number) => void, methods?: Register
|
|
|
97
91
|
type GetTimeFn = () => number;
|
|
98
92
|
declare const getTime: GetTimeFn;
|
|
99
93
|
|
|
100
|
-
declare function clamp(value: number, min: number, max: number): number;
|
|
101
|
-
|
|
102
94
|
interface RunTransitionOptions {
|
|
103
95
|
start: number;
|
|
104
96
|
end: number;
|
|
@@ -121,4 +113,14 @@ declare const easeLinear: EaseFn;
|
|
|
121
113
|
declare const easeOut: EaseFn;
|
|
122
114
|
declare const easeOutCubic: EaseFn;
|
|
123
115
|
|
|
124
|
-
|
|
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,11 +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
|
-
const isClient = typeof window !== "undefined" && typeof document !== "undefined";
|
|
168
|
-
|
|
169
132
|
const Axis = {
|
|
170
133
|
X: "x",
|
|
171
134
|
Y: "y"
|
|
@@ -225,6 +188,36 @@ function checkRectIntersectsByAxis(a, b, axis, reverse = false) {
|
|
|
225
188
|
}
|
|
226
189
|
}
|
|
227
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
|
+
|
|
228
221
|
function registerRaf(handler, methods) {
|
|
229
222
|
const raf = methods?.raf ?? window.requestAnimationFrame;
|
|
230
223
|
const caf = methods?.caf ?? window.cancelAnimationFrame;
|
|
@@ -241,10 +234,6 @@ const perf = isClient ? window?.performance ?? null : null;
|
|
|
241
234
|
const perfNowFn = perf?.now ?? perf?.webkitNow ?? perf?.msNow ?? perf?.mozNow;
|
|
242
235
|
const getTime = perfNowFn ? perfNowFn.bind(perf) : Date.now ?? (() => (/* @__PURE__ */ new Date()).getTime());
|
|
243
236
|
|
|
244
|
-
function clamp(value, min, max) {
|
|
245
|
-
return Math.min(Math.max(value, min), max);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
237
|
function runTransition(options) {
|
|
249
238
|
const {
|
|
250
239
|
start,
|
|
@@ -298,4 +287,20 @@ const easeLinear = (t) => t;
|
|
|
298
287
|
const easeOut = (t) => Math.sin(t * Math.PI / 2);
|
|
299
288
|
const easeOutCubic = (t) => 1 - (1 - t) ** 3;
|
|
300
289
|
|
|
301
|
-
|
|
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.
|
|
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": {
|