@react-fabric/utilities 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/_ascii.js +76 -0
- package/dist/cjs/_boundingBox.js +59 -0
- package/dist/cjs/_countries.js +2805 -0
- package/dist/cjs/_debounce.js +53 -0
- package/dist/cjs/_dedupe.js +63 -0
- package/dist/cjs/_fetch.js +128 -0
- package/dist/cjs/_fileType.js +967 -0
- package/dist/cjs/_format.js +118 -0
- package/dist/cjs/_getByPath.js +125 -0
- package/dist/cjs/_hash.js +67 -0
- package/dist/cjs/_interpolate.js +47 -0
- package/dist/cjs/_isEqual.js +108 -0
- package/dist/cjs/_isType.js +98 -0
- package/dist/cjs/_mergeRefs.js +40 -0
- package/dist/cjs/_tokenize.js +67 -0
- package/dist/cjs/getImageColorset.js +70 -0
- package/dist/cjs/index.js +116 -0
- package/dist/cjs/useLogger.js +339 -0
- package/dist/cjs/yup.js +172 -0
- package/dist/esm/_ascii.js +76 -0
- package/dist/esm/_boundingBox.js +58 -0
- package/dist/esm/_countries.js +2805 -0
- package/dist/esm/_debounce.js +53 -0
- package/dist/esm/_dedupe.js +60 -0
- package/dist/esm/_fetch.js +128 -0
- package/dist/esm/_fileType.js +967 -0
- package/dist/esm/_format.js +120 -0
- package/dist/esm/_getByPath.js +124 -0
- package/dist/esm/_hash.js +65 -0
- package/dist/esm/_interpolate.js +46 -0
- package/dist/esm/_isEqual.js +106 -0
- package/dist/esm/_isType.js +85 -0
- package/dist/esm/_mergeRefs.js +40 -0
- package/dist/esm/_tokenize.js +66 -0
- package/dist/esm/getImageColorset.js +70 -0
- package/dist/esm/index.js +52 -0
- package/dist/esm/useLogger.js +339 -0
- package/dist/esm/yup.js +152 -0
- package/dist/types/_ascii.d.ts +5 -0
- package/dist/types/_boundingBox.d.ts +10 -0
- package/dist/types/_countries.d.ts +85 -0
- package/dist/types/_debounce.d.ts +8 -0
- package/dist/types/_dedupe.d.ts +4 -0
- package/dist/types/_fetch.d.ts +23 -0
- package/dist/types/_fileType.d.ts +926 -0
- package/dist/types/_format.d.ts +27 -0
- package/dist/types/_getByPath.d.ts +10 -0
- package/dist/types/_hash.d.ts +16 -0
- package/dist/types/_interpolate.d.ts +6 -0
- package/dist/types/_isEqual.d.ts +15 -0
- package/dist/types/_isType.d.ts +14 -0
- package/dist/types/_mergeRefs.d.ts +1 -0
- package/dist/types/_tokenize.d.ts +8 -0
- package/dist/types/getImageColorset.d.ts +1 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/useLogger.d.ts +25 -0
- package/dist/types/yup.d.ts +1 -0
- package/package.json +48 -0
- package/src/_ascii.ts +93 -0
- package/src/_boundingBox.ts +72 -0
- package/src/_countries.ts +3046 -0
- package/src/_debounce.ts +42 -0
- package/src/_dedupe.ts +56 -0
- package/src/_fetch.ts +109 -0
- package/src/_fileType.ts +993 -0
- package/src/_format.ts +141 -0
- package/src/_getByPath.ts +108 -0
- package/src/_hash.ts +62 -0
- package/src/_interpolate.ts +42 -0
- package/src/_isEqual.ts +102 -0
- package/src/_isType.ts +105 -0
- package/src/_mergeRefs.ts +33 -0
- package/src/_tokenize.ts +69 -0
- package/src/getImageColorset.ts +81 -0
- package/src/index.ts +51 -0
- package/src/useLogger.ts +304 -0
- package/src/yup.ts +75 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare namespace Format {
|
|
2
|
+
/** ***************** format phone number using `libphonenumber-js` *******************/
|
|
3
|
+
const phone: (value?: string, country?: string, useCss?: boolean) => string | undefined;
|
|
4
|
+
/** ***************** format whole number using `numeral` *******************/
|
|
5
|
+
const number: (number?: string | number, format?: string) => string;
|
|
6
|
+
/** ***************** format bytes using `numeral` *******************/
|
|
7
|
+
const bytes: (number?: string | number) => string;
|
|
8
|
+
/** ***************** format percentage using `numeral` *******************/
|
|
9
|
+
const percent: (number?: string | number) => string;
|
|
10
|
+
/** ***************** format date using `date-fns` *******************/
|
|
11
|
+
const date: (date?: Date | string | number, fmt?: string) => string;
|
|
12
|
+
/** ***************** format time duration from total seconds/milliseconds *******************/
|
|
13
|
+
/**
|
|
14
|
+
* Format time duration with milliseconds
|
|
15
|
+
* @param number
|
|
16
|
+
* @param isFraction
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
const duration: (number?: string | number, isFraction?: boolean) => string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Format time duration without milliseconds
|
|
22
|
+
* @param number
|
|
23
|
+
* @param isFraction
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
const durationSeconds: (number?: string | number, isFraction?: boolean) => string | undefined;
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* get property value by path
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export declare const getByPath: <T = any>(obj: KeyValue, path: string, defaultValue?: any) => T;
|
|
6
|
+
/**
|
|
7
|
+
* get first available value in list
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare const getValue: (...args: any[]) => any;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* calculate hash from string
|
|
3
|
+
* @param str
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare const hash: (str: string) => number;
|
|
7
|
+
/**
|
|
8
|
+
* generate short hash from current time
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare const shortHash: () => string;
|
|
12
|
+
/**
|
|
13
|
+
* generate uuid using `crypto.getRandomValues`
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare const uuid: () => string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* check equality of objects
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export declare const isEqual: (obj: unknown, test: unknown) => boolean;
|
|
6
|
+
/**
|
|
7
|
+
* match strings
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare const matchString: (base: string, match: string, equality?: "start" | boolean) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* compare values
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare const compareValues: (order?: "asc" | "desc", key?: string) => (a: unknown, b: unknown) => 0 | 1 | -1;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const isUndefined: (value: unknown) => value is undefined;
|
|
2
|
+
export declare const isNull: (value: unknown) => value is null;
|
|
3
|
+
export declare const isNil: (val: unknown) => val is undefined;
|
|
4
|
+
export declare const isEmpty: (val: unknown) => val is undefined;
|
|
5
|
+
export declare const isObject: (val: unknown) => val is KeyValue<any>;
|
|
6
|
+
export declare const isArray: <T = unknown>(val: unknown) => val is T[];
|
|
7
|
+
export declare const isString: (value: unknown) => value is string;
|
|
8
|
+
export declare const isNumber: (value: unknown) => value is number;
|
|
9
|
+
export declare const isBoolean: (value: unknown) => value is boolean;
|
|
10
|
+
export declare const isTrue: (value?: unknown) => value is true;
|
|
11
|
+
export declare const isFalse: (value?: unknown) => value is false;
|
|
12
|
+
export declare const isColor: (color: string) => boolean;
|
|
13
|
+
export declare const isSvgPath: (value: unknown) => value is string;
|
|
14
|
+
export declare const isRtl: () => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const mergeRefs: (...refs: AnyObject[]) => (el: AnyObject) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getImageColorset: (el: HTMLImageElement) => string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from "./_ascii";
|
|
2
|
+
export * from "./_boundingBox";
|
|
3
|
+
export { getBoundingBox, getBox } from "./_boundingBox";
|
|
4
|
+
export { default as Countries } from "./_countries";
|
|
5
|
+
export type { Country } from "./_countries";
|
|
6
|
+
export * from "./_debounce";
|
|
7
|
+
export * from "./_dedupe";
|
|
8
|
+
export { _fetch as fetch } from "./_fetch";
|
|
9
|
+
export { default as FileUtil } from "./_fileType";
|
|
10
|
+
export * from "./_format";
|
|
11
|
+
export * from "./_getByPath";
|
|
12
|
+
export * from "./_hash";
|
|
13
|
+
export * from "./_interpolate";
|
|
14
|
+
export * from "./_isEqual";
|
|
15
|
+
export * from "./_isType";
|
|
16
|
+
export * from "./_mergeRefs";
|
|
17
|
+
export * from "./_tokenize";
|
|
18
|
+
export { getImageColorset } from "./getImageColorset";
|
|
19
|
+
export { useLogger } from "./useLogger";
|
|
20
|
+
export * from "./yup";
|
|
21
|
+
export declare const EMPTY_ARRAY: never[];
|
|
22
|
+
export declare const EMPTY_OBJECT: {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface LogDetail {
|
|
2
|
+
module?: string;
|
|
3
|
+
process?: string;
|
|
4
|
+
duration?: number;
|
|
5
|
+
totalTime?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare const useLogger: (mod?: string) => {
|
|
11
|
+
timer(process: string): {
|
|
12
|
+
log: (msg: string, ...rest: unknown[]) => void;
|
|
13
|
+
debug: (msg: string, ...rest: unknown[]) => void;
|
|
14
|
+
info: (msg: string, ...rest: unknown[]) => void;
|
|
15
|
+
error: (msg: string, ...rest: unknown[]) => void;
|
|
16
|
+
warning: (msg: string, ...rest: unknown[]) => void;
|
|
17
|
+
end: (msg?: string, ...rest: unknown[]) => void;
|
|
18
|
+
};
|
|
19
|
+
log(msg: string, { module, ...opts }: LogDetail, ...rest: unknown[]): void;
|
|
20
|
+
debug(msg: string, { module, ...opts }: LogDetail, ...rest: unknown[]): void;
|
|
21
|
+
info(msg: string, { module, ...opts }: LogDetail, ...rest: unknown[]): void;
|
|
22
|
+
error(msg: string, { module, ...opts }: LogDetail, ...rest: unknown[]): void;
|
|
23
|
+
warning(msg: string, { module, ...opts }: LogDetail, ...rest: unknown[]): void;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as yup from "yup";
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-fabric/utilities",
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
|
+
"description": "RUF: Utility package",
|
|
5
|
+
"author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"source": "src/index.ts",
|
|
8
|
+
"main": "dist/cjs/index.js",
|
|
9
|
+
"module": "dist/esm/index.js",
|
|
10
|
+
"types": "dist/types/index.d.ts",
|
|
11
|
+
"directories": {
|
|
12
|
+
"src": "src",
|
|
13
|
+
"test": "tests"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"registry": "https://registry.npmjs.org/"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"makelink": "yarn link",
|
|
25
|
+
"removelink": "yarn unlink",
|
|
26
|
+
"prebuild": "rm -rf ./dist",
|
|
27
|
+
"build:dts": "tsc -p ./tsconfig.build.json",
|
|
28
|
+
"build:rollup": "rollup -c ../../rollup.config.mjs",
|
|
29
|
+
"build": "npm-run-all -p build:rollup build:dts",
|
|
30
|
+
"watch:dts": "tsc -w -p ./tsconfig.build.json",
|
|
31
|
+
"watch:rollup": "rollup -w -c ../../rollup.config.mjs",
|
|
32
|
+
"watch": "npm-run-all -p watch:rollup watch:dts"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"date-fns": "^4.0.0",
|
|
36
|
+
"libphonenumber-js": "^1.11.8",
|
|
37
|
+
"numeral": "^2.0.6",
|
|
38
|
+
"yup": "^1.4.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/numeral": "^2.0.2"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=18.12.0",
|
|
45
|
+
"npm": ">=8.19.0"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "83fa79064b1d57e71149005432192d506810bbda"
|
|
48
|
+
}
|
package/src/_ascii.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/* istanbul ignore file */
|
|
25
|
+
|
|
26
|
+
import { isEmpty } from "./_isType";
|
|
27
|
+
|
|
28
|
+
/** ***************** latin-ascii conversions *******************/
|
|
29
|
+
const conversions = {
|
|
30
|
+
AE: "Æ|Ǽ",
|
|
31
|
+
Ae: "Ä",
|
|
32
|
+
ae: "ä|æ|ǽ",
|
|
33
|
+
OE: "Œ",
|
|
34
|
+
Oe: "Ö",
|
|
35
|
+
oe: "ö|œ",
|
|
36
|
+
Ue: "Ü",
|
|
37
|
+
ue: "ü",
|
|
38
|
+
IJ: "IJ",
|
|
39
|
+
ij: "ij",
|
|
40
|
+
ss: "ß",
|
|
41
|
+
A: "À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ",
|
|
42
|
+
a: "à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª",
|
|
43
|
+
C: "Ç|Ć|Ĉ|Ċ|Č",
|
|
44
|
+
c: "ç|ć|ĉ|ċ|č",
|
|
45
|
+
D: "Ð|Ď|Đ",
|
|
46
|
+
d: "ð|ď|đ",
|
|
47
|
+
E: "È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě",
|
|
48
|
+
e: "è|é|ê|ë|ē|ĕ|ė|ę|ě",
|
|
49
|
+
G: "Ĝ|Ğ|Ġ|Ģ",
|
|
50
|
+
g: "ĝ|ğ|ġ|ģ",
|
|
51
|
+
H: "Ĥ|Ħ",
|
|
52
|
+
h: "ĥ|ħ",
|
|
53
|
+
I: "Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ",
|
|
54
|
+
i: "ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı",
|
|
55
|
+
J: "Ĵ",
|
|
56
|
+
j: "ĵ",
|
|
57
|
+
K: "Ķ",
|
|
58
|
+
k: "ķ",
|
|
59
|
+
L: "Ĺ|Ļ|Ľ|Ŀ|Ł",
|
|
60
|
+
l: "ĺ|ļ|ľ|ŀ|ł",
|
|
61
|
+
N: "Ñ|Ń|Ņ|Ň",
|
|
62
|
+
n: "ñ|ń|ņ|ň|ʼn",
|
|
63
|
+
O: "Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ",
|
|
64
|
+
o: "ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º",
|
|
65
|
+
R: "Ŕ|Ŗ|Ř",
|
|
66
|
+
r: "ŕ|ŗ|ř",
|
|
67
|
+
S: "Ś|Ŝ|Ş|Š",
|
|
68
|
+
s: "ś|ŝ|ş|š|ſ",
|
|
69
|
+
T: "Ţ|Ť|Ŧ",
|
|
70
|
+
t: "ţ|ť|ŧ",
|
|
71
|
+
U: "Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ",
|
|
72
|
+
u: "ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ",
|
|
73
|
+
Y: "Ý|Ÿ|Ŷ",
|
|
74
|
+
y: "ý|ÿ|ŷ",
|
|
75
|
+
W: "Ŵ",
|
|
76
|
+
w: "ŵ",
|
|
77
|
+
Z: "Ź|Ż|Ž",
|
|
78
|
+
z: "ź|ż|ž",
|
|
79
|
+
f: "ƒ",
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* latin to ascii converter for convenient searching
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
export const ascii = (str: string) => {
|
|
87
|
+
if (isEmpty(str)) return "";
|
|
88
|
+
Object.entries(conversions).forEach(([a, l]) => {
|
|
89
|
+
const re = new RegExp(l, "g");
|
|
90
|
+
str = `${str}`.replace(re, a);
|
|
91
|
+
});
|
|
92
|
+
return str;
|
|
93
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { isEmpty } from "./_isType";
|
|
25
|
+
|
|
26
|
+
const _getBox = (
|
|
27
|
+
box: string | [number, number, number, number],
|
|
28
|
+
): [number, number, number, number] => {
|
|
29
|
+
let boundingBox: any = box;
|
|
30
|
+
if (isEmpty(box)) return [0, 0, 0, 0];
|
|
31
|
+
if (typeof box === "string") {
|
|
32
|
+
boundingBox = box.split(",");
|
|
33
|
+
}
|
|
34
|
+
if (Array.isArray(boundingBox)) {
|
|
35
|
+
boundingBox = boundingBox.map((b) => parseInt(b, 10));
|
|
36
|
+
}
|
|
37
|
+
return boundingBox;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* box "x,y,w,h" | [x,y,w,h]
|
|
42
|
+
* returns canvas box [x,y,w,h]
|
|
43
|
+
*/
|
|
44
|
+
export const getBox = (
|
|
45
|
+
box: string | [number, number, number, number] = "0,0,0,0",
|
|
46
|
+
) => {
|
|
47
|
+
const boundingBox = _getBox(box);
|
|
48
|
+
|
|
49
|
+
let [x, y, w, h] = boundingBox;
|
|
50
|
+
w = x < 0 ? w + x : w;
|
|
51
|
+
h = y < 0 ? h + y : h;
|
|
52
|
+
x = x > 0 ? x : 0;
|
|
53
|
+
y = y > 0 ? y : 0;
|
|
54
|
+
return [x, y, w, h] as [number, number, number, number];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* box "x1,y1,x2,y2" | [x1,y1,x2,y2]
|
|
59
|
+
* returns canvas box [x,y,w,h]
|
|
60
|
+
*/
|
|
61
|
+
export const getBoundingBox = (
|
|
62
|
+
box: string | [number, number, number, number] = "0,0,0,0",
|
|
63
|
+
) => {
|
|
64
|
+
const boundingBox = _getBox(box);
|
|
65
|
+
|
|
66
|
+
let [x1, y1, x2, y2] = boundingBox;
|
|
67
|
+
const w = x2 - x1;
|
|
68
|
+
const h = y2 - y1;
|
|
69
|
+
x1 = x1 > 0 ? x1 : 0;
|
|
70
|
+
y1 = y1 > 0 ? y1 : 0;
|
|
71
|
+
return [x1, y1, w, h] as [number, number, number, number];
|
|
72
|
+
};
|