@oscarpalmer/atoms 0.182.0 → 0.183.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/dist/index.d.mts +10 -0
- package/dist/index.mjs +11 -2
- package/dist/string/normalize.d.mts +10 -0
- package/dist/string/normalize.mjs +11 -2
- package/package.json +9 -1
- package/src/string/normalize.ts +31 -1
package/dist/index.d.mts
CHANGED
|
@@ -3170,10 +3170,20 @@ type NormalizeOptions = {
|
|
|
3170
3170
|
* Convert the string to lower case? _(defaults to `true`)_
|
|
3171
3171
|
*/
|
|
3172
3172
|
lowerCase?: boolean;
|
|
3173
|
+
/**
|
|
3174
|
+
* Remove special characters from the string? _(defaults to `false`)_
|
|
3175
|
+
*
|
|
3176
|
+
* _(Punctuation and symbol characters are considered special)_
|
|
3177
|
+
*/
|
|
3178
|
+
special?: boolean;
|
|
3173
3179
|
/**
|
|
3174
3180
|
* Trim the string? _(defaults to `true`)_
|
|
3175
3181
|
*/
|
|
3176
3182
|
trim?: boolean;
|
|
3183
|
+
/**
|
|
3184
|
+
* Shorten consecutive whitespace characters to a single space? _(defaults to `true`)_
|
|
3185
|
+
*/
|
|
3186
|
+
whitespace?: boolean;
|
|
3177
3187
|
};
|
|
3178
3188
|
/**
|
|
3179
3189
|
* String normalizer function
|
package/dist/index.mjs
CHANGED
|
@@ -2939,7 +2939,9 @@ function getNormalizeOptions(input) {
|
|
|
2939
2939
|
return {
|
|
2940
2940
|
deburr: options.deburr !== false,
|
|
2941
2941
|
lowerCase: options.lowerCase !== false,
|
|
2942
|
-
|
|
2942
|
+
special: options.special === true,
|
|
2943
|
+
trim: options.trim !== false,
|
|
2944
|
+
whitespace: options.whitespace !== false
|
|
2943
2945
|
};
|
|
2944
2946
|
}
|
|
2945
2947
|
/**
|
|
@@ -2967,9 +2969,11 @@ function normalizeString(value, options) {
|
|
|
2967
2969
|
if (typeof value !== "string") return "";
|
|
2968
2970
|
let result = value;
|
|
2969
2971
|
if (options.trim) result = result.trim();
|
|
2972
|
+
if (options.whitespace) result = result.replace(WHITESPACE_PATTERN, WHITESPACE_REPLACEMENT);
|
|
2970
2973
|
if (options.deburr) result = deburr(result);
|
|
2971
2974
|
if (options.lowerCase) result = lowerCase(result);
|
|
2972
|
-
|
|
2975
|
+
if (options.special) result = result.replace(SPECIAL_PATTERN, SPECIAL_REPLACEMENT);
|
|
2976
|
+
return result.normalize(NORMALIZATION_NORMALIZATION);
|
|
2973
2977
|
}
|
|
2974
2978
|
const DEBURR_CHARACTERS = {
|
|
2975
2979
|
Æ: "AE",
|
|
@@ -3006,6 +3010,11 @@ const DEBURR_CHARACTERS = {
|
|
|
3006
3010
|
const DEBURR_NORMALIZATION = "NFD";
|
|
3007
3011
|
const DEBURR_PATTERN_CHARACTERS = new RegExp(`(${Object.keys(DEBURR_CHARACTERS).join("|")})`, "g");
|
|
3008
3012
|
const DEBURR_PATTERN_SIMPLE = /[\u0300-\u036f]/g;
|
|
3013
|
+
const NORMALIZATION_NORMALIZATION = "NFC";
|
|
3014
|
+
const SPECIAL_PATTERN = /[\p{P}\p{S}]/gu;
|
|
3015
|
+
const SPECIAL_REPLACEMENT = "";
|
|
3016
|
+
const WHITESPACE_PATTERN = /\s+/g;
|
|
3017
|
+
const WHITESPACE_REPLACEMENT = " ";
|
|
3009
3018
|
let deburrMemoizer;
|
|
3010
3019
|
//#endregion
|
|
3011
3020
|
//#region src/string/template.ts
|
|
@@ -11,10 +11,20 @@ type NormalizeOptions = {
|
|
|
11
11
|
* Convert the string to lower case? _(defaults to `true`)_
|
|
12
12
|
*/
|
|
13
13
|
lowerCase?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Remove special characters from the string? _(defaults to `false`)_
|
|
16
|
+
*
|
|
17
|
+
* _(Punctuation and symbol characters are considered special)_
|
|
18
|
+
*/
|
|
19
|
+
special?: boolean;
|
|
14
20
|
/**
|
|
15
21
|
* Trim the string? _(defaults to `true`)_
|
|
16
22
|
*/
|
|
17
23
|
trim?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Shorten consecutive whitespace characters to a single space? _(defaults to `true`)_
|
|
26
|
+
*/
|
|
27
|
+
whitespace?: boolean;
|
|
18
28
|
};
|
|
19
29
|
/**
|
|
20
30
|
* String normalizer function
|
|
@@ -21,7 +21,9 @@ function getNormalizeOptions(input) {
|
|
|
21
21
|
return {
|
|
22
22
|
deburr: options.deburr !== false,
|
|
23
23
|
lowerCase: options.lowerCase !== false,
|
|
24
|
-
|
|
24
|
+
special: options.special === true,
|
|
25
|
+
trim: options.trim !== false,
|
|
26
|
+
whitespace: options.whitespace !== false
|
|
25
27
|
};
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
@@ -49,9 +51,11 @@ function normalizeString(value, options) {
|
|
|
49
51
|
if (typeof value !== "string") return "";
|
|
50
52
|
let result = value;
|
|
51
53
|
if (options.trim) result = result.trim();
|
|
54
|
+
if (options.whitespace) result = result.replace(WHITESPACE_PATTERN, WHITESPACE_REPLACEMENT);
|
|
52
55
|
if (options.deburr) result = deburr(result);
|
|
53
56
|
if (options.lowerCase) result = lowerCase(result);
|
|
54
|
-
|
|
57
|
+
if (options.special) result = result.replace(SPECIAL_PATTERN, SPECIAL_REPLACEMENT);
|
|
58
|
+
return result.normalize(NORMALIZATION_NORMALIZATION);
|
|
55
59
|
}
|
|
56
60
|
const DEBURR_CHARACTERS = {
|
|
57
61
|
Æ: "AE",
|
|
@@ -88,6 +92,11 @@ const DEBURR_CHARACTERS = {
|
|
|
88
92
|
const DEBURR_NORMALIZATION = "NFD";
|
|
89
93
|
const DEBURR_PATTERN_CHARACTERS = new RegExp(`(${Object.keys(DEBURR_CHARACTERS).join("|")})`, "g");
|
|
90
94
|
const DEBURR_PATTERN_SIMPLE = /[\u0300-\u036f]/g;
|
|
95
|
+
const NORMALIZATION_NORMALIZATION = "NFC";
|
|
96
|
+
const SPECIAL_PATTERN = /[\p{P}\p{S}]/gu;
|
|
97
|
+
const SPECIAL_REPLACEMENT = "";
|
|
98
|
+
const WHITESPACE_PATTERN = /\s+/g;
|
|
99
|
+
const WHITESPACE_REPLACEMENT = " ";
|
|
91
100
|
let deburrMemoizer;
|
|
92
101
|
//#endregion
|
|
93
102
|
export { deburr, initializeNormalizer, normalize };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.183.0",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"helper",
|
|
@@ -105,6 +105,10 @@
|
|
|
105
105
|
"types": "./dist/is.d.mts",
|
|
106
106
|
"default": "./dist/is.mjs"
|
|
107
107
|
},
|
|
108
|
+
"./kalas": {
|
|
109
|
+
"types": "./dist/kalas.d.mts",
|
|
110
|
+
"default": "./dist/kalas.mjs"
|
|
111
|
+
},
|
|
108
112
|
"./logger": {
|
|
109
113
|
"types": "./dist/logger.d.mts",
|
|
110
114
|
"default": "./dist/logger.mjs"
|
|
@@ -200,6 +204,10 @@
|
|
|
200
204
|
"types": "./dist/string/match.d.mts",
|
|
201
205
|
"default": "./dist/string/match.mjs"
|
|
202
206
|
},
|
|
207
|
+
"./string/normalize": {
|
|
208
|
+
"types": "./dist/string/normalize.d.mts",
|
|
209
|
+
"default": "./dist/string/normalize.mjs"
|
|
210
|
+
},
|
|
203
211
|
"./string/template": {
|
|
204
212
|
"types": "./dist/string/template.d.mts",
|
|
205
213
|
"default": "./dist/string/template.mjs"
|
package/src/string/normalize.ts
CHANGED
|
@@ -16,10 +16,20 @@ export type NormalizeOptions = {
|
|
|
16
16
|
* Convert the string to lower case? _(defaults to `true`)_
|
|
17
17
|
*/
|
|
18
18
|
lowerCase?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Remove special characters from the string? _(defaults to `false`)_
|
|
21
|
+
*
|
|
22
|
+
* _(Punctuation and symbol characters are considered special)_
|
|
23
|
+
*/
|
|
24
|
+
special?: boolean;
|
|
19
25
|
/**
|
|
20
26
|
* Trim the string? _(defaults to `true`)_
|
|
21
27
|
*/
|
|
22
28
|
trim?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Shorten consecutive whitespace characters to a single space? _(defaults to `true`)_
|
|
31
|
+
*/
|
|
32
|
+
whitespace?: boolean;
|
|
23
33
|
};
|
|
24
34
|
|
|
25
35
|
/**
|
|
@@ -70,7 +80,9 @@ function getNormalizeOptions(input?: NormalizeOptions): Options {
|
|
|
70
80
|
return {
|
|
71
81
|
deburr: options.deburr !== false,
|
|
72
82
|
lowerCase: options.lowerCase !== false,
|
|
83
|
+
special: options.special === true,
|
|
73
84
|
trim: options.trim !== false,
|
|
85
|
+
whitespace: options.whitespace !== false,
|
|
74
86
|
};
|
|
75
87
|
}
|
|
76
88
|
|
|
@@ -110,6 +122,10 @@ function normalizeString(value: string, options: Options): string {
|
|
|
110
122
|
result = result.trim();
|
|
111
123
|
}
|
|
112
124
|
|
|
125
|
+
if (options.whitespace) {
|
|
126
|
+
result = result.replace(WHITESPACE_PATTERN, WHITESPACE_REPLACEMENT);
|
|
127
|
+
}
|
|
128
|
+
|
|
113
129
|
if (options.deburr) {
|
|
114
130
|
result = deburr(result);
|
|
115
131
|
}
|
|
@@ -118,7 +134,11 @@ function normalizeString(value: string, options: Options): string {
|
|
|
118
134
|
result = lowerCase(result);
|
|
119
135
|
}
|
|
120
136
|
|
|
121
|
-
|
|
137
|
+
if (options.special) {
|
|
138
|
+
result = result.replace(SPECIAL_PATTERN, SPECIAL_REPLACEMENT);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return result.normalize(NORMALIZATION_NORMALIZATION);
|
|
122
142
|
}
|
|
123
143
|
|
|
124
144
|
// #endregion
|
|
@@ -164,6 +184,16 @@ const DEBURR_PATTERN_CHARACTERS = new RegExp(`(${Object.keys(DEBURR_CHARACTERS).
|
|
|
164
184
|
|
|
165
185
|
const DEBURR_PATTERN_SIMPLE = /[\u0300-\u036f]/g;
|
|
166
186
|
|
|
187
|
+
const NORMALIZATION_NORMALIZATION = 'NFC';
|
|
188
|
+
|
|
189
|
+
const SPECIAL_PATTERN = /[\p{P}\p{S}]/gu;
|
|
190
|
+
|
|
191
|
+
const SPECIAL_REPLACEMENT = '';
|
|
192
|
+
|
|
193
|
+
const WHITESPACE_PATTERN = /\s+/g;
|
|
194
|
+
|
|
195
|
+
const WHITESPACE_REPLACEMENT = ' ';
|
|
196
|
+
|
|
167
197
|
let deburrMemoizer: Memoized<typeof deburr>;
|
|
168
198
|
|
|
169
199
|
// #endregion
|