Completed
Push — master ( d83c14...87c801 )
by Jan
16s queued 16s
created

src/api/create.ts   A

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 32
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 4
mnd 1
bc 1
fnc 3
bpm 0.3333
cpm 1.3333
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A create.ts ➔ create 0 5 2
1
import * as ID3Util from "../ID3Util"
2
import { WriteTags } from "../types/Tags"
3
import { isFunction } from  "../util"
4
import { createBufferFromTags } from "../TagsHelpers"
5
6
export type CreateCallback =
7
    (data: Buffer) => void
8
9
/**
10
 * Creates a buffer containing the ID3 Tag
11
 */
12
export function create(tags: WriteTags): Buffer
13
export function create(tags: WriteTags, callback: CreateCallback): void
14
export function create(tags: WriteTags, callback?: CreateCallback) {
15
    const frames = createBufferFromTags(tags)
16
17
    //  Create ID3 header
18
    const header = Buffer.alloc(10)
19
    header.fill(0)
20
    header.write("ID3", 0)              //File identifier
21
    header.writeUInt16BE(0x0300, 3)     //Version 2.3.0  --  03 00
22
    header.writeUInt16BE(0x0000, 5)     //Flags 00
23
    ID3Util.encodeSize(frames.length).copy(header, 6)
24
25
    const id3Data = Buffer.concat([header, frames])
26
27
    if (isFunction(callback)) {
28
        return callback(id3Data)
29
    }
30
    return id3Data
31
}
32