1+ /* eslint-disable no-useless-escape */
2+ import Cache from './cache' ;
3+ let STRINGS = { } ;
4+ export function setStrings ( strings ) {
5+ STRINGS = strings ;
6+ }
7+ export function getStrings ( ) {
8+ return STRINGS ;
9+ }
10+ export function getString ( name ) {
11+ return STRINGS [ name ] ;
12+ }
13+ const STRING_DASHERIZE_REGEXP = / [ _ ] / g;
14+ const STRING_DASHERIZE_CACHE = new Cache ( 1000 , ( key ) => decamelize ( key ) . replace ( STRING_DASHERIZE_REGEXP , '-' ) ) ;
15+ const STRING_CLASSIFY_REGEXP_1 = / ^ ( \- | _ ) + ( .) ? / ;
16+ const STRING_CLASSIFY_REGEXP_2 = / ( .) ( \- | \_ | \. | \s ) + ( .) ? / g;
17+ const STRING_CLASSIFY_REGEXP_3 = / ( ^ | \/ | \. ) ( [ a - z ] ) / g;
18+ const CLASSIFY_CACHE = new Cache ( 1000 , ( str ) => {
19+ const replace1 = ( _match , _separator , chr ) => chr ? `_${ chr . toUpperCase ( ) } ` : '' ;
20+ const replace2 = ( _match , initialChar , _separator , chr ) => initialChar + ( chr ? chr . toUpperCase ( ) : '' ) ;
21+ const parts = str . split ( '/' ) ;
22+ for ( let i = 0 ; i < parts . length ; i ++ ) {
23+ parts [ i ] = parts [ i ]
24+ . replace ( STRING_CLASSIFY_REGEXP_1 , replace1 )
25+ . replace ( STRING_CLASSIFY_REGEXP_2 , replace2 ) ;
26+ }
27+ return parts
28+ . join ( '/' )
29+ . replace ( STRING_CLASSIFY_REGEXP_3 , ( match /*, separator, chr */ ) => match . toUpperCase ( ) ) ;
30+ } ) ;
31+ const STRING_UNDERSCORE_REGEXP_1 = / ( [ a - z \d ] ) ( [ A - Z ] + ) / g;
32+ const STRING_UNDERSCORE_REGEXP_2 = / \- | \s + / g;
33+ const UNDERSCORE_CACHE = new Cache ( 1000 , ( str ) => str
34+ . replace ( STRING_UNDERSCORE_REGEXP_1 , '$1_$2' )
35+ . replace ( STRING_UNDERSCORE_REGEXP_2 , '_' )
36+ . toLowerCase ( ) ) ;
37+ const STRING_DECAMELIZE_REGEXP = / ( [ a - z \d ] ) ( [ A - Z ] ) / g;
38+ const DECAMELIZE_CACHE = new Cache ( 1000 , ( str ) => str . replace ( STRING_DECAMELIZE_REGEXP , '$1_$2' ) . toLowerCase ( ) ) ;
39+ /**
40+ Converts a camelized string into all lower case separated by underscores.
41+
42+ ```javascript
43+ import { decamelize } from '@ember/string';
44+
45+ decamelize('innerHTML'); // 'inner_html'
46+ decamelize('action_name'); // 'action_name'
47+ decamelize('css-class-name'); // 'css-class-name'
48+ decamelize('my favorite items'); // 'my favorite items'
49+ ```
50+
51+ @method decamelize
52+ @param {String } str The string to decamelize.
53+ @return {String } the decamelized string.
54+ @public
55+ */
56+ export function decamelize ( str ) {
57+ return DECAMELIZE_CACHE . get ( str ) ;
58+ }
59+ /**
60+ Replaces underscores, spaces, or camelCase with dashes.
61+
62+ ```javascript
63+ import { dasherize } from '@ember/string';
64+
65+ dasherize('innerHTML'); // 'inner-html'
66+ dasherize('action_name'); // 'action-name'
67+ dasherize('css-class-name'); // 'css-class-name'
68+ dasherize('my favorite items'); // 'my-favorite-items'
69+ dasherize('privateDocs/ownerInvoice'; // 'private-docs/owner-invoice'
70+ ```
71+
72+ @method dasherize
73+ @param {String } str The string to dasherize.
74+ @return {String } the dasherized string.
75+ @public
76+ */
77+ export function dasherize ( str ) {
78+ return STRING_DASHERIZE_CACHE . get ( str ) ;
79+ }
80+ /**
81+ Returns the UpperCamelCase form of a string.
82+
83+ ```javascript
84+ import { classify } from '@ember/string';
85+
86+ classify('innerHTML'); // 'InnerHTML'
87+ classify('action_name'); // 'ActionName'
88+ classify('css-class-name'); // 'CssClassName'
89+ classify('my favorite items'); // 'MyFavoriteItems'
90+ classify('private-docs/owner-invoice'); // 'PrivateDocs/OwnerInvoice'
91+ ```
92+
93+ @method classify
94+ @param {String } str the string to classify
95+ @return {String } the classified string
96+ @public
97+ */
98+ export function classify ( str ) {
99+ return CLASSIFY_CACHE . get ( str ) ;
100+ }
101+ /**
102+ More general than decamelize. Returns the lower\_case\_and\_underscored
103+ form of a string.
104+
105+ ```javascript
106+ import { underscore } from '@ember/string';
107+
108+ underscore('innerHTML'); // 'inner_html'
109+ underscore('action_name'); // 'action_name'
110+ underscore('css-class-name'); // 'css_class_name'
111+ underscore('my favorite items'); // 'my_favorite_items'
112+ underscore('privateDocs/ownerInvoice'); // 'private_docs/owner_invoice'
113+ ```
114+
115+ @method underscore
116+ @param {String } str The string to underscore.
117+ @return {String } the underscored string.
118+ @public
119+ */
120+ export function underscore ( str ) {
121+ return UNDERSCORE_CACHE . get ( str ) ;
122+ }
0 commit comments