Skip to content

Commit

Permalink
Fix formatting and linting. (#8)
Browse files Browse the repository at this point in the history
* Fix formatting and linting.

In first place, let me apologize. I needed to fix the Finnish formatting
but linting got into my editor's way so this is the complete list of
changes:

- Fix formatting for a few languages. Some languages include a dot in
the short format and in the data we get that dot surrounded by single
quotes. This is the main reason for this PR.
- Make the project pass its linting rules.
- Make CI check linting.
- Fix path in tslint.json to properly exclude test/locale-data.ts
- Add test/locale-data.ts to .prettierignore

* Fix versions of node to use

Jest is having a problem with latest unstable node (11.11.0) facebook/create-react-app#6591 so trying to set specific supported versions to fix travis

* Fix specifying versions
  • Loading branch information
Serabe authored and snewcomer committed Mar 12, 2019
1 parent d8c3ead commit d748af3
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 27 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/locale-data.ts
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
language: node_js
node_js:
- "6"
- "8"
- "10"

branches:
only:
Expand All @@ -11,11 +15,8 @@ cache:
notifications:
email: false

node_js:
- node

script:
- npm run test
- npm run test:ci

after_success:
- npm run report-coverage
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"build": "tsc && tsc --outDir dist/es && rollup -c rollup.config.ts",
"lint": "tslint -t codeFrame 'src/**/*.ts' 'test/format.test.ts'",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"prepublishOnly": "npm run test:ci",
"preversion": "npm run lint",
"start": "tsc -w & rollup -c rollup.config.ts -w",
"prebuild": "rimraf dist",
"test": "jest"
"test": "jest",
"test:ci": "npm test && npm run lint"
},
"jest": {
"transform": {
Expand Down Expand Up @@ -60,4 +61,4 @@
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^3.1.5"
}
}
}
4 changes: 4 additions & 0 deletions src/format-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export function replaceNumber(normalized: any, format: string): string {
return format.replace(/0*/, normalized);
}

export function polishString(str: string): string {
return str.replace("'.'", '.');
}

export function normalizeLocale(locale: string | string[]): string {
if (locale instanceof Array) {
return locale[0].replace(/_/, '-').toLowerCase();
Expand Down
3 changes: 2 additions & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
findLocaleData,
needsFormatting,
normalizeLocale,
polishString,
replaceNumber
} from './format-utils';
import { extractIntPart, normalizeNumber } from './math-utils';
Expand Down Expand Up @@ -87,5 +88,5 @@ export function compactFormat(
options
);

return replaceNumber(normalized, formatter);
return polishString(replaceNumber(normalized, formatter));
}
43 changes: 26 additions & 17 deletions test/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import compactFormat from '../src';
import { compactFormat as format } from '../src/format';
import { en, es } from './locale-data';
import { en, es, fi } from './locale-data';

// due to regex replacement
function replaceWhitespace(val: any) {
function normalizeWhitespace(val: any) {
return val.replace(/\s+/, ' ');
}

Expand Down Expand Up @@ -35,29 +35,29 @@ describe('format number', () => {
it('returns value if localeData provided', () => {
const localeData = es;
let result = format(1234, 'es', localeData);
expect(replaceWhitespace(result)).toBe('1 mil');
expect(normalizeWhitespace(result)).toBe('1 mil');
result = format(11234, 'es', localeData);
expect(replaceWhitespace(result)).toBe('11 mil');
expect(normalizeWhitespace(result)).toBe('11 mil');
result = format(94999, 'es', localeData);
expect(replaceWhitespace(result)).toBe('95 mil');
expect(normalizeWhitespace(result)).toBe('95 mil');
result = format(95000, 'es', localeData);
expect(replaceWhitespace(result)).toBe('95 mil');
expect(normalizeWhitespace(result)).toBe('95 mil');
result = format(95001, 'es', localeData);
expect(replaceWhitespace(result)).toBe('100 mil');
expect(normalizeWhitespace(result)).toBe('100 mil');
result = format(100000, 'es', localeData);
expect(replaceWhitespace(result)).toBe('100 mil');
expect(normalizeWhitespace(result)).toBe('100 mil');
});

it('returns value if threshold provided', () => {
const localeData = es;
let result = format(1234, 'es', localeData, { threshold: 0.1 });
expect(replaceWhitespace(result)).toBe('1 mil');
expect(normalizeWhitespace(result)).toBe('1 mil');
result = format(11234, 'es', localeData, { threshold: 0.1 });
expect(replaceWhitespace(result)).toBe('11 mil');
expect(normalizeWhitespace(result)).toBe('11 mil');
result = format(89999, 'es', localeData, { threshold: 0.1 });
expect(replaceWhitespace(result)).toBe('90 mil');
expect(normalizeWhitespace(result)).toBe('90 mil');
result = format(90001, 'es', localeData, { threshold: 0.1 });
expect(replaceWhitespace(result)).toBe('90 mil');
expect(normalizeWhitespace(result)).toBe('90 mil');
// result = format(95001, 'es', localeData, { threshold: 0.1 });
// expect(replaceWhitespace(result)).toBe('100 mil');
});
Expand All @@ -67,15 +67,15 @@ describe('format number', () => {
let result = format(1234, 'es', localeData, {
significantDigits: 1
});
expect(replaceWhitespace(result)).toBe('1.2 mil');
expect(normalizeWhitespace(result)).toBe('1.2 mil');
result = format(11234, 'es', localeData, {
significantDigits: 1
});
expect(replaceWhitespace(result)).toBe('11.2 mil');
expect(normalizeWhitespace(result)).toBe('11.2 mil');
result = format(91934, 'es', localeData, {
significantDigits: 2
});
expect(replaceWhitespace(result)).toBe('91.93 mil');
expect(normalizeWhitespace(result)).toBe('91.93 mil');
});

it('returns with financial format', () => {
Expand All @@ -84,11 +84,20 @@ describe('format number', () => {
financialFormat: true,
significantDigits: 1
});
expect(replaceWhitespace(result)).toBe('1.2K');
expect(normalizeWhitespace(result)).toBe('1.2K');
result = format(101000, 'en', localeData, {
financialFormat: true,
significantDigits: 1
});
expect(replaceWhitespace(result)).toBe('0.1M');
expect(normalizeWhitespace(result)).toBe('0.1M');
});

it("replaces `'.'` in the formatting string with just a .", () => {
const localeData = fi;
const result = format(1234, 'fi', localeData, {
significantDigits: 1
});

expect(normalizeWhitespace(result)).toBe('1.2 t.');
});
});
16 changes: 16 additions & 0 deletions test/locale-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ export const en = {
}
}
}

export const fi = {
fi: {
"locale": "fi",
"numbers": {
"decimal": {
"long": [
[1000, { "one": ["0 tuhat", 1], "other": ["0 tuhatta", 1] }], [10000, { "one": ["00 tuhatta", 2], "other": ["00 tuhatta", 2] }], [100000, { "one": ["000 tuhatta", 3], "other": ["000 tuhatta", 3] }], [1000000, { "one": ["0 miljoona", 1], "other": ["0 miljoonaa", 1] }], [10000000, { "one": ["00 miljoonaa", 2], "other": ["00 miljoonaa", 2] }], [100000000, { "one": ["000 miljoonaa", 3], "other": ["000 miljoonaa", 3] }], [1000000000, { "one": ["0 miljardi", 1], "other": ["0 miljardia", 1] }], [10000000000, { "one": ["00 miljardia", 2], "other": ["00 miljardia", 2] }], [100000000000, { "one": ["000 miljardia", 3], "other": ["000 miljardia", 3] }], [1000000000000, { "one": ["0 biljoona", 1], "other": ["0 biljoonaa", 1] }], [10000000000000, { "one": ["00 biljoonaa", 2], "other": ["00 biljoonaa", 2] }], [100000000000000, { "one": ["000 biljoonaa", 3], "other": ["000 biljoonaa", 3] }]
],
"short": [
[1000, { "one": ["0 t'.'", 1], "other": ["0 t'.'", 1] }], [10000, { "one": ["00 t'.'", 2], "other": ["00 t'.'", 2] }], [100000, { "one": ["000 t'.'", 3], "other": ["000 t'.'", 3] }], [1000000, { "one": ["0 milj'.'", 1], "other": ["0 milj'.'", 1] }], [10000000, { "one": ["00 milj'.'", 2], "other": ["00 milj'.'", 2] }], [100000000, { "one": ["000 milj'.'", 3], "other": ["000 milj'.'", 3] }], [1000000000, { "one": ["0 mrd'.'", 1], "other": ["0 mrd'.'", 1] }], [10000000000, { "one": ["00 mrd'.'", 2], "other": ["00 mrd'.'", 2] }], [100000000000, { "one": ["000 mrd'.'", 3], "other": ["000 mrd'.'", 3] }], [1000000000000, { "one": ["0 bilj'.'", 1], "other": ["0 bilj'.'", 1] }], [10000000000000, { "one": ["00 bilj'.'", 2], "other": ["00 bilj'.'", 2] }], [100000000000000, { "one": ["000 bilj'.'", 3], "other": ["000 bilj'.'", 3] }]
]
}
}
}
};
4 changes: 2 additions & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"linterOptions": {
"exclude": [
"tests/locale-data.ts"
"test/locale-data.ts"
]
},
"jsRules": {},
Expand All @@ -22,4 +22,4 @@
"rulesDirectory": [
"tslint-plugin-prettier"
]
}
}

0 comments on commit d748af3

Please sign in to comment.