nhb-toolbox 2.8.0 → 2.8.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/dist/array/basics.js +7 -15
- package/dist/array/sort.js +1 -4
- package/dist/array/transform.js +4 -9
- package/dist/array/types.js +1 -2
- package/dist/colors/Color.js +17 -21
- package/dist/colors/constants.js +2 -5
- package/dist/colors/convert.js +56 -73
- package/dist/colors/helpers.js +13 -29
- package/dist/colors/initials.js +13 -16
- package/dist/colors/random.js +7 -12
- package/dist/colors/types.js +1 -2
- package/dist/form/convert.js +4 -9
- package/dist/form/guards.js +4 -10
- package/dist/form/transform.js +9 -13
- package/dist/form/types.js +1 -2
- package/dist/guards/non-primitives.js +17 -34
- package/dist/guards/primitives.js +12 -26
- package/dist/guards/specials.js +23 -37
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -147
- package/dist/number/basics.js +11 -21
- package/dist/number/constants.js +4 -7
- package/dist/number/convert.js +5 -8
- package/dist/number/helpers.js +11 -18
- package/dist/number/prime.js +3 -8
- package/dist/number/range.js +12 -15
- package/dist/number/types.js +1 -2
- package/dist/object/basics.js +8 -16
- package/dist/object/convert.js +1 -4
- package/dist/object/objectify.js +22 -32
- package/dist/object/sanitize.js +8 -11
- package/dist/object/types.js +1 -2
- package/dist/string/anagram.js +1 -4
- package/dist/string/basics.js +4 -11
- package/dist/string/constants.js +1 -4
- package/dist/string/convert.js +4 -9
- package/dist/string/types.js +1 -2
- package/dist/types/index.js +1 -2
- package/dist/utils/index.js +8 -15
- package/package.json +2 -1
package/dist/colors/helpers.js
CHANGED
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._extractAlphaColorValues = exports._extractSolidColorValues = exports._isSimilarToLast = exports._generateRandomHSL = exports._applyOpacity = exports._convertOpacityToHex = void 0;
|
|
4
|
-
exports._isHex6 = _isHex6;
|
|
5
|
-
exports._isRGB = _isRGB;
|
|
6
|
-
exports._isHSL = _isHSL;
|
|
7
|
-
exports._isHex8 = _isHex8;
|
|
8
|
-
exports._isRGBA = _isRGBA;
|
|
9
|
-
exports._isHSLA = _isHSLA;
|
|
10
|
-
exports._isValidAlpha = _isValidAlpha;
|
|
11
1
|
/**
|
|
12
2
|
* * Converts opacity percentage (0-100) to a 2-digit hex string.
|
|
13
3
|
*
|
|
14
4
|
* @param opacity - The opacity value as a percentage (0-100).
|
|
15
5
|
* @returns A 2-digit hex string representing the alpha value.
|
|
16
6
|
*/
|
|
17
|
-
const _convertOpacityToHex = (opacity) => {
|
|
7
|
+
export const _convertOpacityToHex = (opacity) => {
|
|
18
8
|
// Ensure opacity is between 0 and 100
|
|
19
9
|
const validOpacity = Math.min(100, Math.max(0, opacity));
|
|
20
10
|
// Convert to a value between 0 and 255, then to a hex string
|
|
@@ -22,29 +12,26 @@ const _convertOpacityToHex = (opacity) => {
|
|
|
22
12
|
// Ensure it's 2 digits (e.g., 0x0A instead of 0xA)
|
|
23
13
|
return alpha.toString(16).padStart(2, '0').toUpperCase();
|
|
24
14
|
};
|
|
25
|
-
exports._convertOpacityToHex = _convertOpacityToHex;
|
|
26
15
|
/**
|
|
27
16
|
* * Applies an opacity value to a color string.
|
|
28
17
|
* @param color The color string to apply opacity to.
|
|
29
18
|
* @param opacity The opacity value as a percentage (0-100).
|
|
30
19
|
* @returns The color string with the opacity value applied.
|
|
31
20
|
*/
|
|
32
|
-
const _applyOpacity = (color, opacity) => {
|
|
21
|
+
export const _applyOpacity = (color, opacity) => {
|
|
33
22
|
return color.concat(opacity);
|
|
34
23
|
};
|
|
35
|
-
exports._applyOpacity = _applyOpacity;
|
|
36
24
|
/**
|
|
37
25
|
* * Helper function to generate a random HSL color.
|
|
38
26
|
*
|
|
39
27
|
* @returns A random HSL color string.
|
|
40
28
|
*/
|
|
41
|
-
const _generateRandomHSL = () => {
|
|
29
|
+
export const _generateRandomHSL = () => {
|
|
42
30
|
const hue = Math.floor(Math.random() * 360);
|
|
43
31
|
const saturation = 75 + Math.floor(Math.random() * 25);
|
|
44
32
|
const lightness = 50 + Math.floor(Math.random() * 15);
|
|
45
33
|
return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
|
|
46
34
|
};
|
|
47
|
-
exports._generateRandomHSL = _generateRandomHSL;
|
|
48
35
|
/**
|
|
49
36
|
* * Helper function to check if the new color is visually too similar to the previous one.
|
|
50
37
|
* * It compares the hue, saturation, and lightness difference between the new color and the last one generated.
|
|
@@ -53,7 +40,7 @@ exports._generateRandomHSL = _generateRandomHSL;
|
|
|
53
40
|
* @param newColor - The new color to compare.
|
|
54
41
|
* @returns `Boolean` : `true` if the new color is similar to the previous one.
|
|
55
42
|
*/
|
|
56
|
-
const _isSimilarToLast = (recentColors, newColor) => {
|
|
43
|
+
export const _isSimilarToLast = (recentColors, newColor) => {
|
|
57
44
|
if (recentColors.length === 0)
|
|
58
45
|
return false;
|
|
59
46
|
const newHSL = newColor.match(/hsl\((\d+), (\d+)%, (\d+)%\)/);
|
|
@@ -73,7 +60,6 @@ const _isSimilarToLast = (recentColors, newColor) => {
|
|
|
73
60
|
saturationDifference < 24 &&
|
|
74
61
|
lightnessDifference < 16);
|
|
75
62
|
};
|
|
76
|
-
exports._isSimilarToLast = _isSimilarToLast;
|
|
77
63
|
/**
|
|
78
64
|
* * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`.
|
|
79
65
|
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
@@ -81,10 +67,9 @@ exports._isSimilarToLast = _isSimilarToLast;
|
|
|
81
67
|
* @param colorString The color string in RGB or HSL format.
|
|
82
68
|
* @returns An array of extracted numbers.
|
|
83
69
|
*/
|
|
84
|
-
const _extractSolidColorValues = (colorString) => {
|
|
70
|
+
export const _extractSolidColorValues = (colorString) => {
|
|
85
71
|
return (colorString.match(/[\d.]+%?/g) || []).map((value) => parseFloat(value));
|
|
86
72
|
};
|
|
87
|
-
exports._extractSolidColorValues = _extractSolidColorValues;
|
|
88
73
|
/**
|
|
89
74
|
* * Extracts numbers from a color string like `rgba(66, 103, 69, 0.6)` or `hsla(120, 42.86%, 41.18%, 0.9)`.
|
|
90
75
|
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
@@ -92,17 +77,16 @@ exports._extractSolidColorValues = _extractSolidColorValues;
|
|
|
92
77
|
* @param colorString The color string in RGB or HSL format.
|
|
93
78
|
* @returns An array of extracted numbers.
|
|
94
79
|
*/
|
|
95
|
-
const _extractAlphaColorValues = (colorString) => {
|
|
80
|
+
export const _extractAlphaColorValues = (colorString) => {
|
|
96
81
|
return (colorString.match(/[\d.]+%?/g) || []).map((value) => parseFloat(value));
|
|
97
82
|
};
|
|
98
|
-
exports._extractAlphaColorValues = _extractAlphaColorValues;
|
|
99
83
|
/**
|
|
100
84
|
* * Checks if a color is in `Hex` format.
|
|
101
85
|
*
|
|
102
86
|
* @param color Color to check.
|
|
103
87
|
* @returns Boolean: `true` if it's a `Hex` color, `false` if not.
|
|
104
88
|
*/
|
|
105
|
-
function _isHex6(color) {
|
|
89
|
+
export function _isHex6(color) {
|
|
106
90
|
return /^#[0-9A-Fa-f]{6}$/.test(color);
|
|
107
91
|
}
|
|
108
92
|
/**
|
|
@@ -111,7 +95,7 @@ function _isHex6(color) {
|
|
|
111
95
|
* @param color Color to check.
|
|
112
96
|
* @returns Boolean: `true` if it's an `RGB` color, `false` if not.
|
|
113
97
|
*/
|
|
114
|
-
function _isRGB(color) {
|
|
98
|
+
export function _isRGB(color) {
|
|
115
99
|
return /^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$/.test(color);
|
|
116
100
|
}
|
|
117
101
|
/**
|
|
@@ -120,7 +104,7 @@ function _isRGB(color) {
|
|
|
120
104
|
* @param color Color to check.
|
|
121
105
|
* @returns Boolean: `true` if it's an `HSL` color, `false` if not.
|
|
122
106
|
*/
|
|
123
|
-
function _isHSL(color) {
|
|
107
|
+
export function _isHSL(color) {
|
|
124
108
|
return /^hsl\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%\)$/.test(color);
|
|
125
109
|
}
|
|
126
110
|
/**
|
|
@@ -130,7 +114,7 @@ function _isHSL(color) {
|
|
|
130
114
|
* @param color Color to check.
|
|
131
115
|
* @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
|
|
132
116
|
*/
|
|
133
|
-
function _isHex8(color) {
|
|
117
|
+
export function _isHex8(color) {
|
|
134
118
|
return /^#[0-9A-Fa-f]{8}$/.test(color);
|
|
135
119
|
}
|
|
136
120
|
/**
|
|
@@ -140,7 +124,7 @@ function _isHex8(color) {
|
|
|
140
124
|
* @param color Color to check.
|
|
141
125
|
* @returns Boolean: `true` if it's an `RGBA` color, `false` if not.
|
|
142
126
|
*/
|
|
143
|
-
function _isRGBA(color) {
|
|
127
|
+
export function _isRGBA(color) {
|
|
144
128
|
return /^rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*(0|1|0?\.\d+)\)$/.test(color);
|
|
145
129
|
}
|
|
146
130
|
/**
|
|
@@ -150,7 +134,7 @@ function _isRGBA(color) {
|
|
|
150
134
|
* @param color Color to check.
|
|
151
135
|
* @returns Boolean: `true` if it's an `HSLA` color, `false` if not.
|
|
152
136
|
*/
|
|
153
|
-
function _isHSLA(color) {
|
|
137
|
+
export function _isHSLA(color) {
|
|
154
138
|
return /^hsla\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%,\s*(0|1|0?\.\d+)\)$/.test(color);
|
|
155
139
|
}
|
|
156
140
|
/**
|
|
@@ -158,6 +142,6 @@ function _isHSLA(color) {
|
|
|
158
142
|
* @param value Alpha value to check.
|
|
159
143
|
* @returns Boolean: `true` if it's a valid alpha value, `false` if not.
|
|
160
144
|
*/
|
|
161
|
-
function _isValidAlpha(value) {
|
|
145
|
+
export function _isValidAlpha(value) {
|
|
162
146
|
return value >= 0 && value <= 1 && !isNaN(value);
|
|
163
147
|
}
|
package/dist/colors/initials.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.getColorForInitial = getColorForInitial;
|
|
4
|
-
const constants_1 = require("./constants");
|
|
5
|
-
const helpers_1 = require("./helpers");
|
|
1
|
+
import { alphabetColorPalette, numberColorPalette } from './constants';
|
|
2
|
+
import { _applyOpacity, _convertOpacityToHex } from './helpers';
|
|
6
3
|
/**
|
|
7
4
|
* * Generates a hex color based on the first character (initial) of a string or number; or an array of hex colors based on the first character (initial) of an array of strings/numbers or even nested arrays of strings/numbers.
|
|
8
5
|
*
|
|
@@ -14,40 +11,40 @@ const helpers_1 = require("./helpers");
|
|
|
14
11
|
* @param opacity - A value from 0 to 100 representing the opacity percentage.
|
|
15
12
|
* @returns A hex color for a string/number, or an array of hex colors for each element of the provided array.
|
|
16
13
|
*/
|
|
17
|
-
function getColorForInitial(input = '', opacity = 100) {
|
|
14
|
+
export function getColorForInitial(input = '', opacity = 100) {
|
|
18
15
|
let initial;
|
|
19
|
-
const hexOpacity =
|
|
16
|
+
const hexOpacity = _convertOpacityToHex(opacity);
|
|
20
17
|
const numbers = '0123456789';
|
|
21
18
|
// Handle empty string case
|
|
22
19
|
if (!input)
|
|
23
|
-
return
|
|
20
|
+
return _applyOpacity('#010514', hexOpacity);
|
|
24
21
|
// Handle string input
|
|
25
22
|
if (typeof input === 'string') {
|
|
26
23
|
initial = input[0];
|
|
27
24
|
// Handle number as string
|
|
28
25
|
if (numbers.includes(initial)) {
|
|
29
|
-
return
|
|
26
|
+
return _applyOpacity(numberColorPalette[parseInt(initial, 10)], hexOpacity);
|
|
30
27
|
}
|
|
31
28
|
const upperInitial = initial.toUpperCase();
|
|
32
29
|
const index = upperInitial.charCodeAt(0) - 'A'.charCodeAt(0);
|
|
33
30
|
// Validate alphabet
|
|
34
|
-
if (index >= 0 && index <
|
|
35
|
-
return
|
|
31
|
+
if (index >= 0 && index < alphabetColorPalette.length) {
|
|
32
|
+
return _applyOpacity(alphabetColorPalette[index], hexOpacity);
|
|
36
33
|
}
|
|
37
|
-
return
|
|
34
|
+
return _applyOpacity('#010514', hexOpacity);
|
|
38
35
|
// Handle number input
|
|
39
36
|
}
|
|
40
37
|
else if (typeof input === 'number' && !isNaN(input)) {
|
|
41
38
|
initial = input.toString()[0];
|
|
42
39
|
if (numbers.includes(initial)) {
|
|
43
|
-
return
|
|
40
|
+
return _applyOpacity(numberColorPalette[parseInt(initial, 10)], hexOpacity);
|
|
44
41
|
}
|
|
45
|
-
return
|
|
42
|
+
return _applyOpacity('#010514', hexOpacity);
|
|
46
43
|
// Handle array of strings/numbers
|
|
47
44
|
}
|
|
48
45
|
else if (Array.isArray(input)) {
|
|
49
46
|
if (input.length < 1)
|
|
50
|
-
return [...
|
|
47
|
+
return [...alphabetColorPalette, ...numberColorPalette].map((color) => _applyOpacity(color, hexOpacity));
|
|
51
48
|
return input
|
|
52
49
|
.map((el) => {
|
|
53
50
|
if (Array.isArray(el)) {
|
|
@@ -57,5 +54,5 @@ function getColorForInitial(input = '', opacity = 100) {
|
|
|
57
54
|
})
|
|
58
55
|
.flat();
|
|
59
56
|
}
|
|
60
|
-
return
|
|
57
|
+
return _applyOpacity('#010514', hexOpacity);
|
|
61
58
|
}
|
package/dist/colors/random.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.generateRandomColorInHexRGB = exports.generateRandomHSLColor = void 0;
|
|
4
|
-
const convert_1 = require("./convert");
|
|
5
|
-
const helpers_1 = require("./helpers");
|
|
1
|
+
import { convertColorCode } from './convert';
|
|
2
|
+
import { _generateRandomHSL, _isSimilarToLast } from './helpers';
|
|
6
3
|
/** Track previously generated colors. */
|
|
7
4
|
const generatedColors = new Set();
|
|
8
5
|
/** Array of recently generated colors */
|
|
@@ -13,13 +10,13 @@ const recentColors = [];
|
|
|
13
10
|
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
|
|
14
11
|
* @returns Generated unique random color in `HSL` format.
|
|
15
12
|
*/
|
|
16
|
-
const generateRandomHSLColor = (maxColors = 16) => {
|
|
13
|
+
export const generateRandomHSLColor = (maxColors = 16) => {
|
|
17
14
|
let color;
|
|
18
15
|
// Keep generating until a unique color is found that is also different from the last one
|
|
19
16
|
do {
|
|
20
|
-
color =
|
|
17
|
+
color = _generateRandomHSL();
|
|
21
18
|
} while (generatedColors.has(color) ||
|
|
22
|
-
|
|
19
|
+
_isSimilarToLast(recentColors, color));
|
|
23
20
|
// Add the newly generated color to the set and recent colors
|
|
24
21
|
generatedColors.add(color);
|
|
25
22
|
recentColors.push(color);
|
|
@@ -29,14 +26,12 @@ const generateRandomHSLColor = (maxColors = 16) => {
|
|
|
29
26
|
}
|
|
30
27
|
return color;
|
|
31
28
|
};
|
|
32
|
-
exports.generateRandomHSLColor = generateRandomHSLColor;
|
|
33
29
|
/**
|
|
34
30
|
* * Utility to generate a unique random color in Hex and RGB format.
|
|
35
31
|
*
|
|
36
32
|
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
|
|
37
33
|
* @returns An object of generated unique random color in both `Hex` and `RGB` formats.
|
|
38
34
|
*/
|
|
39
|
-
const generateRandomColorInHexRGB = (maxColors = 16) => {
|
|
40
|
-
return
|
|
35
|
+
export const generateRandomColorInHexRGB = (maxColors = 16) => {
|
|
36
|
+
return convertColorCode(generateRandomHSLColor(maxColors));
|
|
41
37
|
};
|
|
42
|
-
exports.generateRandomColorInHexRGB = generateRandomColorInHexRGB;
|
package/dist/colors/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/form/convert.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmptyFormData = exports.convertIntoFormData = void 0;
|
|
4
|
-
const basics_1 = require("../array/basics");
|
|
1
|
+
import { isInvalidOrEmptyArray } from '../array/basics';
|
|
5
2
|
/**
|
|
6
3
|
* * Utility to convert object into FormData.
|
|
7
4
|
*
|
|
8
5
|
* @param data Data to convert into FormData.
|
|
9
6
|
* @returns Converted FormData.
|
|
10
7
|
*/
|
|
11
|
-
const convertIntoFormData = (data) => {
|
|
8
|
+
export const convertIntoFormData = (data) => {
|
|
12
9
|
const formData = new FormData();
|
|
13
10
|
Object.entries(data).forEach(([key, value]) => {
|
|
14
|
-
if (!
|
|
11
|
+
if (!isInvalidOrEmptyArray(value) && value[0]?.originFileObj) {
|
|
15
12
|
formData.append(key, value[0].originFileObj);
|
|
16
13
|
}
|
|
17
14
|
else if (value !== undefined && value !== null && value !== '') {
|
|
@@ -20,17 +17,15 @@ const convertIntoFormData = (data) => {
|
|
|
20
17
|
});
|
|
21
18
|
return formData;
|
|
22
19
|
};
|
|
23
|
-
exports.convertIntoFormData = convertIntoFormData;
|
|
24
20
|
/**
|
|
25
21
|
* * Check if a formdata object is empty.
|
|
26
22
|
*
|
|
27
23
|
* @param data FormData to check.
|
|
28
24
|
* @returns Boolean (`true`/`false`) Whether the formdata is empty.
|
|
29
25
|
*/
|
|
30
|
-
const isEmptyFormData = (data) => {
|
|
26
|
+
export const isEmptyFormData = (data) => {
|
|
31
27
|
if ('entries' in data && typeof data.entries === 'function') {
|
|
32
28
|
return Array.from(data.entries()).length === 0;
|
|
33
29
|
}
|
|
34
30
|
throw new Error('`FormData.entries()` is not supported in this environment!');
|
|
35
31
|
};
|
|
36
|
-
exports.isEmptyFormData = isEmptyFormData;
|
package/dist/form/guards.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isOriginFileObj = isOriginFileObj;
|
|
4
|
-
exports.isCustomFile = isCustomFile;
|
|
5
|
-
exports.isCustomFileArray = isCustomFileArray;
|
|
6
|
-
exports.isFileUpload = isFileUpload;
|
|
7
1
|
/**
|
|
8
2
|
* * Checks if a given value is an `OriginFileObj`.
|
|
9
3
|
* @param value - The value to check.
|
|
10
4
|
* @returns `true` if the value is a valid `OriginFileObj`, otherwise `false`.
|
|
11
5
|
*/
|
|
12
|
-
function isOriginFileObj(value) {
|
|
6
|
+
export function isOriginFileObj(value) {
|
|
13
7
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
14
8
|
return false;
|
|
15
9
|
}
|
|
@@ -21,7 +15,7 @@ function isOriginFileObj(value) {
|
|
|
21
15
|
* @param value - The value to check.
|
|
22
16
|
* @returns `true` if the value is a valid `CustomFile`, otherwise `false`.
|
|
23
17
|
*/
|
|
24
|
-
function isCustomFile(value) {
|
|
18
|
+
export function isCustomFile(value) {
|
|
25
19
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
26
20
|
return false;
|
|
27
21
|
}
|
|
@@ -33,7 +27,7 @@ function isCustomFile(value) {
|
|
|
33
27
|
* @param value - The value to check.
|
|
34
28
|
* @returns `true` if the value is a valid `CustomFile[]`, otherwise `false`.
|
|
35
29
|
*/
|
|
36
|
-
function isCustomFileArray(value) {
|
|
30
|
+
export function isCustomFileArray(value) {
|
|
37
31
|
return (Array.isArray(value) && value.length > 0 && value.every(isCustomFile));
|
|
38
32
|
}
|
|
39
33
|
/**
|
|
@@ -41,7 +35,7 @@ function isCustomFileArray(value) {
|
|
|
41
35
|
* @param value - The value to check.
|
|
42
36
|
* @returns `true` if the value is a valid `FileUpload`, otherwise `false`.
|
|
43
37
|
*/
|
|
44
|
-
function isFileUpload(value) {
|
|
38
|
+
export function isFileUpload(value) {
|
|
45
39
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
46
40
|
return false;
|
|
47
41
|
}
|
package/dist/form/transform.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const basics_1 = require("../array/basics");
|
|
5
|
-
const basics_2 = require("../object/basics");
|
|
6
|
-
const guards_1 = require("./guards");
|
|
1
|
+
import { isInvalidOrEmptyArray } from '../array/basics';
|
|
2
|
+
import { isEmptyObject } from '../object/basics';
|
|
3
|
+
import { isCustomFile, isCustomFileArray, isFileUpload } from './guards';
|
|
7
4
|
/**
|
|
8
5
|
* * Utility to convert object into FormData in a controlled way.
|
|
9
6
|
*
|
|
@@ -12,7 +9,7 @@ const guards_1 = require("./guards");
|
|
|
12
9
|
*
|
|
13
10
|
* @returns FormData instance containing the sanitized and transformed data
|
|
14
11
|
*/
|
|
15
|
-
const createControlledFormData = (data, configs) => {
|
|
12
|
+
export const createControlledFormData = (data, configs) => {
|
|
16
13
|
const formData = new FormData();
|
|
17
14
|
const { stringifyNested = '*' } = configs || {};
|
|
18
15
|
/** - Helper function to check if a key matches a dotNotation path to preserve. */
|
|
@@ -39,15 +36,15 @@ const createControlledFormData = (data, configs) => {
|
|
|
39
36
|
configs?.lowerCaseKeys?.includes(key)) ?
|
|
40
37
|
key.toLowerCase()
|
|
41
38
|
: key;
|
|
42
|
-
if (
|
|
39
|
+
if (isCustomFileArray(value)) {
|
|
43
40
|
value.forEach((file) => formData.append(transformedKey, file.originFileObj));
|
|
44
41
|
}
|
|
45
|
-
else if (
|
|
42
|
+
else if (isFileUpload(value)) {
|
|
46
43
|
if (value.fileList) {
|
|
47
44
|
value.fileList.forEach((file) => formData.append(transformedKey, file.originFileObj));
|
|
48
45
|
}
|
|
49
46
|
else if (value.file) {
|
|
50
|
-
if (
|
|
47
|
+
if (isCustomFile(value.file)) {
|
|
51
48
|
formData.append(transformedKey, value.file.originFileObj);
|
|
52
49
|
}
|
|
53
50
|
else {
|
|
@@ -58,7 +55,7 @@ const createControlledFormData = (data, configs) => {
|
|
|
58
55
|
else if (value instanceof Blob || value instanceof File) {
|
|
59
56
|
formData.append(transformedKey, value);
|
|
60
57
|
}
|
|
61
|
-
else if (Array.isArray(value) && !
|
|
58
|
+
else if (Array.isArray(value) && !isInvalidOrEmptyArray(value)) {
|
|
62
59
|
if (shouldBreakArray(key)) {
|
|
63
60
|
value.forEach((item, index) => {
|
|
64
61
|
_addToFormData(`${transformedKey}[${index}]`, item);
|
|
@@ -70,7 +67,7 @@ const createControlledFormData = (data, configs) => {
|
|
|
70
67
|
}
|
|
71
68
|
else if (typeof value === 'object' &&
|
|
72
69
|
value !== null &&
|
|
73
|
-
!
|
|
70
|
+
!isEmptyObject(value)) {
|
|
74
71
|
if (shouldStringify(key) && !shouldDotNotate(key)) {
|
|
75
72
|
formData.append(transformedKey, JSON.stringify(value));
|
|
76
73
|
}
|
|
@@ -123,4 +120,3 @@ const createControlledFormData = (data, configs) => {
|
|
|
123
120
|
_processObject(data);
|
|
124
121
|
return formData;
|
|
125
122
|
};
|
|
126
|
-
exports.createControlledFormData = createControlledFormData;
|
package/dist/form/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,27 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isArray = isArray;
|
|
4
|
-
exports.isObject = isObject;
|
|
5
|
-
exports.isFunction = isFunction;
|
|
6
|
-
exports.isDate = isDate;
|
|
7
|
-
exports.isObjectWithKeys = isObjectWithKeys;
|
|
8
|
-
exports.isEmptyObject = isEmptyObject;
|
|
9
|
-
exports.isArrayOfType = isArrayOfType;
|
|
10
|
-
exports.isPromise = isPromise;
|
|
11
|
-
exports.isSet = isSet;
|
|
12
|
-
exports.isMap = isMap;
|
|
13
|
-
exports.isRegExp = isRegExp;
|
|
14
|
-
exports.isError = isError;
|
|
15
|
-
exports.isBigInt = isBigInt;
|
|
16
|
-
exports.isJSON = isJSON;
|
|
17
|
-
exports.isReturningPromise = isReturningPromise;
|
|
18
|
-
const primitives_1 = require("./primitives");
|
|
1
|
+
import { isString } from './primitives';
|
|
19
2
|
/**
|
|
20
3
|
* Type guard to check if a value is an array.
|
|
21
4
|
* @param value - The value to check.
|
|
22
5
|
* @returns `true` if the value is an array, otherwise `false`.
|
|
23
6
|
*/
|
|
24
|
-
function isArray(value) {
|
|
7
|
+
export function isArray(value) {
|
|
25
8
|
return Array.isArray(value);
|
|
26
9
|
}
|
|
27
10
|
/**
|
|
@@ -29,7 +12,7 @@ function isArray(value) {
|
|
|
29
12
|
* @param value - The value to check.
|
|
30
13
|
* @returns `true` if the value is an object, otherwise `false`.
|
|
31
14
|
*/
|
|
32
|
-
function isObject(value) {
|
|
15
|
+
export function isObject(value) {
|
|
33
16
|
return value !== null && typeof value === 'object' && !isArray(value);
|
|
34
17
|
}
|
|
35
18
|
/**
|
|
@@ -37,7 +20,7 @@ function isObject(value) {
|
|
|
37
20
|
* @param value - The value to check.
|
|
38
21
|
* @returns `true` if the value is a function, otherwise `false`.
|
|
39
22
|
*/
|
|
40
|
-
function isFunction(value) {
|
|
23
|
+
export function isFunction(value) {
|
|
41
24
|
return typeof value === 'function';
|
|
42
25
|
}
|
|
43
26
|
/**
|
|
@@ -45,7 +28,7 @@ function isFunction(value) {
|
|
|
45
28
|
* @param value - The value to check.
|
|
46
29
|
* @returns `true` if the value is a Date object, otherwise `false`.
|
|
47
30
|
*/
|
|
48
|
-
function isDate(value) {
|
|
31
|
+
export function isDate(value) {
|
|
49
32
|
return value instanceof Date;
|
|
50
33
|
}
|
|
51
34
|
/**
|
|
@@ -54,7 +37,7 @@ function isDate(value) {
|
|
|
54
37
|
* @param keys - The set of keys the object should contain.
|
|
55
38
|
* @returns `true` if the value is an object with the specified keys, otherwise `false`.
|
|
56
39
|
*/
|
|
57
|
-
function isObjectWithKeys(value, keys) {
|
|
40
|
+
export function isObjectWithKeys(value, keys) {
|
|
58
41
|
return isObject(value) && keys.every((key) => key in value);
|
|
59
42
|
}
|
|
60
43
|
/**
|
|
@@ -62,7 +45,7 @@ function isObjectWithKeys(value, keys) {
|
|
|
62
45
|
* @param value - The value to check.
|
|
63
46
|
* @returns `true` if the value is an empty object, otherwise `false`.
|
|
64
47
|
*/
|
|
65
|
-
function isEmptyObject(value) {
|
|
48
|
+
export function isEmptyObject(value) {
|
|
66
49
|
return isObject(value) && Object.keys(value).length === 0;
|
|
67
50
|
}
|
|
68
51
|
/**
|
|
@@ -71,7 +54,7 @@ function isEmptyObject(value) {
|
|
|
71
54
|
* @param typeCheck - The type guard function to check each item of the array.
|
|
72
55
|
* @returns `true` if the value is an array of the specified type, otherwise `false`.
|
|
73
56
|
*/
|
|
74
|
-
function isArrayOfType(value, typeCheck) {
|
|
57
|
+
export function isArrayOfType(value, typeCheck) {
|
|
75
58
|
return isArray(value) && value.every(typeCheck);
|
|
76
59
|
}
|
|
77
60
|
/**
|
|
@@ -79,7 +62,7 @@ function isArrayOfType(value, typeCheck) {
|
|
|
79
62
|
* @param value - The value to check.
|
|
80
63
|
* @returns `true` if the value is a Promise, otherwise `false`.
|
|
81
64
|
*/
|
|
82
|
-
function isPromise(value) {
|
|
65
|
+
export function isPromise(value) {
|
|
83
66
|
return isObject(value) && isFunction(value.then);
|
|
84
67
|
}
|
|
85
68
|
/**
|
|
@@ -87,7 +70,7 @@ function isPromise(value) {
|
|
|
87
70
|
* @param value - The value to check.
|
|
88
71
|
* @returns `true` if the value is a Set, otherwise `false`.
|
|
89
72
|
*/
|
|
90
|
-
function isSet(value) {
|
|
73
|
+
export function isSet(value) {
|
|
91
74
|
return value instanceof Set;
|
|
92
75
|
}
|
|
93
76
|
/**
|
|
@@ -95,7 +78,7 @@ function isSet(value) {
|
|
|
95
78
|
* @param value - The value to check.
|
|
96
79
|
* @returns `true` if the value is a Map, otherwise `false`.
|
|
97
80
|
*/
|
|
98
|
-
function isMap(value) {
|
|
81
|
+
export function isMap(value) {
|
|
99
82
|
return value instanceof Map;
|
|
100
83
|
}
|
|
101
84
|
/**
|
|
@@ -103,7 +86,7 @@ function isMap(value) {
|
|
|
103
86
|
* @param value - The value to check.
|
|
104
87
|
* @returns `true` if the value is a RegExp, otherwise `false`.
|
|
105
88
|
*/
|
|
106
|
-
function isRegExp(value) {
|
|
89
|
+
export function isRegExp(value) {
|
|
107
90
|
return value instanceof RegExp;
|
|
108
91
|
}
|
|
109
92
|
/**
|
|
@@ -111,7 +94,7 @@ function isRegExp(value) {
|
|
|
111
94
|
* @param value - The value to check.
|
|
112
95
|
* @returns `true` if the value is an Error object, otherwise `false`.
|
|
113
96
|
*/
|
|
114
|
-
function isError(value) {
|
|
97
|
+
export function isError(value) {
|
|
115
98
|
return value instanceof Error;
|
|
116
99
|
}
|
|
117
100
|
/**
|
|
@@ -119,7 +102,7 @@ function isError(value) {
|
|
|
119
102
|
* @param value - The value to check.
|
|
120
103
|
* @returns `true` if the value is a BigInt, otherwise `false`.
|
|
121
104
|
*/
|
|
122
|
-
function isBigInt(value) {
|
|
105
|
+
export function isBigInt(value) {
|
|
123
106
|
return typeof value === 'bigint';
|
|
124
107
|
}
|
|
125
108
|
/**
|
|
@@ -127,8 +110,8 @@ function isBigInt(value) {
|
|
|
127
110
|
* @param value - The value to check.
|
|
128
111
|
* @returns `true` if the value is valid JSON, otherwise `false`.
|
|
129
112
|
*/
|
|
130
|
-
function isJSON(value) {
|
|
131
|
-
if (!
|
|
113
|
+
export function isJSON(value) {
|
|
114
|
+
if (!isString(value))
|
|
132
115
|
return false;
|
|
133
116
|
try {
|
|
134
117
|
JSON.parse(value);
|
|
@@ -143,6 +126,6 @@ function isJSON(value) {
|
|
|
143
126
|
* @param fn - The function to check.
|
|
144
127
|
* @returns `true` if the function returns a Promise, otherwise `false`.
|
|
145
128
|
*/
|
|
146
|
-
function isReturningPromise(fn) {
|
|
129
|
+
export function isReturningPromise(fn) {
|
|
147
130
|
return isFunction(fn) && fn.constructor.name === 'AsyncFunction';
|
|
148
131
|
}
|