parseBlockArchive and tests
This commit is contained in:
parent
19d6dc1ddf
commit
655635551d
2 changed files with 61 additions and 0 deletions
24
webapp/src/blocks/archive.test.ts
Normal file
24
webapp/src/blocks/archive.test.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
import {TestBlockFactory} from '../test/testBlockFactory'
|
||||
|
||||
import {ArchiveUtils} from './archive'
|
||||
import {IBlock} from './block'
|
||||
|
||||
test('archive: archive and unarchive', async () => {
|
||||
const blocks: IBlock[] = []
|
||||
|
||||
const board = TestBlockFactory.createBoard()
|
||||
blocks.push(board)
|
||||
blocks.push(TestBlockFactory.createBoardView(board))
|
||||
const card = TestBlockFactory.createCard(board)
|
||||
blocks.push(card)
|
||||
blocks.push(TestBlockFactory.createText(card))
|
||||
blocks.push(TestBlockFactory.createDivider(card))
|
||||
blocks.push(TestBlockFactory.createImage(card))
|
||||
|
||||
const archive = ArchiveUtils.buildBlockArchive(blocks)
|
||||
const unarchivedBlocks = ArchiveUtils.parseBlockArchive(archive)
|
||||
|
||||
expect(unarchivedBlocks).toEqual(blocks)
|
||||
})
|
|
@ -39,6 +39,43 @@ class ArchiveUtils {
|
|||
|
||||
return content
|
||||
}
|
||||
|
||||
static parseBlockArchive(contents: string): IBlock[] {
|
||||
const blocks: IBlock[] = []
|
||||
const allLineStrings = contents.split('\n')
|
||||
if (allLineStrings.length >= 2) {
|
||||
const headerString = allLineStrings[0]
|
||||
const header = JSON.parse(headerString) as IArchiveHeader
|
||||
if (header.date && header.version >= 1) {
|
||||
const lineStrings = allLineStrings.slice(1)
|
||||
let lineNum = 2
|
||||
for (const lineString of lineStrings) {
|
||||
if (!lineString) {
|
||||
// Ignore empty lines, e.g. last line
|
||||
continue
|
||||
}
|
||||
const line = JSON.parse(lineString) as IArchiveLine
|
||||
if (!line || !line.type || !line.data) {
|
||||
throw new Error(`ERROR parsing line ${lineNum}`)
|
||||
}
|
||||
switch (line.type) {
|
||||
case 'block': {
|
||||
const blockLine = line as IBlockArchiveLine
|
||||
const block = blockLine.data
|
||||
blocks.push(block)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
lineNum += 1
|
||||
}
|
||||
} else {
|
||||
throw new Error('ERROR parsing header')
|
||||
}
|
||||
}
|
||||
|
||||
return blocks
|
||||
}
|
||||
}
|
||||
|
||||
export {IArchiveHeader, IArchiveLine, IBlockArchiveLine, ArchiveUtils}
|
||||
|
|
Loading…
Reference in a new issue