UNPKG

3.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Converts string into camelCase.
5 *
6 * @see http://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case
7 */
8function camelCase(str, firstCapital) {
9 if (firstCapital === void 0) { firstCapital = false; }
10 return str.replace(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2, offset) {
11 if (firstCapital === true && offset === 0)
12 return p1;
13 if (p2)
14 return p2.toUpperCase();
15 return p1.toLowerCase();
16 });
17}
18exports.camelCase = camelCase;
19/**
20 * Converts string into snake-case.
21 *
22 * @see https://regex101.com/r/QeSm2I/1
23 */
24function snakeCase(str) {
25 return str.replace(/(?:([a-z])([A-Z]))|(?:((?!^)[A-Z])([a-z]))/g, "$1_$3$2$4").toLowerCase();
26}
27exports.snakeCase = snakeCase;
28/**
29 * Converts string into title-case.
30 *
31 * @see http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript
32 */
33function titleCase(str) {
34 return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
35}
36exports.titleCase = titleCase;
37/**
38 * Builds abbreviated string from given string;
39 */
40function abbreviate(str, abbrLettersCount) {
41 if (abbrLettersCount === void 0) { abbrLettersCount = 1; }
42 var words = str.replace(/([a-z\xE0-\xFF])([A-Z\xC0\xDF])/g, "$1 $2").split(" ");
43 return words.reduce(function (res, word) {
44 res += word.substr(0, abbrLettersCount);
45 return res;
46 }, "");
47}
48exports.abbreviate = abbreviate;
49/**
50 * Shorten a given `input`. Useful for RDBMS imposing a limit on the
51 * maximum length of aliases and column names in SQL queries.
52 *
53 * @param input String to be shortened.
54 * @param options Default to `4` for segments length, `2` for terms length, `'__'` as a separator.
55 *
56 * @return Shortened `input`.
57 *
58 * @example
59 * // returns: "UsShCa__orde__mark__dire"
60 * shorten('UserShoppingCart__order__market__director')
61 *
62 * // returns: "cat_wit_ver_lon_nam_pos_wit_ver_lon_nam_pos_wit_ver_lon_nam"
63 * shorten(
64 * 'category_with_very_long_name_posts_with_very_long_name_post_with_very_long_name',
65 * { separator: '_', segmentLength: 3 }
66 * )
67 *
68 * // equals: UsShCa__orde__mark_market_id
69 * `${shorten('UserShoppingCart__order__market')}_market_id`
70 */
71function shorten(input, options) {
72 if (options === void 0) { options = {}; }
73 var _a = options.segmentLength, segmentLength = _a === void 0 ? 4 : _a, _b = options.separator, separator = _b === void 0 ? "__" : _b, _c = options.termLength, termLength = _c === void 0 ? 2 : _c;
74 var segments = input.split(separator);
75 var shortSegments = segments.reduce(function (acc, val) {
76 // split the given segment into many terms based on an eventual camel cased name
77 var segmentTerms = val.replace(/([a-z\xE0-\xFF])([A-Z\xC0-\xDF])/g, "$1 $2").split(" ");
78 // "OrderItemList" becomes "OrItLi", while "company" becomes "comp"
79 var length = segmentTerms.length > 1 ? termLength : segmentLength;
80 var shortSegment = segmentTerms.map(function (term) { return term.substr(0, length); }).join("");
81 acc.push(shortSegment);
82 return acc;
83 }, []);
84 return shortSegments.join(separator);
85}
86exports.shorten = shorten;
87
88//# sourceMappingURL=StringUtils.js.map