Skip to content

Commit

Permalink
refactor: code splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Nov 11, 2022
1 parent 768b4aa commit 8672d90
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/build/xliff/handle-xliff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { resolve } from 'upath2';
import { initIdeaSegmentText } from '../../segment';
import { createMultiBar, createSingleBar } from '../../cli-progress';

export function openXLIFFFile(xliff_file: string, cwd: string)
export function openXLIFFFile(xliff_file: string, cwd?: string)
{
return new CrowdinXLIFFXml(readFileSync(resolve(cwd, xliff_file)))
}
Expand Down
55 changes: 40 additions & 15 deletions lib/util/xml/xliff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ITransUnit
}
}

interface ICrowdinXLIFFXmlFile
export interface ICrowdinXLIFFXmlFile
{
'@id': `${number}`;
/**
Expand Down Expand Up @@ -82,27 +82,52 @@ export class CrowdinXLIFFXml extends XMLSerialized
.replace(/^\<\?xml version="1\.0"\?\>/, '<?xml version="1.0" encoding="UTF-8"?>')
}

splitFiles(maxChunkLength: number = 15)
chunkFiles(maxChunkLength: number = 15)
{
return arrayChunkSplit([this.files].flat(), maxChunkLength).map((files, i) => {
return arrayChunkSplit([this.files].flat(), maxChunkLength)
}

const id = files.map(file => file['@id']);
splitFiles(maxChunkLength: number = 15)
{
return this._splitFiles(this.chunkFiles(maxChunkLength))
}

_splitFiles(chunk: ICrowdinXLIFFXmlFile[][])
{
return chunk.map((files, i) =>
{
return {
name: i.toString().padStart(3, '0'),
id,
xml: this.toString.call({
_headless: this._headless,
raw: {
...this.raw,
xliff: {
...this.raw['xliff'],
file: files,
},
},
}),
...this._toFakeData(files),
}
})
}

_fakeRawByFiles(file: ICrowdinXLIFFXmlFile[] | ICrowdinXLIFFXmlFile)
{
return {
...this.raw,
xliff: {
...this.raw['xliff'],
file,
},
}
}

_toFakeData(file: ICrowdinXLIFFXmlFile[] | ICrowdinXLIFFXmlFile)
{
const id = Array.isArray(file) ? file.flat().map(file => file['@id']) : [file['@id']];

const raw = this._fakeRawByFiles(file);

return {
id,
raw,
xml: this.toString.call({
_headless: this._headless,
raw,
}),
}
}

}

0 comments on commit 8672d90

Please sign in to comment.