|
| 1 | +/** |
| 2 | +@module ember |
| 3 | +@submodule ember-runtime |
| 4 | +*/ |
| 5 | +import Cache from './cache'; |
| 6 | + |
| 7 | +const STRING_DASHERIZE_REGEXP = (/[ _]/g); |
| 8 | + |
| 9 | +const STRING_DASHERIZE_CACHE = new Cache(1000, key => decamelize(key).replace(STRING_DASHERIZE_REGEXP, '-')); |
| 10 | + |
| 11 | +const STRING_CAMELIZE_REGEXP_1 = (/(-|_|\.|\s)+(.)?/g); |
| 12 | +const STRING_CAMELIZE_REGEXP_2 = (/(^|\/)([A-Z])/g); |
| 13 | + |
| 14 | +const CAMELIZE_CACHE = new Cache(1000, key => key.replace(STRING_CAMELIZE_REGEXP_1, (match, separator, chr) => chr ? chr.toUpperCase() : '').replace(STRING_CAMELIZE_REGEXP_2, (match/*, separator, chr*/) => match.toLowerCase())); |
| 15 | + |
| 16 | +const STRING_CLASSIFY_REGEXP_1 = (/^(-|_)+(.)?/); |
| 17 | +const STRING_CLASSIFY_REGEXP_2 = (/(.)(-|_|\.|\s)+(.)?/g); |
| 18 | +const STRING_CLASSIFY_REGEXP_3 = (/(^|\/|\.)([a-z])/g); |
| 19 | + |
| 20 | +const CLASSIFY_CACHE = new Cache(1000, str => { |
| 21 | + let replace1 = (match, separator, chr) => chr ? (`_${chr.toUpperCase()}`) : ''; |
| 22 | + let replace2 = (match, initialChar, separator, chr) => initialChar + (chr ? chr.toUpperCase() : ''); |
| 23 | + let parts = str.split('/'); |
| 24 | + |
| 25 | + for (let i = 0; i < parts.length; i++) { |
| 26 | + parts[i] = parts[i] |
| 27 | + .replace(STRING_CLASSIFY_REGEXP_1, replace1) |
| 28 | + .replace(STRING_CLASSIFY_REGEXP_2, replace2); |
| 29 | + } |
| 30 | + |
| 31 | + return parts |
| 32 | + .join('/') |
| 33 | + .replace(STRING_CLASSIFY_REGEXP_3, (match/*, separator, chr*/) => match.toUpperCase()); |
| 34 | +}); |
| 35 | + |
| 36 | +const STRING_UNDERSCORE_REGEXP_1 = (/([a-z\d])([A-Z]+)/g); |
| 37 | +const STRING_UNDERSCORE_REGEXP_2 = (/-|\s+/g); |
| 38 | + |
| 39 | +const UNDERSCORE_CACHE = new Cache(1000, str => str.replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2'). |
| 40 | + replace(STRING_UNDERSCORE_REGEXP_2, '_').toLowerCase()); |
| 41 | + |
| 42 | +const STRING_CAPITALIZE_REGEXP = (/(^|\/)([a-z\u00C0-\u024F])/g); |
| 43 | + |
| 44 | +const CAPITALIZE_CACHE = new Cache(1000, str => str.replace(STRING_CAPITALIZE_REGEXP, (match/*, separator, chr*/) => match.toUpperCase())); |
| 45 | + |
| 46 | +const STRING_DECAMELIZE_REGEXP = (/([a-z\d])([A-Z])/g); |
| 47 | + |
| 48 | +const DECAMELIZE_CACHE = new Cache(1000, str => str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase()); |
| 49 | + |
| 50 | +/** |
| 51 | + Splits a string into separate units separated by spaces, eliminating any |
| 52 | + empty strings in the process. This is a convenience method for split that |
| 53 | + is mostly useful when applied to the `String.prototype`. |
| 54 | +
|
| 55 | + ```javascript |
| 56 | + Ember.String.w("alpha beta gamma").forEach(function(key) { |
| 57 | + console.log(key); |
| 58 | + }); |
| 59 | +
|
| 60 | + // > alpha |
| 61 | + // > beta |
| 62 | + // > gamma |
| 63 | + ``` |
| 64 | +
|
| 65 | + @method w |
| 66 | + @param {String} str The string to split |
| 67 | + @return {Array} array containing the split strings |
| 68 | + @public |
| 69 | +*/ |
| 70 | +export function w(str) { |
| 71 | + return str.split(/\s+/); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + Converts a camelized string into all lower case separated by underscores. |
| 76 | +
|
| 77 | + ```javascript |
| 78 | + 'innerHTML'.decamelize(); // 'inner_html' |
| 79 | + 'action_name'.decamelize(); // 'action_name' |
| 80 | + 'css-class-name'.decamelize(); // 'css-class-name' |
| 81 | + 'my favorite items'.decamelize(); // 'my favorite items' |
| 82 | + ``` |
| 83 | +
|
| 84 | + @method decamelize |
| 85 | + @param {String} str The string to decamelize. |
| 86 | + @return {String} the decamelized string. |
| 87 | + @public |
| 88 | +*/ |
| 89 | +export function decamelize(str) { |
| 90 | + return DECAMELIZE_CACHE.get(str); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + Replaces underscores, spaces, or camelCase with dashes. |
| 95 | +
|
| 96 | + ```javascript |
| 97 | + 'innerHTML'.dasherize(); // 'inner-html' |
| 98 | + 'action_name'.dasherize(); // 'action-name' |
| 99 | + 'css-class-name'.dasherize(); // 'css-class-name' |
| 100 | + 'my favorite items'.dasherize(); // 'my-favorite-items' |
| 101 | + 'privateDocs/ownerInvoice'.dasherize(); // 'private-docs/owner-invoice' |
| 102 | + ``` |
| 103 | +
|
| 104 | + @method dasherize |
| 105 | + @param {String} str The string to dasherize. |
| 106 | + @return {String} the dasherized string. |
| 107 | + @public |
| 108 | +*/ |
| 109 | +export function dasherize(str) { |
| 110 | + return STRING_DASHERIZE_CACHE.get(str); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + Returns the lowerCamelCase form of a string. |
| 115 | +
|
| 116 | + ```javascript |
| 117 | + 'innerHTML'.camelize(); // 'innerHTML' |
| 118 | + 'action_name'.camelize(); // 'actionName' |
| 119 | + 'css-class-name'.camelize(); // 'cssClassName' |
| 120 | + 'my favorite items'.camelize(); // 'myFavoriteItems' |
| 121 | + 'My Favorite Items'.camelize(); // 'myFavoriteItems' |
| 122 | + 'private-docs/owner-invoice'.camelize(); // 'privateDocs/ownerInvoice' |
| 123 | + ``` |
| 124 | +
|
| 125 | + @method camelize |
| 126 | + @param {String} str The string to camelize. |
| 127 | + @return {String} the camelized string. |
| 128 | + @public |
| 129 | +*/ |
| 130 | +export function camelize(str) { |
| 131 | + return CAMELIZE_CACHE.get(str); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + Returns the UpperCamelCase form of a string. |
| 136 | +
|
| 137 | + ```javascript |
| 138 | + 'innerHTML'.classify(); // 'InnerHTML' |
| 139 | + 'action_name'.classify(); // 'ActionName' |
| 140 | + 'css-class-name'.classify(); // 'CssClassName' |
| 141 | + 'my favorite items'.classify(); // 'MyFavoriteItems' |
| 142 | + 'private-docs/owner-invoice'.classify(); // 'PrivateDocs/OwnerInvoice' |
| 143 | + ``` |
| 144 | +
|
| 145 | + @method classify |
| 146 | + @param {String} str the string to classify |
| 147 | + @return {String} the classified string |
| 148 | + @public |
| 149 | +*/ |
| 150 | +export function classify(str) { |
| 151 | + return CLASSIFY_CACHE.get(str); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + More general than decamelize. Returns the lower\_case\_and\_underscored |
| 156 | + form of a string. |
| 157 | +
|
| 158 | + ```javascript |
| 159 | + 'innerHTML'.underscore(); // 'inner_html' |
| 160 | + 'action_name'.underscore(); // 'action_name' |
| 161 | + 'css-class-name'.underscore(); // 'css_class_name' |
| 162 | + 'my favorite items'.underscore(); // 'my_favorite_items' |
| 163 | + 'privateDocs/ownerInvoice'.underscore(); // 'private_docs/owner_invoice' |
| 164 | + ``` |
| 165 | +
|
| 166 | + @method underscore |
| 167 | + @param {String} str The string to underscore. |
| 168 | + @return {String} the underscored string. |
| 169 | + @public |
| 170 | +*/ |
| 171 | +export function underscore(str) { |
| 172 | + return UNDERSCORE_CACHE.get(str); |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + Returns the Capitalized form of a string |
| 177 | +
|
| 178 | + ```javascript |
| 179 | + 'innerHTML'.capitalize() // 'InnerHTML' |
| 180 | + 'action_name'.capitalize() // 'Action_name' |
| 181 | + 'css-class-name'.capitalize() // 'Css-class-name' |
| 182 | + 'my favorite items'.capitalize() // 'My favorite items' |
| 183 | + 'privateDocs/ownerInvoice'.capitalize(); // 'PrivateDocs/ownerInvoice' |
| 184 | + ``` |
| 185 | +
|
| 186 | + @method capitalize |
| 187 | + @param {String} str The string to capitalize. |
| 188 | + @return {String} The capitalized string. |
| 189 | + @public |
| 190 | +*/ |
| 191 | +export function capitalize(str) { |
| 192 | + return CAPITALIZE_CACHE.get(str); |
| 193 | +} |
0 commit comments