1 | ;
|
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 | */
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.levenshtein = exports.capitalize = exports.underscore = exports.classify = exports.camelize = exports.dasherize = exports.decamelize = void 0;
|
11 | const STRING_DASHERIZE_REGEXP = /[ _]/g;
|
12 | const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
|
13 | const STRING_CAMELIZE_REGEXP = /(-|_|\.|\s)+(.)?/g;
|
14 | const STRING_UNDERSCORE_REGEXP_1 = /([a-z\d])([A-Z]+)/g;
|
15 | const 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 | */
|
30 | function decamelize(str) {
|
31 | return str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase();
|
32 | }
|
33 | exports.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 | */
|
48 | function dasherize(str) {
|
49 | return decamelize(str).replace(STRING_DASHERIZE_REGEXP, '-');
|
50 | }
|
51 | exports.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 | */
|
67 | function 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 | }
|
74 | exports.camelize = camelize;
|
75 | /**
|
76 | Returns the UpperCamelCase form of a string.
|
77 |
|
78 | ```javascript
|
79 | 'innerHTML'.classify(); // 'InnerHTML'
|
80 | 'action_name'.classify(); // 'ActionName'
|
81 | 'css-class-name'.classify(); // 'CssClassName'
|
82 | 'my favorite items'.classify(); // 'MyFavoriteItems'
|
83 | ```
|
84 |
|
85 | @method classify
|
86 | @param {String} str the string to classify
|
87 | @return {String} the classified string
|
88 | */
|
89 | function classify(str) {
|
90 | return str
|
91 | .split('.')
|
92 | .map((part) => capitalize(camelize(part)))
|
93 | .join('.');
|
94 | }
|
95 | exports.classify = classify;
|
96 | /**
|
97 | More general than decamelize. Returns the lower\_case\_and\_underscored
|
98 | form of a string.
|
99 |
|
100 | ```javascript
|
101 | 'innerHTML'.underscore(); // 'inner_html'
|
102 | 'action_name'.underscore(); // 'action_name'
|
103 | 'css-class-name'.underscore(); // 'css_class_name'
|
104 | 'my favorite items'.underscore(); // 'my_favorite_items'
|
105 | ```
|
106 |
|
107 | @method underscore
|
108 | @param {String} str The string to underscore.
|
109 | @return {String} the underscored string.
|
110 | */
|
111 | function underscore(str) {
|
112 | return str
|
113 | .replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2')
|
114 | .replace(STRING_UNDERSCORE_REGEXP_2, '_')
|
115 | .toLowerCase();
|
116 | }
|
117 | exports.underscore = underscore;
|
118 | /**
|
119 | Returns the Capitalized form of a string
|
120 |
|
121 | ```javascript
|
122 | 'innerHTML'.capitalize() // 'InnerHTML'
|
123 | 'action_name'.capitalize() // 'Action_name'
|
124 | 'css-class-name'.capitalize() // 'Css-class-name'
|
125 | 'my favorite items'.capitalize() // 'My favorite items'
|
126 | ```
|
127 |
|
128 | @method capitalize
|
129 | @param {String} str The string to capitalize.
|
130 | @return {String} The capitalized string.
|
131 | */
|
132 | function capitalize(str) {
|
133 | return str.charAt(0).toUpperCase() + str.substr(1);
|
134 | }
|
135 | exports.capitalize = capitalize;
|
136 | /**
|
137 | * Calculate the levenshtein distance of two strings.
|
138 | * See https://en.wikipedia.org/wiki/Levenshtein_distance.
|
139 | * Based off https://gist.github.com/andrei-m/982927 (for using the faster dynamic programming
|
140 | * version).
|
141 | *
|
142 | * @param a String a.
|
143 | * @param b String b.
|
144 | * @returns A number that represents the distance between the two strings. The greater the number
|
145 | * the more distant the strings are from each others.
|
146 | */
|
147 | function levenshtein(a, b) {
|
148 | if (a.length == 0) {
|
149 | return b.length;
|
150 | }
|
151 | if (b.length == 0) {
|
152 | return a.length;
|
153 | }
|
154 | const matrix = [];
|
155 | // increment along the first column of each row
|
156 | for (let i = 0; i <= b.length; i++) {
|
157 | matrix[i] = [i];
|
158 | }
|
159 | // increment each column in the first row
|
160 | for (let j = 0; j <= a.length; j++) {
|
161 | matrix[0][j] = j;
|
162 | }
|
163 | // Fill in the rest of the matrix
|
164 | for (let i = 1; i <= b.length; i++) {
|
165 | for (let j = 1; j <= a.length; j++) {
|
166 | if (b.charAt(i - 1) == a.charAt(j - 1)) {
|
167 | matrix[i][j] = matrix[i - 1][j - 1];
|
168 | }
|
169 | else {
|
170 | matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
|
171 | matrix[i][j - 1] + 1, // insertion
|
172 | matrix[i - 1][j] + 1);
|
173 | }
|
174 | }
|
175 | }
|
176 | return matrix[b.length][a.length];
|
177 | }
|
178 | exports.levenshtein = levenshtein;
|