UNPKG

5.61 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.levenshtein = exports.capitalize = exports.underscore = exports.classify = exports.camelize = exports.dasherize = exports.decamelize = void 0;
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, 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 ```
43
44 @method dasherize
45 @param {String} str The string to dasherize.
46 @return {String} the dasherized string.
47 */
48function dasherize(str) {
49 return decamelize(str).replace(STRING_DASHERIZE_REGEXP, '-');
50}
51exports.dasherize = dasherize;
52/**
53 Returns the lowerCamelCase form of a string.
54
55 ```javascript
56 camelize('innerHTML'); // 'innerHTML'
57 camelize('action_name'); // 'actionName'
58 camelize('css-class-name'); // 'cssClassName'
59 camelize('my favorite items'); // 'myFavoriteItems'
60 camelize('My Favorite Items'); // 'myFavoriteItems'
61 ```
62
63 @method camelize
64 @param {String} str The string to camelize.
65 @return {String} the camelized string.
66 */
67function camelize(str) {
68 return str
69 .replace(STRING_CAMELIZE_REGEXP, (_match, _separator, chr) => {
70 return chr ? chr.toUpperCase() : '';
71 })
72 .replace(/^([A-Z])/, (match) => match.toLowerCase());
73}
74exports.camelize = camelize;
75/**
76 Returns the UpperCamelCase form of a string.
77
78 @example
79 ```javascript
80 'innerHTML'.classify(); // 'InnerHTML'
81 'action_name'.classify(); // 'ActionName'
82 'css-class-name'.classify(); // 'CssClassName'
83 'my favorite items'.classify(); // 'MyFavoriteItems'
84 'app.component'.classify(); // 'AppComponent'
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;
137/**
138 * Calculate the levenshtein distance of two strings.
139 * See https://en.wikipedia.org/wiki/Levenshtein_distance.
140 * Based off https://gist.github.com/andrei-m/982927 (for using the faster dynamic programming
141 * version).
142 *
143 * @param a String a.
144 * @param b String b.
145 * @returns A number that represents the distance between the two strings. The greater the number
146 * the more distant the strings are from each others.
147 */
148function levenshtein(a, b) {
149 if (a.length == 0) {
150 return b.length;
151 }
152 if (b.length == 0) {
153 return a.length;
154 }
155 const matrix = [];
156 // increment along the first column of each row
157 for (let i = 0; i <= b.length; i++) {
158 matrix[i] = [i];
159 }
160 // increment each column in the first row
161 for (let j = 0; j <= a.length; j++) {
162 matrix[0][j] = j;
163 }
164 // Fill in the rest of the matrix
165 for (let i = 1; i <= b.length; i++) {
166 for (let j = 1; j <= a.length; j++) {
167 if (b.charAt(i - 1) == a.charAt(j - 1)) {
168 matrix[i][j] = matrix[i - 1][j - 1];
169 }
170 else {
171 matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
172 matrix[i][j - 1] + 1, // insertion
173 matrix[i - 1][j] + 1);
174 }
175 }
176 }
177 return matrix[b.length][a.length];
178}
179exports.levenshtein = levenshtein;