@@ -346,15 +346,19 @@ export function getStringLen(value: string): number {
346
346
* hideString('👨👨👧👦helloworld👨👨👧👦', { start: 1, end: -1 }); // '👨👨👧👦**********👨👨👧👦'
347
347
* hideString('👨👨👧👦hello👨👨👧world👨👨👧👦', { start: 1, end: -1 }); // '👨👨👧👦***********👨👨👧👦'
348
348
*
349
+ * // 替换字符不对应实际字符数量
350
+ * hideString('👨👨👧👦hello👨👨👧world👨👨👧👦', { start: -12, end: -1, replacementLen: 1 }); // '👨👨👧👦*👨👨👧👦'
351
+ *
349
352
* @param origin 原字符串
350
353
* @param [options={ }] 选项
351
354
* @param [options.replacement='*'] 替换的字符串,默认为'*'
352
355
* @param [options.start=0] 替换起始位置
353
356
* @param [options.end] 替换结束位置 默认为原字符串长度
357
+ * @param [options.replacementLen] 替换字符不对应实际字符数量
354
358
*/
355
359
export function hideString (
356
360
origin : string ,
357
- options ?: { replacement ?: string ; start ?: number ; end ?: number } ,
361
+ options ?: { replacement ?: string ; start ?: number ; end ?: number ; replacementLen ?: number } ,
358
362
) : string ;
359
363
/**
360
364
* 字符串遮掩部分或全部
@@ -383,15 +387,19 @@ export function hideString(
383
387
* hideString('👨👨👧👦helloworld👨👨👧👦', { start: 1, len: 10 }); // '👨👨👧👦**********👨👨👧👦'
384
388
* hideString('👨👨👧👦hello👨👨👧world👨👨👧👦', { start: 1, len: 11 }); // '👨👨👧👦***********👨👨👧👦'
385
389
*
390
+ * // 替换字符不对应实际字符数量
391
+ * hideString('👨👨👧👦hello👨👨👧world👨👨👧👦', { start: 1, len: 11, replacementLen: 1 }); // '👨👨👧👦*👨👨👧👦'
392
+ *
386
393
* @param origin 原字符串
387
394
* @param [options={ }] 选项
388
395
* @param [options.replacement='*'] 替换的字符串,默认为'*'
389
396
* @param [options.start=0] 替换起始位置
390
397
* @param [options.len] 替换文字长度 默认为原字符串长度
398
+ * @param [options.replacementLen] 替换字符不对应实际字符数量
391
399
*/
392
400
export function hideString (
393
401
origin : string ,
394
- options ?: { replacement ?: string ; start ?: number ; len ?: number } ,
402
+ options ?: { replacement ?: string ; start ?: number ; len ?: number ; replacementLen ?: number } ,
395
403
) : string ;
396
404
/**
397
405
* 字符串遮掩部分或全部
@@ -416,24 +424,35 @@ export function hideString(
416
424
* hideString('👨👨👧👦helloworld👨👨👧👦', { len: 10, end: -1 }); // '👨👨👧👦**********👨👨👧👦'
417
425
* hideString('👨👨👧👦hello👨👨👧world👨👨👧👦', { len: 11, end: -1 }); // '👨👨👧👦***********👨👨👧👦'
418
426
*
427
+ * // 替换字符不对应实际字符数量
428
+ * hideString('👨👨👧👦hello👨👨👧world👨👨👧👦', { len: 11, end: -1, replacementLen: 1 }); // '👨👨👧👦*👨👨👧👦'
429
+ *
419
430
* @param origin 原字符串
420
431
* @param [options={ }] 选项
421
432
* @param [options.replacement='*'] 替换的字符串,默认为'*'
422
433
* @param [options.len] 默认为原字符串长度
423
434
* @param [options.end] 替换结束位置 默认为原字符串长度
435
+ * @param [options.replacementLen] 替换字符不对应实际字符数量
424
436
*/
425
437
export function hideString (
426
438
origin : string ,
427
- options ?: { replacement ?: string ; end ?: number ; len ?: number } ,
439
+ options ?: { replacement ?: string ; end ?: number ; len ?: number ; replacementLen ?: number } ,
428
440
) : string ;
429
441
export function hideString (
430
442
origin : string ,
431
443
{
432
444
replacement = '*' ,
445
+ replacementLen = - 1 ,
433
446
start,
434
447
end,
435
448
len,
436
- } : { replacement ?: string ; start ?: number ; end ?: number ; len ?: number } = { } ,
449
+ } : {
450
+ replacement ?: string ;
451
+ replacementLen ?: number ;
452
+ start ?: number ;
453
+ end ?: number ;
454
+ len ?: number ;
455
+ } = { } ,
437
456
) : string {
438
457
const segmenter = new Intl . Segmenter ( 'fr' , { granularity : 'grapheme' } ) ;
439
458
const wordList = Array . from ( segmenter . segment ( origin ) ) ;
@@ -460,7 +479,9 @@ export function hideString(
460
479
const [ before , after ] = [ _before , _after ] . map ( ( item ) =>
461
480
item . reduce ( ( prev , cur ) => prev + cur . segment , '' ) ,
462
481
) ;
463
- const center = replacement . repeat ( wordListLen - _before . length - _after . length ) ;
482
+ const center = replacement . repeat (
483
+ replacementLen !== - 1 ? replacementLen : wordListLen - _before . length - _after . length ,
484
+ ) ;
464
485
465
486
return before + center + after ;
466
487
}
0 commit comments