Passed
Pull Request — filestream (#173)
by
unknown
05:02
created

src/api/write.ts   A

Complexity

Total Complexity 19
Complexity/F 2.11

Size

Lines of Code 135
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 73
mnd 10
bc 10
fnc 9
dl 0
loc 135
rs 10
bpm 1.1111
cpm 2.1111
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A write.ts ➔ writeToFileSync 0 14 1
B write.ts ➔ write 0 8 7
A write.ts ➔ writeInBuffer 0 12 1
A write.ts ➔ writeToFile 0 11 5
1
import { WriteTags } from "../types/Tags"
2
import { WriteCallback, WriteOptions } from "../types/write"
3
import { create }  from "./create"
4
import { removeTagsFromBuffer } from "./remove"
5
import { isFunction, isString, validateString } from "../util"
6
import { writeId3TagToFileAsync, writeId3TagToFileSync } from "../file-write"
7
8
/**
9
 * Replaces any existing tags with the given tags in the given buffer.
10
 * Throws in case of error.
11
 * @deprecated Use `writeInBuffer` instead.
12
 * @public
13
 */
14
export function write(tags: WriteTags, buffer: Buffer): Buffer
15
16
/**
17
 * Replaces synchronously any existing tags with the given tags in the
18
 * specified file.
19
 * Throws in case of error.
20
 * @deprecated Use `writeInFileSync` instead.
21
 * @public
22
 */
23
export function write(
24
    tags: WriteTags,
25
    filepath: string,
26
    options?: WriteOptions
27
): void
28
29
/**
30
 * Replaces asynchronously any existing tags with the given tags in the
31
 * specified file.
32
 * @deprecated Use `writeInFile` instead.
33
 * @public
34
 */
35
export function write(
36
    tags: WriteTags,
37
    filepath: string,
38
    callback: WriteCallback
39
): void
40
41
export function write(
42
    tags: WriteTags,
43
    filebuffer: string | Buffer,
44
    optionsOrCallback?: WriteOptions | WriteCallback,
45
    maybeCallback?: WriteCallback
46
): Buffer | void {
47
    const options =
48
        (isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {}
49
    const callback =
50
        isFunction(optionsOrCallback) ? optionsOrCallback : maybeCallback
51
52
    if (isFunction(callback)) {
53
        writeToFile(tags, validateString(filebuffer), options, callback)
54
        return
55
    }
56
    if (isString(filebuffer)) {
57
        return writeToFileSync(tags, filebuffer, options)
58
    }
59
    return writeInBuffer(tags, filebuffer)
60
}
61
62
// New API
63
64
/**
65
 * Replaces any existing tags with the given tags in the given buffer.
66
 * Throws in case of error.
67
 * @public
68
 */
69
export function writeInBuffer(tags: WriteTags, buffer: Buffer): Buffer {
70
    const id3Tag = create(tags)
71
    const bufferWithoutId3Tag = removeTagsFromBuffer(buffer) || buffer
72
    return Buffer.concat([id3Tag, bufferWithoutId3Tag])
73
}
74
75
/**
76
 * Replaces synchronously any existing tags with the given tags in the
77
 * specified file.
78
 * Throws in case of error.
79
 * @public
80
 */
81
export function writeToFileSync(
82
    tags: WriteTags,
83
    filepath: string,
84
    options: WriteOptions = {}
85
): void {
86
    const id3Tag = create(tags)
87
    writeId3TagToFileSync(filepath, id3Tag, options)
88
}
89
90
/**
91
 * Replaces asynchronously any existing tags with the given tags in the
92
 * specified file.
93
 * @public
94
 */
95
export function writeToFile(
96
    tags: WriteTags,
97
    filepath: string,
98
    callback: WriteCallback
99
): void
100
101
/**
102
 * Replaces asynchronously any existing tags with the given tags in the
103
 * specified file.
104
 * @public
105
 */
106
export function writeToFile(
107
    tags: WriteTags,
108
    filepath: string,
109
    options: WriteOptions,
110
    callback: WriteCallback
111
): void
112
113
/**
114
 * Replaces asynchronously any existing tags with the given tags in the
115
 * specified file.
116
 * @public
117
 */
118
export function writeToFile(
119
    tags: WriteTags,
120
    filepath: string,
121
    optionsOrCallback: WriteOptions | WriteCallback,
122
    maybeCallback: WriteCallback = () => { /* */ }
123
): void {
124
    const options =
125
        (isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {}
126
    const callback =
127
        isFunction(optionsOrCallback) ? optionsOrCallback : maybeCallback
128
129
    const id3Tag = create(tags)
130
    writeId3TagToFileAsync(filepath, id3Tag, options).then(
131
        () => callback(null),
132
        (error) => callback(error)
133
    )
134
}
135