From e00ada351d462da7a40a1b892a610f35bd605e50 Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Mon, 28 Jan 2019 11:35:04 +0000 Subject: [PATCH] remove deprecated loc + fmt --- README.md | 1 - addon/helpers/loc.ts | 46 ------ addon/index.js | 193 ++++++++++++++++++++++++++ addon/index.ts | 58 -------- ember-cli-build.js | 2 +- tests/integration/helpers/loc-test.ts | 17 --- tests/typings/test.ts | 25 +--- tests/unit/loc_test.js | 47 ------- 8 files changed, 195 insertions(+), 194 deletions(-) delete mode 100644 addon/helpers/loc.ts create mode 100644 addon/index.js delete mode 100644 tests/integration/helpers/loc-test.ts delete mode 100644 tests/unit/loc_test.js diff --git a/README.md b/README.md index 6dfcc7f..44235ef 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ import { decamelize, underscore w, - loc, } from '@ember/string' ``` diff --git a/addon/helpers/loc.ts b/addon/helpers/loc.ts deleted file mode 100644 index b9fc8d4..0000000 --- a/addon/helpers/loc.ts +++ /dev/null @@ -1,46 +0,0 @@ -/** - - @module @ember/string - - Calls [loc](/api/classes/Ember.String.html#method_loc) with the - provided string. This is a convenient way to localize text within a template. - For example: - - ```javascript - import { setStrings } from '@ember/string'; - - setStrings({ - '_welcome_': 'Bonjour' - }); - ``` - - ```handlebars -
- {{loc '_welcome_'}} -
- ``` - - ```html -
- Bonjour -
- ``` - - See [Ember.String.loc](/api/classes/Ember.String.html#method_loc) for how to - set up localized string references. - - @method loc - @for Ember.Templates.helpers - @param {String} str The string to format. - @see {Ember.String#loc} - @public - @deprecated Use a l10n/i18n -*/ -import { helper } from '@ember/component/helper'; -import { loc as locUtil } from '@ember/string' - -export function loc(params: [string, any[]]/*, hash*/) { - return locUtil(...params); -} - -export default helper(loc); diff --git a/addon/index.js b/addon/index.js new file mode 100644 index 0000000..6b4f279 --- /dev/null +++ b/addon/index.js @@ -0,0 +1,193 @@ +/** +@module ember +@submodule ember-runtime +*/ +import Cache from './cache'; + +const STRING_DASHERIZE_REGEXP = (/[ _]/g); + +const STRING_DASHERIZE_CACHE = new Cache(1000, key => decamelize(key).replace(STRING_DASHERIZE_REGEXP, '-')); + +const STRING_CAMELIZE_REGEXP_1 = (/(-|_|\.|\s)+(.)?/g); +const STRING_CAMELIZE_REGEXP_2 = (/(^|\/)([A-Z])/g); + +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())); + +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(1000, str => { + let replace1 = (match, separator, chr) => chr ? (`_${chr.toUpperCase()}`) : ''; + let replace2 = (match, initialChar, separator, chr) => 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_UNDERSCORE_REGEXP_1 = (/([a-z\d])([A-Z]+)/g); +const STRING_UNDERSCORE_REGEXP_2 = (/-|\s+/g); + +const UNDERSCORE_CACHE = new Cache(1000, str => str.replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2'). + replace(STRING_UNDERSCORE_REGEXP_2, '_').toLowerCase()); + +const STRING_CAPITALIZE_REGEXP = (/(^|\/)([a-z\u00C0-\u024F])/g); + +const CAPITALIZE_CACHE = new Cache(1000, str => str.replace(STRING_CAPITALIZE_REGEXP, (match/*, separator, chr*/) => match.toUpperCase())); + +const STRING_DECAMELIZE_REGEXP = (/([a-z\d])([A-Z])/g); + +const DECAMELIZE_CACHE = new Cache(1000, str => str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase()); + +/** + Splits a string into separate units separated by spaces, eliminating any + empty strings in the process. This is a convenience method for split that + is mostly useful when applied to the `String.prototype`. + + ```javascript + Ember.String.w("alpha beta gamma").forEach(function(key) { + console.log(key); + }); + + // > alpha + // > beta + // > gamma + ``` + + @method w + @param {String} str The string to split + @return {Array} array containing the split strings + @public +*/ +export function w(str) { + return str.split(/\s+/); +} + +/** + Converts a camelized string into all lower case separated by underscores. + + ```javascript + 'innerHTML'.decamelize(); // 'inner_html' + 'action_name'.decamelize(); // 'action_name' + 'css-class-name'.decamelize(); // 'css-class-name' + 'my favorite items'.decamelize(); // 'my favorite items' + ``` + + @method decamelize + @param {String} str The string to decamelize. + @return {String} the decamelized string. + @public +*/ +export function decamelize(str) { + return DECAMELIZE_CACHE.get(str); +} + +/** + Replaces underscores, spaces, or camelCase with dashes. + + ```javascript + 'innerHTML'.dasherize(); // 'inner-html' + 'action_name'.dasherize(); // 'action-name' + 'css-class-name'.dasherize(); // 'css-class-name' + 'my favorite items'.dasherize(); // 'my-favorite-items' + 'privateDocs/ownerInvoice'.dasherize(); // 'private-docs/owner-invoice' + ``` + + @method dasherize + @param {String} str The string to dasherize. + @return {String} the dasherized string. + @public +*/ +export function dasherize(str) { + return STRING_DASHERIZE_CACHE.get(str); +} + +/** + Returns the lowerCamelCase form of a string. + + ```javascript + 'innerHTML'.camelize(); // 'innerHTML' + 'action_name'.camelize(); // 'actionName' + 'css-class-name'.camelize(); // 'cssClassName' + 'my favorite items'.camelize(); // 'myFavoriteItems' + 'My Favorite Items'.camelize(); // 'myFavoriteItems' + 'private-docs/owner-invoice'.camelize(); // 'privateDocs/ownerInvoice' + ``` + + @method camelize + @param {String} str The string to camelize. + @return {String} the camelized string. + @public +*/ +export function camelize(str) { + return CAMELIZE_CACHE.get(str); +} + +/** + Returns the UpperCamelCase form of a string. + + ```javascript + 'innerHTML'.classify(); // 'InnerHTML' + 'action_name'.classify(); // 'ActionName' + 'css-class-name'.classify(); // 'CssClassName' + 'my favorite items'.classify(); // 'MyFavoriteItems' + 'private-docs/owner-invoice'.classify(); // 'PrivateDocs/OwnerInvoice' + ``` + + @method classify + @param {String} str the string to classify + @return {String} the classified string + @public +*/ +export function classify(str) { + return CLASSIFY_CACHE.get(str); +} + +/** + More general than decamelize. Returns the lower\_case\_and\_underscored + form of a string. + + ```javascript + 'innerHTML'.underscore(); // 'inner_html' + 'action_name'.underscore(); // 'action_name' + 'css-class-name'.underscore(); // 'css_class_name' + 'my favorite items'.underscore(); // 'my_favorite_items' + 'privateDocs/ownerInvoice'.underscore(); // 'private_docs/owner_invoice' + ``` + + @method underscore + @param {String} str The string to underscore. + @return {String} the underscored string. + @public +*/ +export function underscore(str) { + return UNDERSCORE_CACHE.get(str); +} + +/** + Returns the Capitalized form of a string + + ```javascript + 'innerHTML'.capitalize() // 'InnerHTML' + 'action_name'.capitalize() // 'Action_name' + 'css-class-name'.capitalize() // 'Css-class-name' + 'my favorite items'.capitalize() // 'My favorite items' + 'privateDocs/ownerInvoice'.capitalize(); // 'PrivateDocs/ownerInvoice' + ``` + + @method capitalize + @param {String} str The string to capitalize. + @return {String} The capitalized string. + @public +*/ +export function capitalize(str) { + return CAPITALIZE_CACHE.get(str); +} diff --git a/addon/index.ts b/addon/index.ts index 6e0a766..a19391e 100644 --- a/addon/index.ts +++ b/addon/index.ts @@ -2,8 +2,6 @@ @module @ember/string */ import Cache from './cache'; -import { deprecate } from '@ember/application/deprecations'; - // STATE within a module is frowned upon, this exists // to support Ember.STRINGS but shield ember internals from this legacy global @@ -81,62 +79,6 @@ const DECAMELIZE_CACHE = new Cache(1000, str => str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase() ); -function _fmt(str: string, formats: any[]) { - // first, replace any ORDERED replacements. - let idx = 0; // the current index for non-numerical replacements - return str.replace(/%@([0-9]+)?/g, (_s: string, argIndex: string) => { - let i = argIndex ? parseInt(argIndex, 10) - 1 : idx++; - let r = i < formats.length ? formats[i] : undefined; - return typeof r === 'string' ? r : r === null ? '(null)' : r === undefined ? '' : String(r); - }); -} - -/** - Formats the passed string, but first looks up the string in the localized - strings hash. This is a convenient way to localize text. See - `fmt` for more information on formatting. - - Note that it is traditional but not required to prefix localized string - keys with an underscore or other character so you can easily identify - localized strings. - - ```javascript - import { setStrings, loc } from '@ember/string'; - - setStrings({ - '_Hello World': 'Bonjour le monde', - '_Hello %@ %@': 'Bonjour %@ %@' - }); - - loc("_Hello World"); // 'Bonjour le monde'; - loc("_Hello %@ %@", ["John", "Smith"]); // "Bonjour John Smith"; - ``` - - @method loc - @param {String} str The string to format - @param {Array} formats Optional array of parameters to interpolate into string. - @return {String} formatted string - @public -*/ -export function loc(str: string, formats: any[]): string { - deprecate( - 'loc is deprecated, use an internationalization or localization addon instead.', - false, - { - id: 'ember-string-loc', - until: '2.0.0', - // @ts-ignore requires https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32538 to be merged - url: 'http://emberjs.com/deprecations/v2.x#toc_ember-string-loc' - } - ); - if (!Array.isArray(formats) || arguments.length > 2) { - formats = Array.prototype.slice.call(arguments, 1); - } - - str = STRINGS[str] || str; - return _fmt(str, formats); -} - /** Splits a string into separate units separated by spaces, eliminating any empty strings in the process. This is a convenience method for split that diff --git a/ember-cli-build.js b/ember-cli-build.js index db62c3a..e9b7606 100644 --- a/ember-cli-build.js +++ b/ember-cli-build.js @@ -8,7 +8,7 @@ module.exports = function(defaults) { 'ember-cli-babel': { emberModulesAPIPolyfillBlacklist: { ['@ember/string']: [ - 'fmt', 'loc', 'w', + 'w', 'getStrings', 'setStrings', 'decamelize', 'dasherize', 'camelize', 'classify', 'underscore', 'capitalize', diff --git a/tests/integration/helpers/loc-test.ts b/tests/integration/helpers/loc-test.ts deleted file mode 100644 index 0f292f2..0000000 --- a/tests/integration/helpers/loc-test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { module, test } from 'qunit'; -import { setupRenderingTest } from 'ember-qunit'; -import { render } from '@ember/test-helpers'; -import hbs from 'htmlbars-inline-precompile'; - -module('Integration | Helper | loc', function(hooks) { - setupRenderingTest(hooks); - - // Replace this with your real tests. - test('it renders', async function(assert) { - this.set('inputValue', '1234'); - - await render(hbs`{{loc inputValue}}`); - - assert.equal(this.element.textContent!.trim(), '1234'); - }); -}); diff --git a/tests/typings/test.ts b/tests/typings/test.ts index 3cf88ff..1b9a9f5 100644 --- a/tests/typings/test.ts +++ b/tests/typings/test.ts @@ -1,32 +1,9 @@ import { camelize, capitalize, classify, dasherize, - decamelize, fmt, getStrings, loc, setStrings, + decamelize, getStrings, loc, setStrings, underscore, w } from "../../index"; -function testFmt() { - fmt("Hello %@ %@", [ 'John', 'Doe' ]); // "Hello John Doe" - fmt("Hello %@2, %@1", [ 'John', 'Doe' ]); // "Hello Doe, John" - fmt("Hello %@ %@", 'John', 'Doe'); // "Hello John Doe" - fmt('data: %@', [{ id: 3 }]); - fmt('%@', 'John'); -} - -function testLoc() { - let oldStrings = getStrings(); - setStrings({ - '_Hello World': 'Bonjour le monde', - '_Hello %@': 'Bonjour %@', - '_Hello %@ %@': 'Bonjour %@ %@', - '_Hello %@# %@#': 'Bonjour %@2 %@1' - }); - - loc("_Hello World"); // 'Bonjour le monde'; - loc("_Hello %@ %@", ["John", "Smith"]); // "Bonjour John Smith"; - loc('_Hello %@', 'John'); - loc('_Hello %@ %@', ['John'], 'Doe'); -} - function testW() { w("alpha beta gamma").forEach(function(key) { console.log(key); diff --git a/tests/unit/loc_test.js b/tests/unit/loc_test.js deleted file mode 100644 index 023c2d4..0000000 --- a/tests/unit/loc_test.js +++ /dev/null @@ -1,47 +0,0 @@ -import { - module, - test as qunitTest -} from 'qunit'; -import { - loc, - getStrings, - setStrings -} from '@ember/string'; - -let oldStrings; - -module('loc', { - beforeEach() { - oldStrings = getStrings(); - setStrings({ - '_Hello World': 'Bonjour le monde', - '_Hello %@': 'Bonjour %@', - '_Hello %@ %@': 'Bonjour %@ %@', - '_Hello %@# %@#': 'Bonjour %@2 %@1' - }); - }, - - afterEach() { - setStrings(oldStrings); - } -}); - -function test(given, args, expected, description) { - qunitTest(description, function(assert) { - assert.equal(loc(given, args), expected); - - assert.expectDeprecation(); - }); -} - -test('_Hello World', [], 'Bonjour le monde', `loc('_Hello World') => 'Bonjour le monde'`); -test('_Hello %@ %@', ['John', 'Doe'], 'Bonjour John Doe', `loc('_Hello %@ %@', ['John', 'Doe']) => 'Bonjour John Doe'`); -test('_Hello %@# %@#', ['John', 'Doe'], 'Bonjour Doe John', `loc('_Hello %@# %@#', ['John', 'Doe']) => 'Bonjour Doe John'`); -test('_Not In Strings', [], '_Not In Strings', `loc('_Not In Strings') => '_Not In Strings'`); - -qunitTest('works with argument form', function(assert) { - assert.equal(loc('_Hello %@', 'John'), 'Bonjour John'); - assert.equal(loc('_Hello %@ %@', ['John'], 'Doe'), 'Bonjour John Doe'); - - assert.expectDeprecation(); -});