diff --git a/frappe/models/index.ts b/frappe/models/index.ts index ce29ca97..c9c01faf 100644 --- a/frappe/models/index.ts +++ b/frappe/models/index.ts @@ -1,4 +1,4 @@ -import NumberSeries from './NumberSeries.js'; +import NumberSeries from './NumberSeries'; import SystemSettings from './SystemSettings'; export default { diff --git a/frappe/utils/consts.js b/frappe/utils/consts.ts similarity index 100% rename from frappe/utils/consts.js rename to frappe/utils/consts.ts diff --git a/frappe/utils/index.js b/frappe/utils/index.js deleted file mode 100644 index 23ed1831..00000000 --- a/frappe/utils/index.js +++ /dev/null @@ -1,78 +0,0 @@ -import { pesa } from 'pesa'; - -export function slug(str) { - return str - .replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) { - return index == 0 ? letter.toLowerCase() : letter.toUpperCase(); - }) - .replace(/\s+/g, ''); -} - -export function getRandomString() { - return Math.random().toString(36).substr(3); -} - -export async function sleep(seconds) { - return new Promise((resolve) => { - setTimeout(resolve, seconds * 1000); - }); -} - -export function getQueryString(params) { - if (!params) return ''; - let parts = []; - for (let key in params) { - if (key != null && params[key] != null) { - parts.push( - encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) - ); - } - } - return parts.join('&'); -} - -export function asyncHandler(fn) { - return (req, res, next) => - Promise.resolve(fn(req, res, next)).catch((err) => { - console.log(err); - // handle error - res.status(err.statusCode || 500).send({ error: err.message }); - }); -} - -/** - * Returns array from 0 to n - 1 - * @param {Number} n - */ -export function range(n) { - return Array(n) - .fill() - .map((_, i) => i); -} - -export function unique(list, key = (it) => it) { - var seen = {}; - return list.filter((item) => { - var k = key(item); - return seen.hasOwnProperty(k) ? false : (seen[k] = true); - }); -} - -export function getDuplicates(array) { - let duplicates = []; - for (let i in array) { - let previous = array[i - 1]; - let current = array[i]; - - if (current === previous) { - if (!duplicates.includes(current)) { - duplicates.push(current); - } - } - } - return duplicates; -} - -export function isPesa(value) { - return value instanceof pesa().constructor; -} diff --git a/frappe/utils/index.ts b/frappe/utils/index.ts new file mode 100644 index 00000000..0af1b050 --- /dev/null +++ b/frappe/utils/index.ts @@ -0,0 +1,52 @@ +import { pesa } from 'pesa'; + +export function slug(str: string) { + return str + .replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) { + return index == 0 ? letter.toLowerCase() : letter.toUpperCase(); + }) + .replace(/\s+/g, ''); +} + +export function getRandomString() { + return Math.random().toString(36).substr(3); +} + +export async function sleep(seconds: number) { + return new Promise((resolve) => { + setTimeout(resolve, seconds * 1000); + }); +} + +export function range(n: number) { + return Array(n) + .fill(null) + .map((_, i) => i); +} + +export function unique(list: unknown[], key = (it: unknown) => String(it)) { + const seen: Record = {}; + return list.filter((item) => { + const k = key(item); + return seen.hasOwnProperty(k) ? false : (seen[k] = true); + }); +} + +export function getDuplicates(array: unknown[]) { + const duplicates: unknown[] = []; + for (const i in array) { + const previous = array[parseInt(i) - 1]; + const current = array[i]; + + if (current === previous) { + if (!duplicates.includes(current)) { + duplicates.push(current); + } + } + } + return duplicates; +} + +export function isPesa(value: unknown): boolean { + return value instanceof pesa().constructor; +} diff --git a/frappe/utils/translation.js b/frappe/utils/translation.ts similarity index 61% rename from frappe/utils/translation.js rename to frappe/utils/translation.ts index 5a0847d4..bca98963 100644 --- a/frappe/utils/translation.js +++ b/frappe/utils/translation.ts @@ -1,3 +1,4 @@ +import { LanguageMap } from 'utils/types'; import { getIndexFormat, getIndexList, @@ -7,7 +8,13 @@ import { import { ValueError } from './errors'; class TranslationString { - constructor(...args) { + args: TemplateStringsArray; + argList?: string[]; + strList?: string[]; + context?: string; + languageMap?: LanguageMap; + + constructor(...args: TemplateStringsArray) { this.args = args; } @@ -15,12 +22,12 @@ class TranslationString { return this.toString(); } - ctx(context) { + ctx(context?: string) { this.context = context; return this; } - #formatArg(arg) { + #formatArg(arg: string) { return arg ?? ''; } @@ -29,16 +36,16 @@ class TranslationString { indexFormat = getWhitespaceSanitized(indexFormat); const translatedIndexFormat = - this.languageMap[indexFormat]?.translation ?? indexFormat; + this.languageMap![indexFormat]?.translation ?? indexFormat; this.argList = getIndexList(translatedIndexFormat).map( - (i) => this.argList[i] + (i) => this.argList![i] ); this.strList = getSnippets(translatedIndexFormat); } #stitch() { - if (!(this.args[0] instanceof Array)) { + if (!((this.args[0] as any) instanceof Array)) { throw new ValueError( `invalid args passed to TranslationString ${ this.args @@ -46,15 +53,14 @@ class TranslationString { ); } - this.strList = this.args[0]; + this.strList = this.args[0] as any as string[]; this.argList = this.args.slice(1); if (this.languageMap) { this.#translate(); } - return this.strList - .map((s, i) => s + this.#formatArg(this.argList[i])) + return this.strList!.map((s, i) => s + this.#formatArg(this.argList![i])) .join('') .replace(/\s+/g, ' ') .trim(); @@ -73,14 +79,16 @@ class TranslationString { } } -export function T(...args) { +export function T(...args: TemplateStringsArray): TranslationString { + // @ts-ignore return new TranslationString(...args); } -export function t(...args) { +export function t(...args: TemplateStringsArray): string { + // @ts-ignore return new TranslationString(...args).s; } -export function setLanguageMapOnTranslationString(languageMap) { +export function setLanguageMapOnTranslationString(languageMap: LanguageMap) { TranslationString.prototype.languageMap = languageMap; } diff --git a/package.json b/package.json index 25e5b7e7..dae51cd5 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@types/assert": "^1.5.6", "@types/electron-devtools-installer": "^2.2.0", "@types/lodash": "^4.14.179", + "@types/luxon": "^2.3.1", "@types/mocha": "^9.1.0", "@types/node": "^17.0.23", "@typescript-eslint/eslint-plugin": "^4.15.1", diff --git a/src/getLanguageMap.ts b/src/getLanguageMap.ts index c9a5e2cb..d0a846ad 100644 --- a/src/getLanguageMap.ts +++ b/src/getLanguageMap.ts @@ -11,16 +11,14 @@ const fs = require('fs/promises'); const path = require('path'); const fetch = require('node-fetch').default; const { splitCsvLine } = require('../scripts/helpers'); +import { LanguageMap } from '../utils/types'; const VALENTINES_DAY = 1644796800000; -type Translation = { translation: string; context?: string }; -type TranslationMap = Record; - export async function getLanguageMap( code: string, isDevelopment: boolean = false -): Promise { +): Promise { const contents = await getContents(code, isDevelopment); return getMapFromContents(contents); } @@ -46,7 +44,7 @@ async function getContents(code: string, isDevelopment: boolean) { return contents; } -function getMapFromContents(contents: string): TranslationMap { +function getMapFromContents(contents: string): LanguageMap { const lines: string[] = contents.split('\n').slice(1); return lines .map((l) => splitCsvLine(l) as string[]) @@ -62,7 +60,7 @@ function getMapFromContents(contents: string): TranslationMap { } return acc; - }, {} as TranslationMap); + }, {} as LanguageMap); } async function getContentsIfExists(code: string): Promise { diff --git a/utils/types.ts b/utils/types.ts new file mode 100644 index 00000000..254518a6 --- /dev/null +++ b/utils/types.ts @@ -0,0 +1,2 @@ +export type Translation = { translation: string; context?: string }; +export type LanguageMap = Record; diff --git a/yarn.lock b/yarn.lock index f591ad28..9b85286f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1348,6 +1348,11 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.179.tgz#490ec3288088c91295780237d2497a3aa9dfb5c5" integrity sha512-uwc1x90yCKqGcIOAT6DwOSuxnrAbpkdPsUOZtwrXb4D/6wZs+6qG7QnIawDuZWg0sWpxl+ltIKCaLoMlna678w== +"@types/luxon@^2.3.1": + version "2.3.1" + resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-2.3.1.tgz#e34763178b46232e4c5f079f1706e18692415519" + integrity sha512-nAPUltOT28fal2eDZz8yyzNhBjHw1NEymFBP7Q9iCShqpflWPybxHbD7pw/46jQmT+HXOy1QN5hNTms8MOTlOQ== + "@types/mime@^1": version "1.3.2" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a" @@ -7887,12 +7892,7 @@ minimatch@4.2.1: dependencies: brace-expansion "^1.1.7" -minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5: - version "1.2.6" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44" - integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q== - -minimist@^1.2.6: +minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6: version "1.2.6" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44" integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==