nhb-toolbox 2.8.1 → 2.8.3
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.d.ts +2 -2
- package/dist/colors/initials.d.ts.map +1 -1
- 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.d.ts +10 -10
- package/dist/guards/non-primitives.js +27 -44
- package/dist/guards/primitives.d.ts +12 -12
- package/dist/guards/primitives.js +24 -38
- package/dist/guards/specials.d.ts +12 -12
- package/dist/guards/specials.js +35 -49
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -156
- 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
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ColorInputArray, OpacityValue } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Generates a hex color based on the first character (initial) of a string or number.
|
|
4
4
|
*
|
|
@@ -9,7 +9,7 @@ import type { ColorInput, ColorInputArray, OpacityValue } from './types';
|
|
|
9
9
|
* @param opacity - A value from 0 to 100 representing the opacity percentage.
|
|
10
10
|
* @returns A hex color for the first character of the provided string/number.
|
|
11
11
|
*/
|
|
12
|
-
export declare function getColorForInitial(input:
|
|
12
|
+
export declare function getColorForInitial(input: string | number, opacity?: OpacityValue): string;
|
|
13
13
|
/**
|
|
14
14
|
* * Generates an array of hex colors based on the first character (initial) of an array of strings/numbers or even nested arrays of strings/numbers.
|
|
15
15
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initials.d.ts","sourceRoot":"","sources":["../../src/colors/initials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"initials.d.ts","sourceRoot":"","sources":["../../src/colors/initials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAc,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEzE;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,OAAO,CAAC,EAAE,YAAY,GACpB,MAAM,CAAC;AAEV;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,eAAe,EACtB,OAAO,CAAC,EAAE,YAAY,GACpB,MAAM,EAAE,CAAC"}
|
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 {};
|
|
@@ -31,62 +31,62 @@ export declare function isDate(value: unknown): value is Date;
|
|
|
31
31
|
*/
|
|
32
32
|
export declare function isObjectWithKeys<T extends Record<string, unknown>>(value: unknown, keys: (keyof T)[]): value is T;
|
|
33
33
|
/**
|
|
34
|
-
* Type guard to check if a value is an empty object.
|
|
34
|
+
* * Type guard to check if a value is an empty object.
|
|
35
35
|
* @param value - The value to check.
|
|
36
36
|
* @returns `true` if the value is an empty object, otherwise `false`.
|
|
37
37
|
*/
|
|
38
38
|
export declare function isEmptyObject(value: unknown): value is Record<string, unknown>;
|
|
39
39
|
/**
|
|
40
|
-
* Type guard to check if a value is an array of a specific type.
|
|
40
|
+
* * Type guard to check if a value is an array of a specific type.
|
|
41
41
|
* @param value - The value to check.
|
|
42
42
|
* @param typeCheck - The type guard function to check each item of the array.
|
|
43
43
|
* @returns `true` if the value is an array of the specified type, otherwise `false`.
|
|
44
44
|
*/
|
|
45
45
|
export declare function isArrayOfType<T>(value: unknown, typeCheck: (item: unknown) => item is T): value is T[];
|
|
46
46
|
/**
|
|
47
|
-
* Type guard to check if a value is a Promise.
|
|
47
|
+
* * Type guard to check if a value is a Promise.
|
|
48
48
|
* @param value - The value to check.
|
|
49
49
|
* @returns `true` if the value is a Promise, otherwise `false`.
|
|
50
50
|
*/
|
|
51
51
|
export declare function isPromise(value: unknown): value is Promise<unknown>;
|
|
52
52
|
/**
|
|
53
|
-
* Type guard to check if a value is a Set.
|
|
53
|
+
* * Type guard to check if a value is a Set.
|
|
54
54
|
* @param value - The value to check.
|
|
55
55
|
* @returns `true` if the value is a Set, otherwise `false`.
|
|
56
56
|
*/
|
|
57
57
|
export declare function isSet<T>(value: unknown): value is Set<T>;
|
|
58
58
|
/**
|
|
59
|
-
* Type guard to check if a value is a Map.
|
|
59
|
+
* * Type guard to check if a value is a Map.
|
|
60
60
|
* @param value - The value to check.
|
|
61
61
|
* @returns `true` if the value is a Map, otherwise `false`.
|
|
62
62
|
*/
|
|
63
63
|
export declare function isMap<K, V>(value: unknown): value is Map<K, V>;
|
|
64
64
|
/**
|
|
65
|
-
* Type guard to check if a value is a RegExp.
|
|
65
|
+
* * Type guard to check if a value is a RegExp.
|
|
66
66
|
* @param value - The value to check.
|
|
67
67
|
* @returns `true` if the value is a RegExp, otherwise `false`.
|
|
68
68
|
*/
|
|
69
69
|
export declare function isRegExp(value: unknown): value is RegExp;
|
|
70
70
|
/**
|
|
71
|
-
* Type guard to check if a value is an Error object.
|
|
71
|
+
* * Type guard to check if a value is an Error object.
|
|
72
72
|
* @param value - The value to check.
|
|
73
73
|
* @returns `true` if the value is an Error object, otherwise `false`.
|
|
74
74
|
*/
|
|
75
75
|
export declare function isError(value: unknown): value is Error;
|
|
76
76
|
/**
|
|
77
|
-
* Type guard to check if a value is a BigInt.
|
|
77
|
+
* * Type guard to check if a value is a BigInt.
|
|
78
78
|
* @param value - The value to check.
|
|
79
79
|
* @returns `true` if the value is a BigInt, otherwise `false`.
|
|
80
80
|
*/
|
|
81
81
|
export declare function isBigInt(value: unknown): value is bigint;
|
|
82
82
|
/**
|
|
83
|
-
* Type guard to check if a string is valid JSON.
|
|
83
|
+
* * Type guard to check if a string is valid JSON.
|
|
84
84
|
* @param value - The value to check.
|
|
85
85
|
* @returns `true` if the value is valid JSON, otherwise `false`.
|
|
86
86
|
*/
|
|
87
87
|
export declare function isJSON(value: unknown): value is string;
|
|
88
88
|
/**
|
|
89
|
-
* Type guard to check if a function returns a Promise.
|
|
89
|
+
* * Type guard to check if a function returns a Promise.
|
|
90
90
|
* @param fn - The function to check.
|
|
91
91
|
* @returns `true` if the function returns a Promise, otherwise `false`.
|
|
92
92
|
*/
|