• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ialopezg / CommonJS / #23

03 Mar 2025 06:43PM UTC coverage: 91.254% (-2.7%) from 93.929%
#23

push

ialopezg
chore(release): 1.3.4

221 of 261 branches covered (84.67%)

Branch coverage included in aggregate %.

405 of 425 relevant lines covered (95.29%)

7.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/common/helpers/string.helper.ts
1
import { mask } from './string';
1✔
2
import { padWithChar } from './string';
1✔
3

4
export type MaskType =
5
  | 'email'
6
  | 'phone'
7
  | 'credit-card'
8
  | 'password'
9
  | 'ssn'
10
  | 'iban'
11
  | 'zip-code'
12
  | 'tax-id'
13
  | 'bank-account'
14
  | 'drivers-license'
15
  | string; // Allows custom mask characters
16

17
declare global {
18
  interface String {
19
    /**
20
     * Masks the current string based on the given masking type or custom mask character.
21
     *
22
     * @param {MaskType|string} maskCharOrType - The type of masking to apply or a custom mask character.
23
     * @param {number} visibleStart - Number of characters to remain visible at the beginning.
24
     * @param {number} visibleEnd - Number of characters to remain visible at the end.
25
     * @returns {string} A masked version of the string.
26
     */
27
    mask(
28
      maskCharOrType?: MaskType | string,
29
      visibleStart?: number,
30
      visibleEnd?: number,
31
    ): string;
32

33
    /**
34
     * Pads the current string with the specified character up to the given length.
35
     *
36
     * @param {string} padChar - The character to use for padding.
37
     * @param {number} [length] - The total desired length of the resulting string. Defaults to the current length.
38
     * @param {'left' | 'right'} [position='right'] - The side to apply padding ('left' or 'right'). Defaults to 'right'.
39
     * @returns {string} The padded string.
40
     *
41
     * @example
42
     * "42".padWithChar('*', 5, 'left');  // "***42"
43
     * "42".padWithChar('0', 4, 'right'); // "4200"
44
     */
45
    padWithChar(
46
      padChar: string,
47
      length?: number,
48
      position?: 'left' | 'right',
49
    ): string;
50
  }
51

52
  interface StringConstructor {
53
    /**
54
     * Masks a given string based on the specified masking type or custom character.
55
     *
56
     * @param input - The string to be masked.
57
     * @param maskCharOrType - The type of masking to apply or a custom mask character.
58
     * @param visibleStart - Number of characters to remain visible at the beginning.
59
     * @param visibleEnd - Number of characters to remain visible at the end.
60
     * @param locale - The locale to apply for formatting specific types (e.g., phone numbers).
61
     * @returns A masked version of the input string.
62
     */
63
    mask(
64
      input: string,
65
      maskCharOrType?: MaskType,
66
      visibleStart?: number,
67
      visibleEnd?: number,
68
      locale?: string,
69
    ): string;
70

71
    /**
72
     * Pads a string with the specified character up to the given length.
73
     *
74
     * @param {string} str - The string to pad.
75
     * @param {string} padChar - The character to use for padding.
76
     * @param {number} [length] - The total desired length of the resulting string. Defaults to the current length.
77
     * @param {'left' | 'right'} [position='right'] - The side to apply padding ('left' or 'right'). Defaults to 'right'.
78
     * @returns {string} The padded string.
79
     *
80
     * @example
81
     * String.padWithChar("42", '*', 5, 'left');  // "***42"
82
     * String.padWithChar("42", '0', 4, 'right'); // "4200"
83
     */
84
    padWithChar(
85
      str: string,
86
      padChar: string,
87
      length?: number,
88
      position?: 'left' | 'right',
89
    ): string;
90
  }
91
}
92

93
String.prototype.mask = function (
1✔
94
  maskCharOrType?: MaskType,
95
  visibleStart?: number,
96
  visibleEnd?: number,
97
): string {
98
  return mask(this as string, maskCharOrType, visibleStart, visibleEnd);
×
99
};
100

101
String.mask = mask;
1✔
102

103
String.prototype.padWithChar = function (
1✔
104
  padChar: string,
105
  length: number = 2,
1✔
106
  position: 'left' | 'right' = 'left',
37✔
107
): string {
108
  return padWithChar(this, padChar, length, position);
53✔
109
};
110
String.padWithChar = padWithChar;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc