Passed
Pull Request — master (#136)
by
unknown
01:56
created

src/FrameBuilder.ts   A

Complexity

Total Complexity 14
Complexity/F 1.75

Size

Lines of Code 86
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 64
mnd 6
bc 6
fnc 8
dl 0
loc 86
rs 10
bpm 0.75
cpm 1.75
noi 0
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A FrameBuilder.getBuffer 0 6 1
A FrameBuilder.appendNullTerminatedValue 0 7 1
A FrameBuilder.appendBuffer 0 3 1
A FrameBuilder.appendNumber 0 12 3
A FrameBuilder.appendValue 0 9 1
A FrameBuilder.ts ➔ getTerminatingMarker 0 8 2
A FrameBuilder.ts ➔ staticValueToBuffer 0 6 2
A FrameBuilder.ts ➔ convertValue 0 12 3
1
import * as ID3Util from "./ID3Util"
2
import { isString } from "./util"
3
import { TextEncoding } from "./definitions/Encoding"
4
5
type Value = Buffer | number | string
6
7
export class FrameBuilder {
8
    private identifier: string
9
    private buffer = Buffer.alloc(0)
10
11
    constructor(identifier: string) {
12
        this.identifier = identifier
13
    }
14
15
    appendValue(
16
        value: Value,
17
        size?: number | null,
18
        encoding: TextEncoding = TextEncoding.ISO_8859_1
19
    ) {
20
        const convertedValue = convertValue(value, encoding)
21
        this.appendBuffer(staticValueToBuffer(convertedValue, size))
22
        return this
23
    }
24
25
    appendNumber(value: number, size: number) {
26
        if (Number.isInteger(value)) {
27
            let hexValue = value.toString(16)
28
            if (hexValue.length % 2 !== 0) {
29
                hexValue = "0" + hexValue
30
            }
31
            this.appendBuffer(
32
                staticValueToBuffer(Buffer.from(hexValue, 'hex'), size)
33
            )
34
        }
35
        return this
36
    }
37
38
    appendNullTerminatedValue(value = '', encoding: TextEncoding = TextEncoding.ISO_8859_1) {
39
        this.appendBuffer(
40
            convertValue(value, encoding),
41
            getTerminatingMarker(encoding)
42
        )
43
        return this
44
    }
45
46
    getBuffer() {
47
        const header = Buffer.alloc(10)
48
        header.write(this.identifier, 0)
49
        header.writeUInt32BE(this.buffer.length, 4)
50
        return Buffer.concat([header, this.buffer])
51
    }
52
53
    private appendBuffer(...buffers: Buffer[]) {
54
        this.buffer = Buffer.concat([this.buffer, ...buffers])
55
    }
56
}
57
58
function convertValue(
59
    value: Value,
60
    encoding: TextEncoding = TextEncoding.ISO_8859_1
61
) {
62
    if (value instanceof Buffer) {
63
        return value
64
    }
65
    if (Number.isInteger(value) || isString(value)) {
66
        return ID3Util.stringToEncodedBuffer(value.toString(), encoding)
67
    }
68
    return Buffer.alloc(0)
69
}
70
71
function staticValueToBuffer(buffer: Buffer, size?: number | null) {
72
    if (size && buffer.length < size) {
73
        return Buffer.concat([Buffer.alloc(size - buffer.length, 0x00), buffer])
74
    }
75
    return buffer
76
}
77
78
function getTerminatingMarker(encoding: TextEncoding) {
79
    if (encoding === TextEncoding.UTF_16_WITH_BOM ||
80
        encoding === TextEncoding.UTF_16_BE
81
    ) {
82
        return Buffer.alloc(2, 0x00)
83
    }
84
    return Buffer.alloc(1, 0x00)
85
}
86
87