Skip to content

Commit 5c6bd07

Browse files
added normalize value fn
1 parent ab46a73 commit 5c6bd07

File tree

12 files changed

+75
-65
lines changed

12 files changed

+75
-65
lines changed

.eslintrc renamed to .eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ register.js
99
.prettierignore
1010
.nycrc.json
1111
.mocharc.json
12-
.eslintrc.json
1312
.editorconfig

.eslintrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
1717
"rules": {
1818
"@typescript-eslint/explicit-module-boundary-types": "off",
19-
"@typescript-eslint/no-explicit-any": "off",
2019
"eslint-comments/disable-enable-pair": [
2120
"error",
2221
{ "allowWholeFile": true }
2322
],
2423
"eslint-comments/no-unused-disable": "error",
2524
"functional/prefer-readonly-type": "off",
2625
"functional/no-return-void": "off",
27-
"functional/no-mixed-type": "off",
26+
"functional/no-mixed-types": "off",
2827
"import/order": [
2928
"error",
3029
{ "newlines-between": "always", "alphabetize": { "order": "asc" } }

spreadsheet.config.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/core/load.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
GoogleSpreadsheetWorksheet,
44
} from 'google-spreadsheet';
55

6-
import {LoadSheetOptions} from '../types/load';
6+
import { LoadSheetOptions } from '../types/load';
77

88
export const loadSheets = async (
99
doc: Readonly<GoogleSpreadsheet>,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// function for transform regex to interpolation for i18n
2+
import {NormalizeDynamicValueOptions} from "../types/normalize-dynamic-value";
3+
4+
const defaultOptions: NormalizeDynamicValueOptions = {
5+
prefix: '{{',
6+
suffix: '}}',
7+
dynamicValueName: 'value',
8+
}
9+
10+
export const normalizeDynamicValue = (value: string, options?: Readonly<NormalizeDynamicValueOptions> ) => {
11+
12+
const { prefix, suffix, dynamicValueName } = { ...defaultOptions, ...options || {} };
13+
14+
const regex = /(%\d*(?:\.\d*)?[sdf])/g;
15+
const matches = value.match(regex);
16+
if (!matches) {
17+
return value;
18+
}
19+
20+
return matches.reduce((acc, curr, index) => {
21+
const interpolationValue = matches.length > 1 ? `${prefix}${dynamicValueName}${index + 1}${suffix}` : `${prefix}${dynamicValueName}${suffix}`;
22+
return acc.replace(curr, interpolationValue);
23+
}, value);
24+
25+
}

src/core/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {GoogleSpreadsheetWorksheet} from 'google-spreadsheet';
1+
import { GoogleSpreadsheetWorksheet } from 'google-spreadsheet';
22

3-
import {ParsedTranslations, ParserOptions} from '../types/parser';
3+
import { ParsedTranslations, ParserOptions } from '../types/parser';
44

55
const parseSpreadsheet = async (
66
sheet: Readonly<GoogleSpreadsheetWorksheet>,

src/core/write-translations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {mkdirSync, writeFileSync} from 'fs';
1+
import { mkdirSync, writeFileSync } from 'fs';
22

3-
import {ParsedTranslations} from '../types/parser';
3+
import { ParsedTranslations } from '../types/parser';
44

55
export const writeTranslations = (
66
path: string,
@@ -13,7 +13,7 @@ export const writeTranslations = (
1313
Object.keys(translations).forEach((language) => {
1414
writeFileSync(
1515
`${__dirname}/${language}.json`,
16-
JSON.stringify(translations[language], null, 2),
16+
JSON.stringify(translations[language], null, 2)
1717
);
1818
});
1919
return true;

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
export {GoogleSpreadsheet} from 'google-spreadsheet';
2-
export {loadSheets} from './core/load';
3-
export {parseSpreadsheets} from './core/parser';
4-
export {writeTranslations} from './core/write-translations';
1+
export { GoogleSpreadsheet } from 'google-spreadsheet';
2+
export { loadSheets } from './core/load';
3+
export { parseSpreadsheets } from './core/parser';
4+
export { writeTranslations } from './core/write-translations';
5+
export { normalizeDynamicValue } from './core/normalize-dynamic-value';
56

67
export * from './types/load';
78
export * from './types/parser';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface NormalizeDynamicValueOptions {
2+
prefix?: string;
3+
suffix?: string;
4+
dynamicValueName?: string;
5+
}

src/types/utility.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)