From c0fd0a4f3e62f67ec54b8af7a2cf906fb7f2219a Mon Sep 17 00:00:00 2001 From: Jan Bobisud Date: Sun, 12 Jun 2022 09:59:48 +0200 Subject: [PATCH] Allow to override format* methods on service, fixes #1684 --- addon/services/intl.js | 31 +++++++++++++++++++++---------- package.json | 6 +++++- tests/unit/services/intl-test.ts | 23 +++++++++++++++++++++-- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/addon/services/intl.js b/addon/services/intl.js index 733ade466..551030e51 100644 --- a/addon/services/intl.js +++ b/addon/services/intl.js @@ -64,22 +64,34 @@ export default class extends Service { } /** @public **/ - formatRelative = createFormatterProxy('relative'); + formatRelative(value, formatOptions) { + return this._format('relative', value, formatOptions); + } /** @public **/ - formatMessage = createFormatterProxy('message'); + formatMessage(value, formatOptions) { + return this._format('message', value, formatOptions); + } /** @public **/ - formatNumber = createFormatterProxy('number'); + formatNumber(value, formatOptions) { + return this._format('number', value, formatOptions); + } /** @public **/ - formatTime = createFormatterProxy('time'); + formatTime(value, formatOptions) { + return this._format('time', value, formatOptions); + } /** @public **/ - formatDate = createFormatterProxy('date'); + formatDate(value, formatOptions) { + return this._format('date', value, formatOptions); + } /** @public **/ - formatList = createFormatterProxy('list'); + formatList(value, formatOptions) { + return this._format('list', value, formatOptions); + } /** @private **/ @tracked _locale = null; @@ -346,10 +358,9 @@ export default class extends Service { this._ee.off('localeChanged', ...args); }; } -} -function createFormatterProxy(name) { - return function serviceFormatterProxy(value, formatOptions) { + /** @private */ + _format(name, value, formatOptions) { let locale; let intl; if (formatOptions && formatOptions.locale) { @@ -365,5 +376,5 @@ function createFormatterProxy(name) { } return this._formatters[name].format(intl, value, formatOptions); - }; + } } diff --git a/package.json b/package.json index f8b0b25ba..8c27832d7 100644 --- a/package.json +++ b/package.json @@ -135,6 +135,10 @@ "engines": { "node": "14.* || >= 16" }, + "volta": { + "node": "14.19.3", + "yarn": "1.22.11" + }, "ember": { "edition": "octane" }, @@ -162,4 +166,4 @@ "configPath": "tests/dummy/config", "demoURL": "https://ember-intl.github.io/ember-intl/" } -} +} \ No newline at end of file diff --git a/tests/unit/services/intl-test.ts b/tests/unit/services/intl-test.ts index e30e91f16..2ffc70510 100644 --- a/tests/unit/services/intl-test.ts +++ b/tests/unit/services/intl-test.ts @@ -6,11 +6,12 @@ import { setupTest } from 'ember-qunit'; import { get } from '@ember/object'; import td from 'testdouble'; import { setupIntl, TestContext } from 'ember-intl/test-support'; -import type { TOptions } from 'ember-intl/services/intl'; +import IntlService from 'ember-intl/services/intl'; +import { TOptions } from 'ember-intl/services/intl'; import { next } from '@ember/runloop'; const LOCALE = 'en-us'; -module('service:init initialization', function (hooks) { +module('service:intl initialization', function (hooks) { setupTest(hooks); test('it calls `setLocale` on init', async function (this: TestContext, assert) { @@ -26,6 +27,24 @@ module('service:init initialization', function (hooks) { }); }); +module('service:intl inheritance', function (hooks) { + setupTest(hooks); + + test('it calls overriden method', async function (this: TestContext, assert) { + assert.expect(2); + + this.owner.register('service:intl', class extends IntlService { + formatNumber(value, options) { + assert.step('custom formatNumber') + return super.formatNumber(value, options); + } + }) + + this.owner.lookup('service:intl').formatNumber(1) + assert.verifySteps(['custom formatNumber']) + }); +}); + module('service:intl', function (hooks) { setupTest(hooks); setupIntl(hooks, LOCALE, {}, { missingMessage: false });