@ewb/translate
Advanced tools
Comparing version 1.8.2 to 1.9.0
@@ -14,2 +14,7 @@ import Empty from './Empty'; | ||
} | ||
export interface TranslationAddOptions { | ||
apiID?: string; | ||
packageName?: string | null; | ||
} | ||
export declare type TranslationExportFilter = Pick<TranslationAddOptions, 'packageName'>; | ||
export default class Branch extends ApiBranch { | ||
@@ -21,4 +26,6 @@ level: number; | ||
usageStack: TranslationUsage[]; | ||
constructor(level: number, word: string, sentence?: boolean, translations?: Translations, apiID?: string); | ||
add(newWord: string, translations?: Translations, apiID?: string): boolean; | ||
readonly apiID: string; | ||
readonly packageName: string | null; | ||
constructor(level: number, word: string, sentence?: boolean, translations?: Translations, opts?: TranslationAddOptions); | ||
add(newWord: string, translations?: Translations, opts?: TranslationAddOptions): boolean; | ||
find(word: string): Branch | Empty; | ||
@@ -30,5 +37,6 @@ addTranslations(translations: Translations): void; | ||
wordCount(): number; | ||
export(): WordTranslations; | ||
export(filter?: TranslationExportFilter): WordTranslations; | ||
toString(): string; | ||
map(fn: (branch: Branch) => any): any[]; | ||
private notFiltered; | ||
private getNextCharacter; | ||
@@ -35,0 +43,0 @@ private match; |
@@ -10,3 +10,3 @@ "use strict"; | ||
class Branch extends ApiBranch_1.default { | ||
constructor(level, word, sentence = false, translations = {}, apiID = '') { | ||
constructor(level, word, sentence = false, translations = {}, opts = {}) { | ||
super(helpers_1.getWord(level, word, sentence)); | ||
@@ -18,5 +18,7 @@ this.level = level; | ||
this.usageStack = []; | ||
this.apiID = ''; | ||
this.packageName = null; | ||
const char = this.getNextCharacter(word); | ||
if (char) { | ||
this.words[char] = new Branch(this.level + 1, word, sentence, translations, apiID); | ||
this.words[char] = new Branch(this.level + 1, word, sentence, translations, opts); | ||
} | ||
@@ -26,6 +28,7 @@ else { | ||
this.isWord = true; | ||
this.apiID = apiID; | ||
this.apiID = (opts === null || opts === void 0 ? void 0 : opts.apiID) || ''; | ||
this.packageName = (opts === null || opts === void 0 ? void 0 : opts.packageName) || null; | ||
} | ||
} | ||
add(newWord, translations, apiID) { | ||
add(newWord, translations, opts = {}) { | ||
if (this.match(newWord)) { | ||
@@ -42,6 +45,6 @@ if (!this.isWord) { | ||
if (this.words[char]) { | ||
this.words[char].add(newWord, translations, apiID); | ||
this.words[char].add(newWord, translations, opts); | ||
} | ||
else { | ||
this.words[char] = new Branch(this.level + 1, newWord, this.sentence, translations, apiID); | ||
this.words[char] = new Branch(this.level + 1, newWord, this.sentence, translations, opts); | ||
} | ||
@@ -58,10 +61,10 @@ return true; | ||
const pathArr = error.stack.split('\n'); | ||
const filterPaths = pathArr.filter(x => !x.match(FILTER_STACK_PATHS)); | ||
const paths = filterPaths.slice(1).map(x => x.split('at ')[1] || ''); | ||
const filterPaths = pathArr.filter((x) => !x.match(FILTER_STACK_PATHS)); | ||
const paths = filterPaths.slice(1).map((x) => x.split('at ')[1] || ''); | ||
const file = paths[0].split(' ')[0]; | ||
const isTranslations = !pathArr.some(x => x.match(FILTER_STACK_TRANS)); | ||
if (isTranslations && this.usageStack.every(x => x.file !== file)) { | ||
const isTranslations = !pathArr.some((x) => x.match(FILTER_STACK_TRANS)); | ||
if (isTranslations && this.usageStack.every((x) => x.file !== file)) { | ||
this.usageStack.push({ | ||
file, | ||
stack: filterPaths.join('\n') | ||
stack: filterPaths.join('\n'), | ||
}); | ||
@@ -95,15 +98,29 @@ } | ||
wordCount() { | ||
return this.map(x => x.wordCount()).reduce(math_1.add, 0) + 1; | ||
return this.map((x) => x.wordCount()).reduce(math_1.add, 0) + 1; | ||
} | ||
export() { | ||
return this.isWord | ||
? Object.assign({ [this.word]: this.translations }, this.map(x => x.export()).reduce(helpers_1.arrayToObject, {})) : this.map(x => x.export()).reduce(helpers_1.arrayToObject, {}); | ||
export(filter) { | ||
if (this.isWord) { | ||
if (this.notFiltered(filter)) { | ||
return Object.assign({ [this.word]: this.translations }, this.map((x) => x.export(filter)).reduce(helpers_1.arrayToObject, {})); | ||
} | ||
} | ||
return this.map((x) => x.export(filter)).reduce(helpers_1.arrayToObject, {}); | ||
} | ||
toString() { | ||
const str = `${this.level} ${this.word} \n`; | ||
return str + this.map(x => x.toString()).join(''); | ||
return str + this.map((x) => x.toString()).join(''); | ||
} | ||
map(fn) { | ||
return Object.keys(this.words).map(k => fn(this.words[k])); | ||
return Object.keys(this.words).map((k) => fn(this.words[k])); | ||
} | ||
notFiltered(filter) { | ||
if (!filter) { | ||
return true; | ||
} | ||
if (typeof filter.packageName === 'undefined' || | ||
filter.packageName === this.packageName) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
getNextCharacter(word) { | ||
@@ -110,0 +127,0 @@ if (this.sentence) { |
@@ -18,10 +18,16 @@ "use strict"; | ||
if (this.branch instanceof Branch_1.default) { | ||
this.branch.add(this.word, translations, this.apiID); | ||
this.branch.add(this.word, translations, { | ||
apiID: this.apiID, | ||
}); | ||
} | ||
else { | ||
if (this.isTreeText) { | ||
this.branch.addText(this.word, translations, this.apiID); | ||
this.branch.addText(this.word, translations, { | ||
apiID: this.apiID, | ||
}); | ||
} | ||
else { | ||
this.branch.addWord(this.word, translations, this.apiID); | ||
this.branch.addWord(this.word, translations, { | ||
apiID: this.apiID, | ||
}); | ||
} | ||
@@ -28,0 +34,0 @@ } |
import Tree, { WordTranslations, TreeOptions } from './Tree'; | ||
import Empty from './Empty'; | ||
import Branch, { Translations, TranslationUsage, BranchObject } from './Branch'; | ||
import Branch, { Translations, TranslationUsage, BranchObject, TranslationAddOptions, TranslationExportFilter } from './Branch'; | ||
import { ISO_639_1 } from './utils/iso_639_1'; | ||
@@ -38,4 +38,4 @@ import { TranslationApi } from './TranslationApi'; | ||
}; | ||
exportWords(): WordTranslations; | ||
exportTexts(): WordTranslations; | ||
exportWords(filter?: TranslationExportFilter): WordTranslations; | ||
exportTexts(filter?: TranslationExportFilter): WordTranslations; | ||
exportBranches(): Branch[]; | ||
@@ -47,4 +47,4 @@ getWord(word: string): Branch | null; | ||
setLocale(locale: string): void; | ||
addWords(words: WordTranslations): void; | ||
addTexts(texts: WordTranslations): void; | ||
addWords(words: WordTranslations, opts?: TranslationAddOptions): void; | ||
addTexts(texts: WordTranslations, opts?: TranslationAddOptions): void; | ||
private _branch; | ||
@@ -51,0 +51,0 @@ private replaceVariables; |
@@ -68,7 +68,7 @@ "use strict"; | ||
} | ||
exportWords() { | ||
return this.tree.exportWords(); | ||
exportWords(filter) { | ||
return this.tree.exportWords(filter); | ||
} | ||
exportTexts() { | ||
return this.tree.exportTexts(); | ||
exportTexts(filter) { | ||
return this.tree.exportTexts(filter); | ||
} | ||
@@ -99,7 +99,7 @@ exportBranches() { | ||
} | ||
addWords(words) { | ||
Object.keys(words).forEach((key) => this.tree.addWord(key, words[key])); | ||
addWords(words, opts) { | ||
Object.keys(words).forEach((key) => this.tree.addWord(key, words[key], opts)); | ||
} | ||
addTexts(texts) { | ||
Object.keys(texts).forEach((key) => this.tree.addText(key, texts[key])); | ||
addTexts(texts, opts) { | ||
Object.keys(texts).forEach((key) => this.tree.addText(key, texts[key], opts)); | ||
} | ||
@@ -106,0 +106,0 @@ _branch(wordOrText, isText = false) { |
@@ -1,2 +0,2 @@ | ||
import Branch, { BranchObject, Translations } from './Branch'; | ||
import Branch, { BranchObject, TranslationAddOptions, TranslationExportFilter, Translations } from './Branch'; | ||
import Empty from './Empty'; | ||
@@ -16,11 +16,11 @@ export interface WordTranslations { | ||
constructor({ words, texts }: TreeOptions); | ||
addWord(word: string, translations?: Translations, apiID?: string): void; | ||
addText(text: string, translations?: Translations, apiID?: string): void; | ||
addWord(word: string, translations?: Translations, opts?: TranslationAddOptions): void; | ||
addText(text: string, translations?: Translations, opts?: TranslationAddOptions): void; | ||
word(word: string): Branch | Empty; | ||
text(text: string): Branch | Empty; | ||
suggestions(text: boolean): Branch[]; | ||
exportWords(): WordTranslations; | ||
exportTexts(): WordTranslations; | ||
exportWords(filter?: TranslationExportFilter): WordTranslations; | ||
exportTexts(filter?: TranslationExportFilter): WordTranslations; | ||
private wordMap; | ||
private textMap; | ||
} |
@@ -10,21 +10,21 @@ "use strict"; | ||
this.texts = {}; | ||
Object.keys(words).map(k => this.addWord(k, words[k])); | ||
Object.keys(texts).map(k => this.addText(k, texts[k])); | ||
Object.keys(words).map((k) => this.addWord(k, words[k])); | ||
Object.keys(texts).map((k) => this.addText(k, texts[k])); | ||
} | ||
addWord(word, translations, apiID) { | ||
addWord(word, translations, opts) { | ||
const char = helpers_1.getFirst(word); | ||
if (this.words[char]) { | ||
this.words[char].add(word, translations, apiID); | ||
this.words[char].add(word, translations, opts); | ||
} | ||
else { | ||
this.words[char] = new Branch_1.default(0, word, false, translations, apiID); | ||
this.words[char] = new Branch_1.default(0, word, false, translations, opts); | ||
} | ||
} | ||
addText(text, translations, apiID) { | ||
addText(text, translations, opts) { | ||
const word = helpers_1.getFirst(text, true); | ||
if (this.texts[word]) { | ||
this.texts[word].add(text, translations, apiID); | ||
this.texts[word].add(text, translations, opts); | ||
} | ||
else { | ||
this.texts[word] = new Branch_1.default(0, text, true, translations, apiID); | ||
this.texts[word] = new Branch_1.default(0, text, true, translations, opts); | ||
} | ||
@@ -46,16 +46,16 @@ } | ||
return text | ||
? this.textMap(x => x.suggestions()).flat() | ||
: this.wordMap(x => x.suggestions()).flat(); | ||
? this.textMap((x) => x.suggestions()).flat() | ||
: this.wordMap((x) => x.suggestions()).flat(); | ||
} | ||
exportWords() { | ||
return this.wordMap(x => x.export()).reduce(helpers_1.arrayToObject, {}); | ||
exportWords(filter) { | ||
return this.wordMap((x) => x.export(filter)).reduce(helpers_1.arrayToObject, {}); | ||
} | ||
exportTexts() { | ||
return this.textMap(x => x.export()).reduce(helpers_1.arrayToObject, {}); | ||
exportTexts(filter) { | ||
return this.textMap((x) => x.export(filter)).reduce(helpers_1.arrayToObject, {}); | ||
} | ||
wordMap(fn) { | ||
return Object.keys(this.words).map(k => fn(this.words[k])); | ||
return Object.keys(this.words).map((k) => fn(this.words[k])); | ||
} | ||
textMap(fn) { | ||
return Object.keys(this.texts).map(k => fn(this.texts[k])); | ||
return Object.keys(this.texts).map((k) => fn(this.texts[k])); | ||
} | ||
@@ -62,0 +62,0 @@ } |
{ | ||
"name": "@ewb/translate", | ||
"version": "1.8.2", | ||
"version": "1.9.0", | ||
"description": "Translate as you create your app. Saves the words and text in a tree structure for fast and easy lookup.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -20,2 +20,12 @@ import Empty from './Empty'; | ||
export interface TranslationAddOptions { | ||
apiID?: string; | ||
packageName?: string | null; // null is root package | ||
} | ||
export type TranslationExportFilter = Pick< | ||
TranslationAddOptions, | ||
'packageName' | ||
>; | ||
const FILTER_STACK_PATHS = /(Branch|Tree|Translate)\./; | ||
@@ -28,2 +38,4 @@ const FILTER_STACK_TRANS = /Translate._branch/; | ||
public usageStack: TranslationUsage[] = []; | ||
public readonly apiID: string = ''; | ||
public readonly packageName: string | null = null; | ||
@@ -35,3 +47,3 @@ constructor( | ||
translations: Translations = {}, | ||
apiID = '' | ||
opts: TranslationAddOptions = {} | ||
) { | ||
@@ -47,3 +59,3 @@ super(getWord(level, word, sentence)); | ||
translations, | ||
apiID | ||
opts | ||
); | ||
@@ -53,7 +65,12 @@ } else { | ||
this.isWord = true; | ||
this.apiID = apiID; | ||
this.apiID = opts?.apiID || ''; | ||
this.packageName = opts?.packageName || null; | ||
} | ||
} | ||
public add(newWord: string, translations?: Translations, apiID?: string) { | ||
public add( | ||
newWord: string, | ||
translations?: Translations, | ||
opts: TranslationAddOptions = {} | ||
) { | ||
if (this.match(newWord)) { | ||
@@ -71,3 +88,3 @@ if (!this.isWord) { | ||
if (this.words[char]) { | ||
this.words[char].add(newWord, translations, apiID); | ||
this.words[char].add(newWord, translations, opts); | ||
} else { | ||
@@ -79,3 +96,3 @@ this.words[char] = new Branch( | ||
translations, | ||
apiID | ||
opts | ||
); | ||
@@ -96,10 +113,12 @@ } | ||
const pathArr = error.stack.split('\n'); | ||
const filterPaths = pathArr.filter(x => !x.match(FILTER_STACK_PATHS)); | ||
const paths = filterPaths.slice(1).map(x => x.split('at ')[1] || ''); | ||
const filterPaths = pathArr.filter((x) => !x.match(FILTER_STACK_PATHS)); | ||
const paths = filterPaths.slice(1).map((x) => x.split('at ')[1] || ''); | ||
const file = paths[0].split(' ')[0]; | ||
const isTranslations = !pathArr.some(x => x.match(FILTER_STACK_TRANS)); | ||
if (isTranslations && this.usageStack.every(x => x.file !== file)) { | ||
const isTranslations = !pathArr.some((x) => | ||
x.match(FILTER_STACK_TRANS) | ||
); | ||
if (isTranslations && this.usageStack.every((x) => x.file !== file)) { | ||
this.usageStack.push({ | ||
file, | ||
stack: filterPaths.join('\n') | ||
stack: filterPaths.join('\n'), | ||
}); | ||
@@ -141,12 +160,15 @@ } | ||
public wordCount(): number { | ||
return this.map(x => x.wordCount()).reduce(add, 0) + 1; | ||
return this.map((x) => x.wordCount()).reduce(add, 0) + 1; | ||
} | ||
public export(): WordTranslations { | ||
return this.isWord | ||
? { | ||
public export(filter?: TranslationExportFilter): WordTranslations { | ||
if (this.isWord) { | ||
if (this.notFiltered(filter)) { | ||
return { | ||
[this.word]: this.translations, | ||
...this.map(x => x.export()).reduce(arrayToObject, {}) | ||
} | ||
: this.map(x => x.export()).reduce(arrayToObject, {}); | ||
...this.map((x) => x.export(filter)).reduce(arrayToObject, {}), | ||
}; | ||
} | ||
} | ||
return this.map((x) => x.export(filter)).reduce(arrayToObject, {}); | ||
} | ||
@@ -156,9 +178,22 @@ | ||
const str = `${this.level} ${this.word} \n`; | ||
return str + this.map(x => x.toString()).join(''); | ||
return str + this.map((x) => x.toString()).join(''); | ||
} | ||
public map(fn: (branch: Branch) => any) { | ||
return Object.keys(this.words).map(k => fn(this.words[k])); | ||
return Object.keys(this.words).map((k) => fn(this.words[k])); | ||
} | ||
private notFiltered(filter?: TranslationExportFilter) { | ||
if (!filter) { | ||
return true; | ||
} | ||
if ( | ||
typeof filter.packageName === 'undefined' || | ||
filter.packageName === this.packageName | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
private getNextCharacter(word: string) { | ||
@@ -165,0 +200,0 @@ if (this.sentence) { |
@@ -23,8 +23,14 @@ import Branch, { Translations } from './Branch'; | ||
if (this.branch instanceof Branch) { | ||
this.branch.add(this.word, translations, this.apiID); | ||
this.branch.add(this.word, translations, { | ||
apiID: this.apiID, | ||
}); | ||
} else { | ||
if (this.isTreeText) { | ||
this.branch.addText(this.word, translations, this.apiID); | ||
this.branch.addText(this.word, translations, { | ||
apiID: this.apiID, | ||
}); | ||
} else { | ||
this.branch.addWord(this.word, translations, this.apiID); | ||
this.branch.addWord(this.word, translations, { | ||
apiID: this.apiID, | ||
}); | ||
} | ||
@@ -31,0 +37,0 @@ } |
import Tree, { WordTranslations, TreeOptions } from './Tree'; | ||
import Empty from './Empty'; | ||
import Branch, { Translations, TranslationUsage, BranchObject } from './Branch'; | ||
import Branch, { | ||
Translations, | ||
TranslationUsage, | ||
BranchObject, | ||
TranslationAddOptions, | ||
TranslationExportFilter, | ||
} from './Branch'; | ||
import { VARIABLE_REGEXP } from './utils/helpers'; | ||
@@ -97,8 +103,8 @@ import { ISO_639_1 } from './utils/iso_639_1'; | ||
public exportWords() { | ||
return this.tree.exportWords(); | ||
public exportWords(filter?: TranslationExportFilter) { | ||
return this.tree.exportWords(filter); | ||
} | ||
public exportTexts() { | ||
return this.tree.exportTexts(); | ||
public exportTexts(filter?: TranslationExportFilter) { | ||
return this.tree.exportTexts(filter); | ||
} | ||
@@ -136,8 +142,12 @@ | ||
public addWords(words: WordTranslations) { | ||
Object.keys(words).forEach((key) => this.tree.addWord(key, words[key])); | ||
public addWords(words: WordTranslations, opts?: TranslationAddOptions) { | ||
Object.keys(words).forEach((key) => | ||
this.tree.addWord(key, words[key], opts) | ||
); | ||
} | ||
public addTexts(texts: WordTranslations) { | ||
Object.keys(texts).forEach((key) => this.tree.addText(key, texts[key])); | ||
public addTexts(texts: WordTranslations, opts?: TranslationAddOptions) { | ||
Object.keys(texts).forEach((key) => | ||
this.tree.addText(key, texts[key], opts) | ||
); | ||
} | ||
@@ -144,0 +154,0 @@ |
@@ -1,2 +0,7 @@ | ||
import Branch, { BranchObject, Translations } from './Branch'; | ||
import Branch, { | ||
BranchObject, | ||
TranslationAddOptions, | ||
TranslationExportFilter, | ||
Translations, | ||
} from './Branch'; | ||
import Empty from './Empty'; | ||
@@ -21,23 +26,31 @@ import { arrayToObject, getFirst } from './utils/helpers'; | ||
constructor({ words = {}, texts = {} }: TreeOptions) { | ||
Object.keys(words).map(k => this.addWord(k, words[k])); | ||
Object.keys(texts).map(k => this.addText(k, texts[k])); | ||
Object.keys(words).map((k) => this.addWord(k, words[k])); | ||
Object.keys(texts).map((k) => this.addText(k, texts[k])); | ||
} | ||
public addWord(word: string, translations?: Translations, apiID?: string) { | ||
public addWord( | ||
word: string, | ||
translations?: Translations, | ||
opts?: TranslationAddOptions | ||
) { | ||
const char = getFirst(word); | ||
if (this.words[char]) { | ||
this.words[char].add(word, translations, apiID); | ||
this.words[char].add(word, translations, opts); | ||
} else { | ||
this.words[char] = new Branch(0, word, false, translations, apiID); | ||
this.words[char] = new Branch(0, word, false, translations, opts); | ||
} | ||
} | ||
public addText(text: string, translations?: Translations, apiID?: string) { | ||
public addText( | ||
text: string, | ||
translations?: Translations, | ||
opts?: TranslationAddOptions | ||
) { | ||
const word = getFirst(text, true); | ||
if (this.texts[word]) { | ||
this.texts[word].add(text, translations, apiID); | ||
this.texts[word].add(text, translations, opts); | ||
} else { | ||
this.texts[word] = new Branch(0, text, true, translations, apiID); | ||
this.texts[word] = new Branch(0, text, true, translations, opts); | ||
} | ||
@@ -62,21 +75,21 @@ } | ||
return text | ||
? this.textMap(x => x.suggestions()).flat() | ||
: this.wordMap(x => x.suggestions()).flat(); | ||
? this.textMap((x) => x.suggestions()).flat() | ||
: this.wordMap((x) => x.suggestions()).flat(); | ||
} | ||
public exportWords() { | ||
return this.wordMap(x => x.export()).reduce(arrayToObject, {}); | ||
public exportWords(filter?: TranslationExportFilter) { | ||
return this.wordMap((x) => x.export(filter)).reduce(arrayToObject, {}); | ||
} | ||
public exportTexts() { | ||
return this.textMap(x => x.export()).reduce(arrayToObject, {}); | ||
public exportTexts(filter?: TranslationExportFilter) { | ||
return this.textMap((x) => x.export(filter)).reduce(arrayToObject, {}); | ||
} | ||
private wordMap<T extends any>(fn: (branch: Branch) => T) { | ||
return Object.keys(this.words).map(k => fn(this.words[k])); | ||
return Object.keys(this.words).map((k) => fn(this.words[k])); | ||
} | ||
private textMap<T extends any>(fn: (branch: Branch) => T) { | ||
return Object.keys(this.texts).map(k => fn(this.texts[k])); | ||
return Object.keys(this.texts).map((k) => fn(this.texts[k])); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99469
1713