|
1
|
|
|
import { Tags, TagAliases, RawTags, WriteTags } from "./Tags" |
|
2
|
|
|
import { |
|
3
|
|
|
FrameOptions, |
|
4
|
|
|
FRAME_IDENTIFIERS, |
|
5
|
|
|
FRAME_ALIASES |
|
6
|
|
|
} from "./ID3Definitions" |
|
7
|
|
|
import * as ID3Util from "./ID3Util" |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
// TODO: Convertion to typescript need to be cleaned up, it has been quickly |
|
11
|
|
|
// hacked for now. |
|
12
|
|
|
export function updateTags(newTags: WriteTags, currentTags: Tags): RawTags { |
|
13
|
|
|
|
|
14
|
|
|
const newRawTags = makeRawTags(newTags) |
|
15
|
|
|
|
|
16
|
|
|
// eslint-disable-next-line |
|
17
|
|
|
const read = <T extends {}> |
|
18
|
|
|
(object: T, index: string) => object[index as keyof T] |
|
19
|
|
|
|
|
20
|
|
|
// eslint-disable-next-line |
|
21
|
|
|
const currentRawTags: Record<string, any> = currentTags.raw ?? {} |
|
22
|
|
|
Object.entries(newRawTags).map(([frameIdentifierString, frame]) => { |
|
23
|
|
|
const frameIdentifier = frameIdentifierString as keyof RawTags |
|
24
|
|
|
const options: FrameOptions = ID3Util.getSpecOptions(frameIdentifier) |
|
25
|
|
|
const newTag = newRawTags[frameIdentifier] |
|
26
|
|
|
const currentTag = currentRawTags[frameIdentifier] |
|
27
|
|
|
if (options.multiple && newTag && currentTag) { |
|
28
|
|
|
const cCompare: Record<string, number> = {} |
|
29
|
|
|
if (options.updateCompareKey) { |
|
30
|
|
|
(currentTag as []).forEach((cTag, index) => { |
|
31
|
|
|
// eslint-disable-next-line |
|
32
|
|
|
cCompare[cTag[options.updateCompareKey!]] = index |
|
33
|
|
|
}) |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
const newTagArray = newTag instanceof Array ? newTag : [newTag] |
|
37
|
|
|
newTagArray.forEach((tagValue) => { |
|
38
|
|
|
const tagProperty = |
|
39
|
|
|
// eslint-disable-next-line |
|
40
|
|
|
read(tagValue, options.updateCompareKey!) as string |
|
41
|
|
|
if (tagProperty in cCompare) { |
|
42
|
|
|
const comparison = cCompare[tagProperty] |
|
43
|
|
|
currentRawTags[frameIdentifier][comparison] = tagValue |
|
44
|
|
|
} else { |
|
45
|
|
|
currentRawTags[frameIdentifier].push(tagValue) |
|
46
|
|
|
} |
|
47
|
|
|
}) |
|
48
|
|
|
} else { |
|
49
|
|
|
currentRawTags[frameIdentifier] = frame |
|
50
|
|
|
} |
|
51
|
|
|
}) |
|
52
|
|
|
return currentRawTags |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
function makeRawTags(tags: WriteTags): RawTags { |
|
56
|
|
|
return Object.entries(tags).reduce<RawTags>((rawTags, [key, value]) => { |
|
57
|
|
|
const identifiers = FRAME_IDENTIFIERS.v34 |
|
58
|
|
|
const aliases = FRAME_ALIASES.v34 |
|
59
|
|
|
if (key in identifiers) { |
|
60
|
|
|
rawTags[identifiers[key as keyof TagAliases]] = value |
|
61
|
|
|
} |
|
62
|
|
|
if (key in aliases) { |
|
63
|
|
|
rawTags[key as keyof RawTags] = value |
|
64
|
|
|
} |
|
65
|
|
|
return rawTags |
|
66
|
|
|
}, {}) |
|
67
|
|
|
} |
|
68
|
|
|
|