Skip to content

Commit

Permalink
Allow to override format* methods on service, fixes ember-intl#1684
Browse files Browse the repository at this point in the history
  • Loading branch information
bobisjan committed Jun 12, 2022
1 parent f9ca2e0 commit c0fd0a4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
31 changes: 21 additions & 10 deletions addon/services/intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -365,5 +376,5 @@ function createFormatterProxy(name) {
}

return this._formatters[name].format(intl, value, formatOptions);
};
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
"engines": {
"node": "14.* || >= 16"
},
"volta": {
"node": "14.19.3",
"yarn": "1.22.11"
},
"ember": {
"edition": "octane"
},
Expand Down Expand Up @@ -162,4 +166,4 @@
"configPath": "tests/dummy/config",
"demoURL": "https://ember-intl.github.io/ember-intl/"
}
}
}
23 changes: 21 additions & 2 deletions tests/unit/services/intl-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 });
Expand Down

0 comments on commit c0fd0a4

Please sign in to comment.