src/frames/frame-uslt.ts   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 37
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 27
mnd 4
bc 4
fnc 0
dl 0
loc 37
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { TextEncoding } from "../definitions/Encoding"
2
import { FrameBuilder } from "../FrameBuilder"
3
import { FrameReader } from "../FrameReader"
4
import { UnsynchronisedLyrics } from "../types/TagFrames"
5
import { isString } from '../util'
6
import { validateLanguageCode } from "./util"
7
8
export const USLT = {
9
    create: (data: UnsynchronisedLyrics | string): Buffer => {
10
        if(isString(data)) {
11
            // TODO: we should probably not accept a string only,
12
            // as the language is not optionalm default to eng for now.
13
            data = {
14
                language: 'eng',
15
                text: data
16
            }
17
        }
18
        if (data.text == undefined) {
19
            throw new TypeError("A description text must be provided")
20
        }
21
22
        return new FrameBuilder("USLT", TextEncoding.UTF_16_WITH_BOM)
23
            .appendText(validateLanguageCode(data.language))
24
            .appendTerminatedTextWithFrameEncoding(data.shortText ?? "")
25
            .appendTextWithFrameEncoding(data.text)
26
            .getBufferWithPartialHeader()
27
    },
28
    read: (buffer: Buffer): UnsynchronisedLyrics => {
29
        const reader = new FrameReader(buffer, {consumeEncodingByte: true})
30
        return {
31
            language: reader.consumeText({ size: 3}),
32
            shortText: reader.consumeTerminatedTextWithFrameEncoding(),
33
            text: reader.consumeTextWithFrameEncoding()
34
        }
35
    }
36
}
37