topkat-utils 1.2.49 → 1.2.50
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ObjectGeneric } from
|
|
1
|
+
import { ObjectGeneric } from './types';
|
|
2
2
|
/**Eg: camelCase */
|
|
3
3
|
export declare function camelCase(...wordBits: string[] | [string[]]): string;
|
|
4
4
|
/**Eg: snake_case
|
|
@@ -18,7 +18,7 @@ export declare function titleCase(...wordBits: string[] | [string[]]): string;
|
|
|
18
18
|
export declare function upperCase(...wordBits: string[] | [string[]]): string;
|
|
19
19
|
/**Eg: lowercase undefined, null... => '' */
|
|
20
20
|
export declare function lowerCase(...wordBits: string[] | [string[]]): string;
|
|
21
|
-
export declare function capitalize1st(str?: string): string;
|
|
21
|
+
export declare function capitalize1st(str?: string, lowercaseTheRest?: boolean): string;
|
|
22
22
|
export declare function camelCaseToWords(str: string): string[];
|
|
23
23
|
/** GIVEN A STRING '{ blah;2}, ['nested,(what,ever)']' AND A SEPARATOR ",""
|
|
24
24
|
* This will return the content separated by first level of separators
|
package/dist/src/string-utils.js
CHANGED
|
@@ -10,7 +10,7 @@ const getWordBits = (wb) => Array.isArray(wb[0]) ? wb[0] : wb;
|
|
|
10
10
|
/**Eg: camelCase */
|
|
11
11
|
function camelCase(...wordBits) {
|
|
12
12
|
const wordBitsReal = getWordBits(wordBits);
|
|
13
|
-
return wordBitsReal.filter(e => e).map((w, i) => i === 0 ? w : capitalize1st(w)).join('');
|
|
13
|
+
return wordBitsReal.filter(e => e).map((w, i) => i === 0 ? w : capitalize1st(w, true)).join('');
|
|
14
14
|
}
|
|
15
15
|
exports.camelCase = camelCase;
|
|
16
16
|
/**Eg: snake_case
|
|
@@ -33,13 +33,13 @@ exports.kebabCase = kebabCase;
|
|
|
33
33
|
/**Eg: PascalCase undefined, null... => '' */
|
|
34
34
|
function pascalCase(...wordBits) {
|
|
35
35
|
const wordBitsReal = getWordBits(wordBits);
|
|
36
|
-
return wordBitsReal.filter(e => e).map(
|
|
36
|
+
return wordBitsReal.filter(e => e).map(w => capitalize1st(w, true)).join('');
|
|
37
37
|
}
|
|
38
38
|
exports.pascalCase = pascalCase;
|
|
39
39
|
/**Eg: Titlecase undefined, null... => '' */
|
|
40
40
|
function titleCase(...wordBits) {
|
|
41
41
|
const wordBitsReal = getWordBits(wordBits);
|
|
42
|
-
return capitalize1st(wordBitsReal.filter(e => e).map(w => w.trim()).join(''));
|
|
42
|
+
return capitalize1st(wordBitsReal.filter(e => e).map(w => w.trim()).join(''), true);
|
|
43
43
|
}
|
|
44
44
|
exports.titleCase = titleCase;
|
|
45
45
|
/**Eg: UPPERCASE undefined, null... => '' */
|
|
@@ -54,7 +54,7 @@ function lowerCase(...wordBits) {
|
|
|
54
54
|
return wordBitsReal.filter(e => e).map(w => w.trim().toLowerCase()).join('');
|
|
55
55
|
}
|
|
56
56
|
exports.lowerCase = lowerCase;
|
|
57
|
-
function capitalize1st(str = '') { return str ? str[0].toUpperCase() + str.slice(1) : str; }
|
|
57
|
+
function capitalize1st(str = '', lowercaseTheRest = false) { return str ? str[0].toUpperCase() + (lowercaseTheRest ? str.toLowerCase() : str).slice(1) : str; }
|
|
58
58
|
exports.capitalize1st = capitalize1st;
|
|
59
59
|
function camelCaseToWords(str) {
|
|
60
60
|
return str ? str.trim().replace(/([A-Z])/g, '-$1').toLowerCase().split('-') : [];
|
|
@@ -168,7 +168,7 @@ function convertAccentedCharacters(str, config = {}) {
|
|
|
168
168
|
}
|
|
169
169
|
exports.convertAccentedCharacters = convertAccentedCharacters;
|
|
170
170
|
let generatedTokens = []; // cache to avoid collision
|
|
171
|
-
let lastTs =
|
|
171
|
+
let lastTs = Date.now();
|
|
172
172
|
/** minLength 8 if unique
|
|
173
173
|
* @param {Number} length default: 20
|
|
174
174
|
* @param {Boolean} unique default: true. Generate a real unique token base on the date. min length will be min 8 in this case
|
|
@@ -176,19 +176,21 @@ let lastTs = new Date().getTime();
|
|
|
176
176
|
* NOTE: to generate a mongoDB Random Id, use the params: 24, true, 'hexadecimal'
|
|
177
177
|
*/
|
|
178
178
|
function generateToken(length = 20, unique = true, mode = 'alphanumeric') {
|
|
179
|
-
|
|
179
|
+
const charConvNumeric = mode === 'alphanumeric' ? 36 : 16;
|
|
180
180
|
if (unique && length < 8)
|
|
181
181
|
length = 8;
|
|
182
182
|
let token;
|
|
183
183
|
let tokenTs;
|
|
184
184
|
do {
|
|
185
|
-
tokenTs =
|
|
185
|
+
tokenTs = Date.now();
|
|
186
186
|
token = unique ? tokenTs.toString(charConvNumeric) : '';
|
|
187
187
|
while (token.length < length)
|
|
188
188
|
token += Math.random().toString(charConvNumeric).substr(2, 1); // char alphaNumeric aléatoire
|
|
189
189
|
} while (generatedTokens.includes(token));
|
|
190
|
-
if (lastTs < tokenTs)
|
|
190
|
+
if (lastTs < tokenTs) {
|
|
191
191
|
generatedTokens = []; // reset generated token on new timestamp because cannot collide
|
|
192
|
+
lastTs = Date.now();
|
|
193
|
+
}
|
|
192
194
|
generatedTokens.push(token);
|
|
193
195
|
return token;
|
|
194
196
|
}
|
|
@@ -237,7 +239,8 @@ function nbOccurenceInString(baseString, searchedString, allowOverlapping = fals
|
|
|
237
239
|
return baseString.length + 1;
|
|
238
240
|
let n = 0;
|
|
239
241
|
let pos = 0;
|
|
240
|
-
|
|
242
|
+
const step = allowOverlapping ? 1 : searchedString.length;
|
|
243
|
+
// eslint-disable-next-line no-constant-condition
|
|
241
244
|
while (true) {
|
|
242
245
|
pos = baseString.indexOf(searchedString, pos);
|
|
243
246
|
if (pos >= 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-utils.js","sourceRoot":"","sources":["../../src/string-utils.ts"],"names":[],"mappings":";;;AAAA,0CAA0C;AAC1C,eAAe;AACf,0CAA0C;AAC1C,+CAAqD;AAErD,mCAA+B;AAE/B,MAAM,WAAW,GAAG,CAAC,EAAyB,EAAY,EAAE,CAA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AE;QACT,GAAG,GAAG,UAAU,CAAC,OAAO,CAAC,cAAc,EAAE,GAAG,CAAC,CAAA;QAC7C,IAAI,GAAG,IAAI,CAAC,EAAE;YACV,EAAE,CAAC,CAAA;YACH,GAAG,IAAI,IAAI,CAAA;SACd;;YAAM,MAAK;KACf;IACD,OAAO,CAAC,CAAA;AACZ,CAAC;AAjBD,kDAiBC"}
|
package/package.json
CHANGED
package/src/string-utils.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
//----------------------------------------
|
|
2
2
|
// STRING UTILS
|
|
3
3
|
//----------------------------------------
|
|
4
|
-
import { err500IfEmptyOrNotSet } from
|
|
5
|
-
import { ObjectGeneric } from
|
|
6
|
-
import { isset } from
|
|
4
|
+
import { err500IfEmptyOrNotSet } from './error-utils'
|
|
5
|
+
import { ObjectGeneric } from './types'
|
|
6
|
+
import { isset } from './isset'
|
|
7
7
|
|
|
8
8
|
const getWordBits = (wb: string[] | [string[]]): string[] => Array.isArray(wb[0]) ? wb[0] : wb as any
|
|
9
9
|
|
|
10
10
|
/**Eg: camelCase */
|
|
11
11
|
export function camelCase(...wordBits: string[] | [string[]]): string {
|
|
12
12
|
const wordBitsReal = getWordBits(wordBits)
|
|
13
|
-
return wordBitsReal.filter(e => e).map((w, i) => i === 0 ? w : capitalize1st(w)).join('')
|
|
13
|
+
return wordBitsReal.filter(e => e).map((w, i) => i === 0 ? w : capitalize1st(w, true)).join('')
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**Eg: snake_case
|
|
@@ -31,13 +31,13 @@ export function kebabCase(...wordBits: string[] | [string[]]): string {
|
|
|
31
31
|
/**Eg: PascalCase undefined, null... => '' */
|
|
32
32
|
export function pascalCase(...wordBits: string[] | [string[]]): string {
|
|
33
33
|
const wordBitsReal = getWordBits(wordBits)
|
|
34
|
-
return wordBitsReal.filter(e => e).map(
|
|
34
|
+
return wordBitsReal.filter(e => e).map(w => capitalize1st(w, true)).join('')
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**Eg: Titlecase undefined, null... => '' */
|
|
38
38
|
export function titleCase(...wordBits: string[] | [string[]]): string {
|
|
39
39
|
const wordBitsReal = getWordBits(wordBits)
|
|
40
|
-
return capitalize1st(wordBitsReal.filter(e => e).map(w => w.trim()).join(''))
|
|
40
|
+
return capitalize1st(wordBitsReal.filter(e => e).map(w => w.trim()).join(''), true)
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**Eg: UPPERCASE undefined, null... => '' */
|
|
@@ -52,7 +52,7 @@ export function lowerCase(...wordBits: string[] | [string[]]): string {
|
|
|
52
52
|
return wordBitsReal.filter(e => e).map(w => w.trim().toLowerCase()).join('')
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
export function capitalize1st(str = ''): string { return str ? str[0].toUpperCase() + str.slice(1) : str }
|
|
55
|
+
export function capitalize1st(str = '', lowercaseTheRest = false): string { return str ? str[0].toUpperCase() + (lowercaseTheRest ? str.toLowerCase() : str).slice(1) : str }
|
|
56
56
|
|
|
57
57
|
export function camelCaseToWords(str: string): string[] {
|
|
58
58
|
return str ? str.trim().replace(/([A-Z])/g, '-$1').toLowerCase().split('-') : []
|
|
@@ -155,32 +155,35 @@ export function convertAccentedCharacters(str : string, config: { removeNumbers?
|
|
|
155
155
|
.replace(/[ÔÖ]/g, 'O')
|
|
156
156
|
.replace(/[ùúû]/g, 'u')
|
|
157
157
|
.replace(/[ÙÚÛ]/g, 'U')
|
|
158
|
-
if (config.removeNumbers === true) output = output.replace(/\d+/g, '')
|
|
159
|
-
if (config.removeSpecialChars === true) output = output.replace(/[^A-Za-z0-9 ]/g, '')
|
|
160
|
-
if (config.removeSpaces === true) output = output.replace(/\s+/g, '')
|
|
161
|
-
return output
|
|
158
|
+
if (config.removeNumbers === true) output = output.replace(/\d+/g, '')
|
|
159
|
+
if (config.removeSpecialChars === true) output = output.replace(/[^A-Za-z0-9 ]/g, '')
|
|
160
|
+
if (config.removeSpaces === true) output = output.replace(/\s+/g, '')
|
|
161
|
+
return output
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
|
|
165
165
|
let generatedTokens: string[] = [] // cache to avoid collision
|
|
166
|
-
let lastTs =
|
|
167
|
-
/** minLength 8 if unique
|
|
166
|
+
let lastTs = Date.now()
|
|
167
|
+
/** minLength 8 if unique
|
|
168
168
|
* @param {Number} length default: 20
|
|
169
169
|
* @param {Boolean} unique default: true. Generate a real unique token base on the date. min length will be min 8 in this case
|
|
170
170
|
* @param {string} mode one of ['alphanumeric', 'hexadecimal']
|
|
171
171
|
* NOTE: to generate a mongoDB Random Id, use the params: 24, true, 'hexadecimal'
|
|
172
172
|
*/
|
|
173
173
|
export function generateToken(length = 20, unique = true, mode: 'alphanumeric' | 'hexadecimal' = 'alphanumeric') {
|
|
174
|
-
|
|
174
|
+
const charConvNumeric = mode === 'alphanumeric' ? 36 : 16
|
|
175
175
|
if (unique && length < 8) length = 8
|
|
176
176
|
let token
|
|
177
177
|
let tokenTs
|
|
178
178
|
do {
|
|
179
|
-
tokenTs =
|
|
179
|
+
tokenTs = Date.now()
|
|
180
180
|
token = unique ? tokenTs.toString(charConvNumeric) : ''
|
|
181
181
|
while (token.length < length) token += Math.random().toString(charConvNumeric).substr(2, 1) // char alphaNumeric aléatoire
|
|
182
182
|
} while (generatedTokens.includes(token))
|
|
183
|
-
if (lastTs < tokenTs)
|
|
183
|
+
if (lastTs < tokenTs) {
|
|
184
|
+
generatedTokens = [] // reset generated token on new timestamp because cannot collide
|
|
185
|
+
lastTs = Date.now()
|
|
186
|
+
}
|
|
184
187
|
generatedTokens.push(token)
|
|
185
188
|
return token
|
|
186
189
|
}
|
|
@@ -217,9 +220,9 @@ export type MiniTemplaterOptions = {
|
|
|
217
220
|
valueWhenContentUndefined: string
|
|
218
221
|
}
|
|
219
222
|
/** Replace variables in a string like: `Hello {{userName}}!`
|
|
220
|
-
* @param {String} content
|
|
223
|
+
* @param {String} content
|
|
221
224
|
* @param {Object} varz object with key => value === toReplace => replacer
|
|
222
|
-
* @param {Object} options
|
|
225
|
+
* @param {Object} options
|
|
223
226
|
* * valueWhenNotSet => replacer for undefined values. Default: ''
|
|
224
227
|
* * regexp => must be 'g' and first capturing group matching the value to replace. Default: /{{\s*([^}]*)\s*}}/g
|
|
225
228
|
*/
|
|
@@ -243,14 +246,15 @@ export function nbOccurenceInString(baseString: string, searchedString:string, a
|
|
|
243
246
|
|
|
244
247
|
let n = 0
|
|
245
248
|
let pos = 0
|
|
246
|
-
|
|
249
|
+
const step = allowOverlapping ? 1 : searchedString.length
|
|
247
250
|
|
|
251
|
+
// eslint-disable-next-line no-constant-condition
|
|
248
252
|
while (true) {
|
|
249
|
-
pos = baseString.indexOf(searchedString, pos)
|
|
253
|
+
pos = baseString.indexOf(searchedString, pos)
|
|
250
254
|
if (pos >= 0) {
|
|
251
|
-
++n
|
|
252
|
-
pos += step
|
|
253
|
-
} else break
|
|
255
|
+
++n
|
|
256
|
+
pos += step
|
|
257
|
+
} else break
|
|
254
258
|
}
|
|
255
|
-
return n
|
|
259
|
+
return n
|
|
256
260
|
}
|