UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.featurePath = exports.group = exports.capitalize = exports.underscore = exports.classify = exports.camelize = exports.dasherize = exports.decamelize = void 0;
4/**
5 * @license
6 * Copyright Google Inc. All Rights Reserved.
7 *
8 * Use of this source code is governed by an MIT-style license that can be
9 * found in the LICENSE file at https://angular.io/license
10 */
11const STRING_DASHERIZE_REGEXP = /[ _.]/g;
12const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
13const STRING_CAMELIZE_REGEXP = /(-|_|\.|\s)+(.)?/g;
14const STRING_UNDERSCORE_REGEXP_1 = /([a-z\d])([A-Z]+)/g;
15const STRING_UNDERSCORE_REGEXP_2 = /-|\s+/g;
16/**
17 * Converts a camelized string into all lower case separated by underscores.
18 *
19 ```javascript
20 decamelize('innerHTML'); // 'inner_html'
21 decamelize('action_name'); // 'action_name'
22 decamelize('css-class-name'); // 'css-class-name'
23 decamelize('my favorite items'); // 'my favorite items'
24 ```
25
26 @method decamelize
27 @param {String} str The string to decamelize.
28 @return {String} the decamelized string.
29 */
30function decamelize(str) {
31 return str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase();
32}
33exports.decamelize = decamelize;
34/**
35 Replaces underscores, spaces, periods, or camelCase with dashes.
36
37 ```javascript
38 dasherize('innerHTML'); // 'inner-html'
39 dasherize('action_name'); // 'action-name'
40 dasherize('css-class-name'); // 'css-class-name'
41 dasherize('my favorite items'); // 'my-favorite-items'
42 dasherize('nrwl.io'); // 'nrwl-io'
43 ```
44
45 @method dasherize
46 @param {String} str The string to dasherize.
47 @return {String} the dasherized string.
48 */
49function dasherize(str) {
50 return decamelize(str || '').replace(STRING_DASHERIZE_REGEXP, '-');
51}
52exports.dasherize = dasherize;
53/**
54 Returns the lowerCamelCase form of a string.
55
56 ```javascript
57 camelize('innerHTML'); // 'innerHTML'
58 camelize('action_name'); // 'actionName'
59 camelize('css-class-name'); // 'cssClassName'
60 camelize('my favorite items'); // 'myFavoriteItems'
61 camelize('My Favorite Items'); // 'myFavoriteItems'
62 ```
63
64 @method camelize
65 @param {String} str The string to camelize.
66 @return {String} the camelized string.
67 */
68function camelize(str) {
69 return str
70 .replace(STRING_CAMELIZE_REGEXP, (_match, _separator, chr) => {
71 return chr ? chr.toUpperCase() : '';
72 })
73 .replace(/^([A-Z])/, (match) => match.toLowerCase());
74}
75exports.camelize = camelize;
76/**
77 Returns the UpperCamelCase form of a string.
78
79 ```javascript
80 'innerHTML'.classify(); // 'InnerHTML'
81 'action_name'.classify(); // 'ActionName'
82 'css-class-name'.classify(); // 'CssClassName'
83 'my favorite items'.classify(); // 'MyFavoriteItems'
84 ```
85
86 @method classify
87 @param {String} str the string to classify
88 @return {String} the classified string
89 */
90function classify(str) {
91 return str
92 .split('.')
93 .map((part) => capitalize(camelize(part)))
94 .join('.');
95}
96exports.classify = classify;
97/**
98 More general than decamelize. Returns the lower\_case\_and\_underscored
99 form of a string.
100
101 ```javascript
102 'innerHTML'.underscore(); // 'inner_html'
103 'action_name'.underscore(); // 'action_name'
104 'css-class-name'.underscore(); // 'css_class_name'
105 'my favorite items'.underscore(); // 'my_favorite_items'
106 ```
107
108 @method underscore
109 @param {String} str The string to underscore.
110 @return {String} the underscored string.
111 */
112function underscore(str) {
113 return str
114 .replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2')
115 .replace(STRING_UNDERSCORE_REGEXP_2, '_')
116 .toLowerCase();
117}
118exports.underscore = underscore;
119/**
120 Returns the Capitalized form of a string
121
122 ```javascript
123 'innerHTML'.capitalize() // 'InnerHTML'
124 'action_name'.capitalize() // 'Action_name'
125 'css-class-name'.capitalize() // 'Css-class-name'
126 'my favorite items'.capitalize() // 'My favorite items'
127 ```
128
129 @method capitalize
130 @param {String} str The string to capitalize.
131 @return {String} The capitalized string.
132 */
133function capitalize(str) {
134 return str.charAt(0).toUpperCase() + str.slice(1);
135}
136exports.capitalize = capitalize;
137function group(name, group) {
138 return group ? `${group}/${name}` : name;
139}
140exports.group = group;
141function featurePath(group, flat, path, name) {
142 if (group && !flat) {
143 return `../../${path}/${name}/`;
144 }
145 return group ? `../${path}/` : './';
146}
147exports.featurePath = featurePath;
148//# sourceMappingURL=strings.js.map
\No newline at end of file