-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20344 from kategengler/kg-ember-string-deprecation
- Loading branch information
Showing
21 changed files
with
357 additions
and
61 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/@ember/-internals/glimmer/lib/helpers/-normalize-class.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
This module exists to separate the @ember/string methods used | ||
internally in ember-source, from those public methods that are | ||
now deprecated and to be removed. | ||
*/ | ||
|
||
import { Cache } from '@ember/-internals/utils'; | ||
|
||
const STRING_DASHERIZE_REGEXP = /[ _]/g; | ||
|
||
const STRING_DASHERIZE_CACHE = new Cache<string, string>(1000, (key) => | ||
decamelize(key).replace(STRING_DASHERIZE_REGEXP, '-') | ||
); | ||
|
||
const STRING_CLASSIFY_REGEXP_1 = /^(-|_)+(.)?/; | ||
const STRING_CLASSIFY_REGEXP_2 = /(.)(-|_|\.|\s)+(.)?/g; | ||
const STRING_CLASSIFY_REGEXP_3 = /(^|\/|\.)([a-z])/g; | ||
|
||
const CLASSIFY_CACHE = new Cache<string, string>(1000, (str) => { | ||
let replace1 = (_match: string, _separator: string, chr: string) => | ||
chr ? `_${chr.toUpperCase()}` : ''; | ||
let replace2 = (_match: string, initialChar: string, _separator: string, chr: string) => | ||
initialChar + (chr ? chr.toUpperCase() : ''); | ||
let parts = str.split('/'); | ||
for (let i = 0; i < parts.length; i++) { | ||
parts[i] = parts[i]!.replace(STRING_CLASSIFY_REGEXP_1, replace1).replace( | ||
STRING_CLASSIFY_REGEXP_2, | ||
replace2 | ||
); | ||
} | ||
return parts | ||
.join('/') | ||
.replace(STRING_CLASSIFY_REGEXP_3, (match /*, separator, chr */) => match.toUpperCase()); | ||
}); | ||
|
||
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g; | ||
|
||
const DECAMELIZE_CACHE = new Cache<string, string>(1000, (str) => | ||
str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase() | ||
); | ||
|
||
/** | ||
Defines string helper methods used internally in ember-source. | ||
@class String | ||
@private | ||
*/ | ||
|
||
/** | ||
Replaces underscores, spaces, or camelCase with dashes. | ||
```javascript | ||
import { dasherize } from '@ember/-internals/string'; | ||
dasherize('innerHTML'); // 'inner-html' | ||
dasherize('action_name'); // 'action-name' | ||
dasherize('css-class-name'); // 'css-class-name' | ||
dasherize('my favorite items'); // 'my-favorite-items' | ||
dasherize('privateDocs/ownerInvoice'; // 'private-docs/owner-invoice' | ||
``` | ||
@method dasherize | ||
@param {String} str The string to dasherize. | ||
@return {String} the dasherized string. | ||
@private | ||
*/ | ||
export function dasherize(str: string): string { | ||
return STRING_DASHERIZE_CACHE.get(str); | ||
} | ||
|
||
/** | ||
Returns the UpperCamelCase form of a string. | ||
```javascript | ||
import { classify } from '@ember/string'; | ||
classify('innerHTML'); // 'InnerHTML' | ||
classify('action_name'); // 'ActionName' | ||
classify('css-class-name'); // 'CssClassName' | ||
classify('my favorite items'); // 'MyFavoriteItems' | ||
classify('private-docs/owner-invoice'); // 'PrivateDocs/OwnerInvoice' | ||
``` | ||
@method classify | ||
@param {String} str the string to classify | ||
@return {String} the classified string | ||
@private | ||
*/ | ||
export function classify(str: string): string { | ||
return CLASSIFY_CACHE.get(str); | ||
} | ||
|
||
/** | ||
Converts a camelized string into all lower case separated by underscores. | ||
```javascript | ||
decamelize('innerHTML'); // 'inner_html' | ||
decamelize('action_name'); // 'action_name' | ||
decamelize('css-class-name'); // 'css-class-name' | ||
decamelize('my favorite items'); // 'my favorite items' | ||
``` | ||
*/ | ||
function decamelize(str: string): string { | ||
return DECAMELIZE_CACHE.get(str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* eslint-disable qunit/no-test-expect-argument */ | ||
import { classify } from '@ember/-internals/string'; | ||
import { moduleFor, AbstractTestCase } from 'internal-test-helpers'; | ||
|
||
function test(assert, given, expected, description) { | ||
assert.deepEqual(classify(given), expected, description); | ||
} | ||
|
||
moduleFor( | ||
'EmberInternalsString.classify', | ||
class extends AbstractTestCase { | ||
['@test String classify tests'](assert) { | ||
test(assert, 'my favorite items', 'MyFavoriteItems', 'classify normal string'); | ||
test(assert, 'css-class-name', 'CssClassName', 'classify dasherized string'); | ||
test(assert, 'action_name', 'ActionName', 'classify underscored string'); | ||
test( | ||
assert, | ||
'privateDocs/ownerInvoice', | ||
'PrivateDocs/OwnerInvoice', | ||
'classify namespaced camelized string' | ||
); | ||
test( | ||
assert, | ||
'private_docs/owner_invoice', | ||
'PrivateDocs/OwnerInvoice', | ||
'classify namespaced underscored string' | ||
); | ||
test( | ||
assert, | ||
'private-docs/owner-invoice', | ||
'PrivateDocs/OwnerInvoice', | ||
'classify namespaced dasherized string' | ||
); | ||
test(assert, '-view-registry', '_ViewRegistry', 'classify prefixed dasherized string'); | ||
test( | ||
assert, | ||
'components/-text-field', | ||
'Components/_TextField', | ||
'classify namespaced prefixed dasherized string' | ||
); | ||
test(assert, '_Foo_Bar', '_FooBar', 'classify underscore-prefixed underscored string'); | ||
test(assert, '_Foo-Bar', '_FooBar', 'classify underscore-prefixed dasherized string'); | ||
test( | ||
assert, | ||
'_foo/_bar', | ||
'_Foo/_Bar', | ||
'classify underscore-prefixed-namespaced underscore-prefixed string' | ||
); | ||
test( | ||
assert, | ||
'-foo/_bar', | ||
'_Foo/_Bar', | ||
'classify dash-prefixed-namespaced underscore-prefixed string' | ||
); | ||
test( | ||
assert, | ||
'-foo/-bar', | ||
'_Foo/_Bar', | ||
'classify dash-prefixed-namespaced dash-prefixed string' | ||
); | ||
test(assert, 'InnerHTML', 'InnerHTML', 'does nothing with classified string'); | ||
test(assert, '_FooBar', '_FooBar', 'does nothing with classified prefixed string'); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable qunit/no-test-expect-argument */ | ||
import { dasherize } from '@ember/-internals/string'; | ||
import { moduleFor, AbstractTestCase } from 'internal-test-helpers'; | ||
|
||
function test(assert, given, expected, description) { | ||
assert.deepEqual(dasherize(given), expected, description); | ||
} | ||
|
||
moduleFor( | ||
'EmberInternalsString.dasherize', | ||
class extends AbstractTestCase { | ||
['@test String dasherize tests'](assert) { | ||
test(assert, 'my favorite items', 'my-favorite-items', 'dasherize normal string'); | ||
test(assert, 'css-class-name', 'css-class-name', 'does nothing with dasherized string'); | ||
test(assert, 'action_name', 'action-name', 'dasherize underscored string'); | ||
test(assert, 'innerHTML', 'inner-html', 'dasherize camelcased string'); | ||
test( | ||
assert, | ||
'toString', | ||
'to-string', | ||
'dasherize string that is the property name of Object.prototype' | ||
); | ||
test( | ||
assert, | ||
'PrivateDocs/OwnerInvoice', | ||
'private-docs/owner-invoice', | ||
'dasherize namespaced classified string' | ||
); | ||
test( | ||
assert, | ||
'privateDocs/ownerInvoice', | ||
'private-docs/owner-invoice', | ||
'dasherize namespaced camelized string' | ||
); | ||
test( | ||
assert, | ||
'private_docs/owner_invoice', | ||
'private-docs/owner-invoice', | ||
'dasherize namespaced underscored string' | ||
); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.