Skip to content

Commit f4ab9ab

Browse files
committed
feat(string): hideString添加replacementLen选项,支持替换字符不等于实际字符数量
1 parent aac1b1b commit f4ab9ab

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/string.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,19 @@ export function getStringLen(value: string): number {
346346
* hideString('👨‍👨‍👧‍👦helloworld👨‍👨‍👧‍👦', { start: 1, end: -1 }); // '👨‍👨‍👧‍👦**********👨‍👨‍👧‍👦'
347347
* hideString('👨‍👨‍👧‍👦hello👨‍👨‍👧world👨‍👨‍👧‍👦', { start: 1, end: -1 }); // '👨‍👨‍👧‍👦***********👨‍👨‍👧‍👦'
348348
*
349+
* // 替换字符不对应实际字符数量
350+
* hideString('👨‍👨‍👧‍👦hello👨‍👨‍👧world👨‍👨‍👧‍👦', { start: -12, end: -1, replacementLen: 1 }); // '👨‍👨‍👧‍👦*👨‍👨‍👧‍👦'
351+
*
349352
* @param origin 原字符串
350353
* @param [options={}] 选项
351354
* @param [options.replacement='*'] 替换的字符串,默认为'*'
352355
* @param [options.start=0] 替换起始位置
353356
* @param [options.end] 替换结束位置 默认为原字符串长度
357+
* @param [options.replacementLen] 替换字符不对应实际字符数量
354358
*/
355359
export function hideString(
356360
origin: string,
357-
options?: { replacement?: string; start?: number; end?: number },
361+
options?: { replacement?: string; start?: number; end?: number; replacementLen?: number },
358362
): string;
359363
/**
360364
* 字符串遮掩部分或全部
@@ -383,15 +387,19 @@ export function hideString(
383387
* hideString('👨‍👨‍👧‍👦helloworld👨‍👨‍👧‍👦', { start: 1, len: 10 }); // '👨‍👨‍👧‍👦**********👨‍👨‍👧‍👦'
384388
* hideString('👨‍👨‍👧‍👦hello👨‍👨‍👧world👨‍👨‍👧‍👦', { start: 1, len: 11 }); // '👨‍👨‍👧‍👦***********👨‍👨‍👧‍👦'
385389
*
390+
* // 替换字符不对应实际字符数量
391+
* hideString('👨‍👨‍👧‍👦hello👨‍👨‍👧world👨‍👨‍👧‍👦', { start: 1, len: 11, replacementLen: 1 }); // '👨‍👨‍👧‍👦*👨‍👨‍👧‍👦'
392+
*
386393
* @param origin 原字符串
387394
* @param [options={}] 选项
388395
* @param [options.replacement='*'] 替换的字符串,默认为'*'
389396
* @param [options.start=0] 替换起始位置
390397
* @param [options.len] 替换文字长度 默认为原字符串长度
398+
* @param [options.replacementLen] 替换字符不对应实际字符数量
391399
*/
392400
export function hideString(
393401
origin: string,
394-
options?: { replacement?: string; start?: number; len?: number },
402+
options?: { replacement?: string; start?: number; len?: number; replacementLen?: number },
395403
): string;
396404
/**
397405
* 字符串遮掩部分或全部
@@ -416,24 +424,35 @@ export function hideString(
416424
* hideString('👨‍👨‍👧‍👦helloworld👨‍👨‍👧‍👦', { len: 10, end: -1 }); // '👨‍👨‍👧‍👦**********👨‍👨‍👧‍👦'
417425
* hideString('👨‍👨‍👧‍👦hello👨‍👨‍👧world👨‍👨‍👧‍👦', { len: 11, end: -1 }); // '👨‍👨‍👧‍👦***********👨‍👨‍👧‍👦'
418426
*
427+
* // 替换字符不对应实际字符数量
428+
* hideString('👨‍👨‍👧‍👦hello👨‍👨‍👧world👨‍👨‍👧‍👦', { len: 11, end: -1, replacementLen: 1 }); // '👨‍👨‍👧‍👦*👨‍👨‍👧‍👦'
429+
*
419430
* @param origin 原字符串
420431
* @param [options={}] 选项
421432
* @param [options.replacement='*'] 替换的字符串,默认为'*'
422433
* @param [options.len] 默认为原字符串长度
423434
* @param [options.end] 替换结束位置 默认为原字符串长度
435+
* @param [options.replacementLen] 替换字符不对应实际字符数量
424436
*/
425437
export function hideString(
426438
origin: string,
427-
options?: { replacement?: string; end?: number; len?: number },
439+
options?: { replacement?: string; end?: number; len?: number; replacementLen?: number },
428440
): string;
429441
export function hideString(
430442
origin: string,
431443
{
432444
replacement = '*',
445+
replacementLen = -1,
433446
start,
434447
end,
435448
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+
} = {},
437456
): string {
438457
const segmenter = new Intl.Segmenter('fr', { granularity: 'grapheme' });
439458
const wordList = Array.from(segmenter.segment(origin));
@@ -460,7 +479,9 @@ export function hideString(
460479
const [before, after] = [_before, _after].map((item) =>
461480
item.reduce((prev, cur) => prev + cur.segment, ''),
462481
);
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+
);
464485

465486
return before + center + after;
466487
}

0 commit comments

Comments
 (0)