Passed
Push — convert-to-typescript ( 6cf209 )
by Jan
03:57
created

src/update.ts   A

Complexity

Total Complexity 12
Complexity/F 3

Size

Lines of Code 75
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 61
mnd 8
bc 8
fnc 4
dl 0
loc 75
bpm 2
cpm 3
noi 0
c 0
b 0
f 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A update.ts ➔ replaceMatchingTags 0 16 2
A update.ts ➔ replaceMatchingTag 0 12 1
B update.ts ➔ updateTags 0 26 6
A update.ts ➔ makeRawTags 0 13 3
1
import { Tags, TagAliases, RawTags, WriteTags  } from "./Tags"
2
import { FrameOptions, FRAME_IDENTIFIERS, FRAME_ALIASES } from "./ID3Definitions"
3
const ID3Util = require('./ID3Util')
4
5
export function updateTags(newTags: WriteTags, currentTags: Tags): RawTags {
6
    const newRawTags = makeRawTags(newTags)
7
    const currentRawTags = currentTags.raw ?? {}
8
    let updatedRawTags = {... currentRawTags}
9
10
    Object.entries(newRawTags).forEach(([identifier, newValue]) => {
11
        const identifierKey = identifier as keyof RawTags
12
        const options: FrameOptions = ID3Util.getSpecOptions(identifier)
13
        const currentValue =
14
            currentRawTags[identifierKey] as RawTags[typeof identifierKey]
15
16
        if(!currentValue || !options.multiple || !options.updateCompareKey) {
17
            updatedRawTags[identifierKey] = newValue
18
            return
19
        }
20
21
        newValue = newValue instanceof Array ? newValue : [newValue]
22
        updatedRawTags[identifierKey] = replaceMatchingTags(
23
            currentValue instanceof Array ? currentValue : [currentValue],
24
            newValue,
25
            options.updateCompareKey
26
        )
27
    })
28
29
    return updatedRawTags
30
}
31
32
function replaceMatchingTags(
33
    oldFrames: Array<any>,
34
    newFrames: Array<any>,
35
    replaceKey: string): any
36
{
37
    for(let newFrame of newFrames) {
38
        oldFrames = replaceMatchingTag(
39
            oldFrames,
40
            replaceKey,
41
            newFrame[replaceKey],
42
            newFrame
43
        )
44
    }
45
46
    return oldFrames
47
}
48
49
function replaceMatchingTag(
50
    frames: Array<any>,
51
    replaceKey: string,
52
    searchString: string,
53
    replaceValue: any): any
54
{
55
    let newFrames = frames.filter(
56
        (frame) => !frame[replaceKey] || frame[replaceKey] !== searchString)
57
58
    newFrames.push(replaceValue)
59
    return newFrames
60
}
61
62
function makeRawTags(tags: WriteTags): RawTags {
63
    return Object.entries(tags).reduce<RawTags>((rawTags, [key, value]) => {
64
        const identifiers = FRAME_IDENTIFIERS.v34
65
        const aliases = FRAME_ALIASES.v34
66
        if (key in identifiers) {
67
            rawTags[identifiers[key as keyof TagAliases]] = value
68
        }
69
        if (key in aliases) {
70
            rawTags[key as keyof RawTags] = value
71
        }
72
        return rawTags
73
    }, {})
74
}
75