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

typeorm / typeorm / 15219332477

23 May 2025 09:13PM UTC coverage: 17.216% (-59.1%) from 76.346%
15219332477

Pull #11332

github

naorpeled
cr comments - move if block
Pull Request #11332: feat: add new undefined and null behavior flags

1603 of 12759 branches covered (12.56%)

Branch coverage included in aggregate %.

0 of 31 new or added lines in 3 files covered. (0.0%)

14132 existing lines in 166 files now uncovered.

4731 of 24033 relevant lines covered (19.69%)

60.22 hits per line

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

43.48
/src/util/StringUtils.ts
1
import shajs from "sha.js"
9✔
2

3
/**
4
 * Converts string into camelCase.
5
 *
6
 * @see http://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case
7
 */
8
export function camelCase(str: string, firstCapital: boolean = false): string {
9✔
9
    if (firstCapital) str = " " + str
19✔
10
    return str.replace(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2) {
19✔
11
        if (p2) return p2.toUpperCase()
27✔
UNCOV
12
        return p1.toLowerCase()
×
13
    })
14
}
15

16
/**
17
 * Converts string into snake_case.
18
 *
19
 */
20
export function snakeCase(str: string): string {
9✔
21
    return (
59✔
22
        str
23
            // ABc -> a_bc
24
            .replace(/([A-Z])([A-Z])([a-z])/g, "$1_$2$3")
25
            // aC -> a_c
26
            .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
27
            .toLowerCase()
28
    )
29
}
30

31
/**
32
 * Converts string into Title Case.
33
 *
34
 * @see http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript
35
 */
36
export function titleCase(str: string): string {
9✔
UNCOV
37
    return str.replace(
×
38
        /\w\S*/g,
UNCOV
39
        (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(),
×
40
    )
41
}
42

43
/**
44
 * Builds abbreviated string from given string;
45
 */
46
export function abbreviate(str: string, abbrLettersCount: number = 1): string {
9!
47
    const words = str
×
48
        .replace(/([a-z\xE0-\xFF])([A-Z\xC0\xDF])/g, "$1 $2")
49
        .split(" ")
50
    return words.reduce((res, word) => {
×
51
        res += word.substr(0, abbrLettersCount)
×
52
        return res
×
53
    }, "")
54
}
55

56
export interface IShortenOptions {
57
    /** String used to split "segments" of the alias/column name */
58
    separator?: string
59
    /** Maximum length of any "segment" */
60
    segmentLength?: number
61
    /** Length of any "term" in a "segment"; "OrderItem" is a segment, "Order" and "Items" are terms */
62
    termLength?: number
63
}
64

65
/**
66
 * Shorten a given `input`. Useful for RDBMS imposing a limit on the
67
 * maximum length of aliases and column names in SQL queries.
68
 *
69
 * @param input String to be shortened.
70
 * @param options Default to `4` for segments length, `2` for terms length, `'__'` as a separator.
71
 *
72
 * @return Shortened `input`.
73
 *
74
 * @example
75
 * // returns: "UsShCa__orde__mark__dire"
76
 * shorten('UserShoppingCart__order__market__director')
77
 *
78
 * // returns: "cat_wit_ver_lon_nam_pos_wit_ver_lon_nam_pos_wit_ver_lon_nam"
79
 * shorten(
80
 *   'category_with_very_long_name_posts_with_very_long_name_post_with_very_long_name',
81
 *   { separator: '_', segmentLength: 3 }
82
 * )
83
 *
84
 * // equals: UsShCa__orde__mark_market_id
85
 * `${shorten('UserShoppingCart__order__market')}_market_id`
86
 */
87
export function shorten(input: string, options: IShortenOptions = {}): string {
9!
UNCOV
88
    const { segmentLength = 4, separator = "__", termLength = 2 } = options
×
89

UNCOV
90
    const segments = input.split(separator)
×
UNCOV
91
    const shortSegments = segments.reduce((acc: string[], val: string) => {
×
92
        // split the given segment into many terms based on an eventual camel cased name
UNCOV
93
        const segmentTerms = val
×
94
            .replace(/([a-z\xE0-\xFF])([A-Z\xC0-\xDF])/g, "$1 $2")
95
            .split(" ")
96
        // "OrderItemList" becomes "OrItLi", while "company" becomes "comp"
UNCOV
97
        const length = segmentTerms.length > 1 ? termLength : segmentLength
×
UNCOV
98
        const shortSegment = segmentTerms
×
UNCOV
99
            .map((term) => term.substr(0, length))
×
100
            .join("")
101

UNCOV
102
        acc.push(shortSegment)
×
UNCOV
103
        return acc
×
104
    }, [])
105

UNCOV
106
    return shortSegments.join(separator)
×
107
}
108

109
interface IHashOptions {
110
    length?: number
111
}
112

113
/**
114
 * Returns a hashed input.
115
 *
116
 * @param input String to be hashed.
117
 * @param options.length Optionally, shorten the output to desired length.
118
 */
119
export function hash(input: string, options: IHashOptions = {}): string {
9!
120
    const hashFunction = shajs("sha1")
26✔
121
    hashFunction.update(input, "utf8")
26✔
122
    const hashedInput = hashFunction.digest("hex")
26✔
123
    if (options.length) {
26✔
124
        return hashedInput.slice(0, options.length)
26✔
125
    }
126
    return hashedInput
×
127
}
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

© 2025 Coveralls, Inc