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

Yrkki / cv-generator-fe / #526106776

pending completion
#526106776

push

travis-pro

Yrkki
ci(update): bump dependencies

680 of 711 branches covered (95.64%)

Branch coverage included in aggregate %.

3017 of 3074 relevant lines covered (98.15%)

533.09 hits per line

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

97.87
/src/app/services/string-ex/string-ex.service.ts
1
// SPDX-License-Identifier: Apache-2.0
2
// Copyright (c) 2018 Georgi Marinov
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//     http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//
16
import { Injectable } from '@angular/core';
17

18
/**
19
 * String processing utility functions service
20
 */
21
@Injectable({
22
  providedIn: 'root'
23
})
24
export class StringExService {
1✔
25
  /**
26
   * Replace all occurrences of a string within another string with a third string.
27
   *
28
   * @param str String to replace occcurrences in.
29
   * @param search String to find.
30
   * @param replacement String to replace the found search string occurrences with.
31
   *
32
   * @returns String having all occurrences of the search string replaced with a replacement string.
33
   */
34
  static replaceAll(str: string, search: string | RegExp, replacement: any): string {
35
    return str.replace(new RegExp(search, 'g'), replacement);
24,265✔
36
  }
37

38
  /**
39
   * Capitalize a string to title case.
40
   *
41
   * @param str String to capitalize.
42
   *
43
   * @returns The string capitalized.
44
   */
45
  static capitalize(str: string): string {
46
    return str.length > 0 ? str[0].toUpperCase() + str.substring(1) : str;
677✔
47
  }
48

49
  /**
50
   * Convert a string to title case.
51
   *
52
   * @param str String to turn into title case.
53
   *
54
   * @returns The string converted into title case.
55
   */
56
  // eslint-disable-next-line max-lines-per-function
57
  static toTitleCase(str: string | undefined): string {
58
    if (!str) { return ''; }
2,672✔
59

60
    let i: number;
61
    let j: number;
62
    str = str.replace(new RegExp('([^\\W_]+[^\\s-]*) *', 'g'),
320✔
63
      (txt) => txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase());
657✔
64

65
    // Certain minor words should be left lowercase unless
66
    // they are the first or last words in the string
67
    const lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At',
320✔
68
      'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
69
    for (i = 0, j = lowers.length; i < j; i++) {
320✔
70
      str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'),
6,720✔
71
        (txt) => txt.toLowerCase());
×
72
    }
73

74
    // Certain words such as initialisms or acronyms should be left uppercase
75
    const uppers = ['Cv', 'Icb', 'Id', 'Tv'];
320✔
76
    for (i = 0, j = uppers.length; i < j; i++) {
320✔
77
      str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'),
1,280✔
78
        uppers[i].toUpperCase());
79
    }
80

81
    return str;
320✔
82
  }
83

84
  /**
85
   * Convert a string to Pascal case.
86
   *
87
   * @param str String to turn into Pascal case.
88
   *
89
   * @returns The string converted into Pascal case.
90
   */
91
  static toPascalCase(str: string | undefined): string {
92
    return this.toTitleCase(str).split(' ').join('');
2,374✔
93
  }
94

95
  /**
96
   * Replaces separators with underscores.
97
   *
98
   * @param value The string to process.
99
   *
100
   * @returns The processed string.
101
   */
102
  static snakeCase(value: string): string {
103
    return this.replaceAll(value, ' ', '_');
71✔
104
  }
105

106
  /**
107
   * Acronym.
108
   *
109
   * @param value The string to shorten.
110
   *
111
   * @returns The acronym.
112
   */
113
  static acronym(value: string): string {
114
    return value ? value.split(' ').map((_) => _[0]).join('').toUpperCase() : '';
42✔
115
  }
116

117
  /**
118
   * Glyph.
119
   *
120
   * @param value The string to process.
121
   *
122
   * @returns The glyph.
123
   */
124
  static glyph(value: string): string { return StringExService.acronym(value).slice(void 0, 2); }
3✔
125

126
  /**
127
   * Shortens a long caption.
128
   *
129
   * @param str The caption to shorten.
130
   *
131
   * @returns A shortened caption.
132
   */
133
  static shorten(str: string): string {
134
    const maxlength = 50;
1✔
135

136
    if (str.length > maxlength) {
1✔
137
      str = str.substring(0, maxlength) + '...';
1✔
138
    }
139

140
    return str;
1✔
141
  }
142

143
  /**
144
   * Splits a long label into lines.
145
   *
146
   * @param label The label(s) to split.
147
   *
148
   * @returns A lines array.
149
   */
150
  static splitLine(label: string | string[]): string[] {
151
    const str = label instanceof Array ? label.join(' - ') : label;
12✔
152
    const maxLength = 40;
12✔
153

154
    const lines: string[] = [];
12✔
155

156
    if (str.length > maxLength) {
12✔
157
      const firstSpace = str.substring(maxLength).indexOf(' ');
4✔
158
      if (firstSpace === -1) {
4✔
159
        lines.push(str);
1✔
160
        return lines;
1✔
161
      }
162

163
      StringExService.recurseSplitLine(str, maxLength, lines, firstSpace);
3✔
164
    } else {
165
      lines.push(str);
8✔
166
    }
167

168
    return lines;
11✔
169
  }
170

171
  /**
172
   * Recurses the split lines function.
173
   *
174
   * @param str The current string.
175
   * @param maxLength The line splitting line length threshold.
176
   * @param lines The array of lines being built.
177
   * @param firstSpace the first space ahead position.
178
   */
179
  private static recurseSplitLine(str: string, maxLength: number, lines: string[], firstSpace: number): void {
180
    const position = maxLength + firstSpace;
3✔
181
    lines.push(str.substring(0, position));
3✔
182
    this.splitLine(str.substring(position + 1)).forEach((_) => lines.push(_));
4✔
183
  }
184
}
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