Skip to content

Commit

Permalink
allow leading and trailing space when looking up formatter (#46)
Browse files Browse the repository at this point in the history
feat: allow leading and trailing space when looking up formatter
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 25, 2021
1 parent e97c272 commit 2eac975
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class NumberFormatterRegistry extends RegistryWithDefaultKey {
}

get(formatterId) {
const targetFormat = formatterId || this.defaultKey;
const targetFormat = (formatterId || this.defaultKey).trim();

if (this.has(targetFormat)) {
return super.get(targetFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describe('NumberFormatterRegistry', () => {
const formatter = registry.get();
expect(formatter.format(100)).toEqual('100.0');
});
it('removes leading and trailing spaces from format', () => {
const formatter = registry.get(' .2f');
expect(formatter).toBeInstanceOf(NumberFormatter);
expect(formatter.format(100)).toEqual('100.00');
const formatter2 = registry.get('.2f ');
expect(formatter2).toBeInstanceOf(NumberFormatter);
expect(formatter2.format(100)).toEqual('100.00');
const formatter3 = registry.get(' .2f ');
expect(formatter3).toBeInstanceOf(NumberFormatter);
expect(formatter3.format(100)).toEqual('100.00');
});
});
describe('.format(format, value)', () => {
it('return the value with the specified format', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class TimeFormatterRegistry extends RegistryWithDefaultKey {
}

get(format) {
const targetFormat = format || this.defaultKey;
const targetFormat = (format || this.defaultKey).trim();

if (this.has(targetFormat)) {
return super.get(targetFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('TimeFormatterRegistry', () => {
const formatter = registry.get();
expect(formatter.format(PREVIEW_TIME)).toEqual('14/02/2017');
});
it('removes leading and trailing spaces from format', () => {
const formatter = registry.get(' %Y ');
expect(formatter).toBeInstanceOf(TimeFormatter);
expect(formatter.format(PREVIEW_TIME)).toEqual('2017');
});
});
describe('.format(format, value)', () => {
it('return the value with the specified format', () => {
Expand Down

0 comments on commit 2eac975

Please sign in to comment.