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

typeorm / typeorm / 14796576772

02 May 2025 01:52PM UTC coverage: 45.367% (-30.9%) from 76.309%
14796576772

Pull #11434

github

web-flow
Merge ec4ce2d00 into fadad1a74
Pull Request #11434: feat: release PR releases using pkg.pr.new

5216 of 12761 branches covered (40.87%)

Branch coverage included in aggregate %.

11439 of 23951 relevant lines covered (47.76%)

15712.55 hits per line

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

73.91
/src/util/StringUtils.ts
1
import shajs from "sha.js"
36✔
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 {
36✔
9
    if (firstCapital) str = " " + str
4,111✔
10
    return str.replace(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2) {
4,111✔
11
        if (p2) return p2.toUpperCase()
2,909✔
12
        return p1.toLowerCase()
×
13
    })
14
}
15

16
/**
17
 * Converts string into snake_case.
18
 *
19
 */
20
export function snakeCase(str: string): string {
36✔
21
    return (
4,383✔
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 {
36✔
37
    return str.replace(
1,824✔
38
        /\w\S*/g,
39
        (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(),
1,824✔
40
    )
41
}
42

43
/**
44
 * Builds abbreviated string from given string;
45
 */
46
export function abbreviate(str: string, abbrLettersCount: number = 1): string {
36!
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 {
36!
88
    const { segmentLength = 4, separator = "__", termLength = 2 } = options
13!
89

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

102
        acc.push(shortSegment)
63✔
103
        return acc
63✔
104
    }, [])
105

106
    return shortSegments.join(separator)
13✔
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 {
36!
120
    const hashFunction = shajs("sha1")
1,180✔
121
    hashFunction.update(input, "utf8")
1,180✔
122
    const hashedInput = hashFunction.digest("hex")
1,180✔
123
    if (options.length) {
1,180✔
124
        return hashedInput.slice(0, options.length)
1,180✔
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