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

CBIIT / crdc-datahub-ui / 11074479132

27 Sep 2024 04:44PM UTC coverage: 44.982% (+26.5%) from 18.435%
11074479132

Pull #479

github

web-flow
Merge a0867d25a into 3d8b55818
Pull Request #479: 3.0.0 Release

1727 of 4418 branches covered (39.09%)

Branch coverage included in aggregate %.

2612 of 5228 relevant lines covered (49.96%)

128.96 hits per line

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

85.44
/src/utils/stringUtils.ts
1
/**
2
 * Capitalizes the first letter of a given string.
3
 * If the input string is empty, it returns an empty string.
4
 *
5
 * @param {string} str - The string to capitalize.
6
 * @returns {string} - The capitalized string or an empty string if the input is empty.
7
 */
8
export const capitalizeFirstLetter = (str: string): string =>
64✔
9
  str ? str[0].toUpperCase() + str.slice(1) : "";
1,898✔
10

11
/**
12
 * Capitalizes the first letter of each word in a given string.
13
 *
14
 * @see Utilizes {@link capitalizeFirstLetter} to capitalize each word.
15
 * @param str - The string to capitalize.
16
 * @returns The capitalized string.
17
 */
18
export const titleCase = (str: string): string => {
64✔
19
  if (typeof str !== "string") {
1,716✔
20
    return "";
238✔
21
  }
22

23
  return str
1,478✔
24
    .toLowerCase()
25
    .split(" ")
26
    .map((word) => capitalizeFirstLetter(word))
1,530✔
27
    .join(" ");
28
};
29

30
/**
31
 * Function to add a space between a number and a letter in a string.
32
 * @param input - The input string to be processed. It should be a string where a number is directly followed by a letter.
33
 * @returns The processed string with a space between the number and the letter. If the input string does not match the required pattern, the function will return the original string.
34
 */
35
export const addSpace = (input: string): string => {
64✔
36
  // Regular expression to match a pattern where a number is directly followed by a letter
37
  const regex = /(\d+)([a-zA-Z]+)/g;
×
38

39
  if (!regex.test(input)) {
×
40
    return input;
×
41
  }
42

43
  // Replace the matched pattern with the same pattern but with a space in between
44
  return input.replace(regex, "$1 $2");
×
45
};
46

47
/**
48
 * Format a phone number string to (###) ###-####
49
 *
50
 * @param phoneNumber input phone number string
51
 * @returns formatted phone number or original phoneNumber string if invalid
52
 */
53
export const formatPhoneNumber = (phoneNumber: string): string => {
64✔
54
  // Remove all non-digits from the string
55
  const cleanNumber = phoneNumber.replace(/\D/g, "");
×
56

57
  // Ensure we have exactly 10 digits for a valid US phone number
58
  if (cleanNumber.length !== 10) {
×
59
    // If we don't, return the original string
60
    return phoneNumber;
×
61
  }
62

63
  // Use a regex to insert the formatting elements
64
  const formatted = cleanNumber.replace(/(\d{3})(\d{3})(\d{4})/, "($1)$2-$3");
×
65

66
  return formatted;
×
67
};
68

69
/**
70
 * Filters out all characters from a string except for alphanumeric characters.
71
 * Can be customized to allow additional characters
72
 *
73
 * @param {string} input The input string to be filtered
74
 * @param {string} [extraChars=""] Additional characters to allow in the filtered string
75
 * @returns {string} The filtered string containing only alphanumeric characters and any
76
 * additional allowed characters. Returns formatted string, otherwise if no value is passed,
77
 * then it returns an empty string
78
 */
79
export const filterAlphaNumeric = (input: string, extraChars = ""): string => {
64✔
80
  // The base regex matches alphanumeric characters.
81
  // We add the `extraChars` by splitting it into individual characters, escaping each one, and then joining with "|".
82
  // This ensures characters with special meaning in regex (like ".") are treated as literal characters.
83
  const pattern = new RegExp(
90✔
84
    `[^a-zA-Z0-9${extraChars
85
      .split("")
86
      .map((char) => `\\${char}`)
94✔
87
      .join("|")}]`,
88
    "g"
89
  );
90

91
  // We replace characters that don't match the allowed set with an empty string.
92
  return input?.replace(pattern, "") || "";
90✔
93
};
94

95
/**
96
 * Filters a string to allow only positive integers.
97
 *
98
 * @param {string} input The input string to be filtered
99
 * @returns {string} A string containing only positive integers, otherwise an empty string
100
 */
101
export const filterPositiveIntegerString = (input: string): string => {
64✔
102
  if (!input) {
28✔
103
    return "";
6✔
104
  }
105

106
  const nonIntegerPattern = /[^0-9]/g;
22✔
107
  const filtered = input.replace(nonIntegerPattern, "");
22✔
108

109
  // Remove leading zeros using a regular expression
110
  const noLeadingZeros = filtered.replace(/^0+/, "");
22✔
111

112
  return noLeadingZeros || "";
22✔
113
};
114

115
/**
116
 * Compares two string values for sorting in an array. Non-empty strings are sorted
117
 * alphabetically, and `null` or empty strings are placed at the beginning.
118
 *
119
 * @param {string | null} a - The first string to compare.
120
 * @param {string | null} b - The second string to compare.
121
 * @returns {number} - A negative number if `a` should come before `b`, a positive number
122
 * if `a` should come after `b`, or zero if they are considered equal for sorting purposes.
123
 */
124
export const compareStrings = (a: string | null, b: string | null): number => {
64✔
125
  if (a === b) return 0;
48✔
126
  if (!a || a === "") return -1;
42✔
127
  if (!b || b === "") return 1;
38✔
128

129
  return a.localeCompare(b);
34✔
130
};
131

132
/**
133
 * Moves the specified key element to the front of the array.
134
 * If the key element does not exist in the array, the original array is returned.
135
 *
136
 * @param {string[]} array - The array of strings to be reordered.
137
 * @param {string} keyElement - The key element to move to the front of the array.
138
 * @returns {string[]} A new array with the key element moved to the front if present.
139
 */
140
export const moveToFrontOfArray = (array: string[], keyElement: string): string[] => {
64✔
141
  if (!array?.length) {
44✔
142
    return [];
4✔
143
  }
144
  if (!keyElement?.length) {
40✔
145
    return array;
4✔
146
  }
147

148
  const index = array.indexOf(keyElement);
36✔
149

150
  // Return original array if keyElement is not found or already at the first position
151
  if (index <= 0) {
36✔
152
    return array;
34✔
153
  }
154

155
  const newArray = [...array];
2✔
156
  newArray.splice(index, 1);
2✔
157
  newArray.unshift(keyElement);
2✔
158

159
  return newArray;
2✔
160
};
161

162
/**
163
 * Rearranges the order of specified keys in an array and appends the remaining keys to the end.
164
 *
165
 * @param {string[]} keysArray - The array of keys to be processed.
166
 * @param {string[]} keyOrder - An array specifying the desired order of keys.
167
 * @returns {string[]} A new array with keys in the specified order, followed by the remaining keys.
168
 */
169
export const rearrangeKeys = (keysArray: string[], keyOrder: string[]): string[] => {
64✔
170
  if (!Array.isArray(keysArray) || !Array.isArray(keyOrder)) {
92✔
171
    return keysArray || [];
8✔
172
  }
173

174
  const orderedKeys = keyOrder.filter((key) => keysArray.includes(key));
166✔
175
  const remainingKeys = keysArray.filter((key) => !orderedKeys.includes(key));
230✔
176

177
  return [...orderedKeys, ...remainingKeys];
84✔
178
};
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