• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

benrr101 / node-taglib-sharp / 46462135

pending completion
46462135

push

appveyor

Benjamin Russell
Merge branch 'release/v5.1.0'

3096 of 3788 branches covered (81.73%)

Branch coverage included in aggregate %.

2171 of 2171 new or added lines in 47 files covered. (100.0%)

25320 of 26463 relevant lines covered (95.68%)

437.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.53
/src/id3v2/frames/termsOfUseFrame.ts
1
import Id3v2Settings from "../id3v2Settings";
1✔
2
import {ByteVector, StringType} from "../../byteVector";
1✔
3
import {CorruptFileError} from "../../errors";
1✔
4
import {Frame, FrameClassType} from "./frame";
1✔
5
import {Id3v2FrameHeader} from "./frameHeader";
1✔
6
import {FrameIdentifiers} from "../frameIdentifiers";
1✔
7
import {Guards} from "../../utils";
1✔
8

9
export default class TermsOfUseFrame extends Frame {
1✔
10
    private _language: string;
11
    private _text: string;
12
    private _textEncoding: StringType = Id3v2Settings.defaultEncoding;
22✔
13

14
    // #region Constructors
15

16
    private constructor(header: Id3v2FrameHeader) {
17
        super(header);
22✔
18
    }
19

20
    /**
21
     * Constructs and initializes a new instance with a specified language.
22
     * @param language ISO-639-2 language code for the new frame
23
     * @param textEncoding Optional, text encoding to use when rendering the new frame. If not
24
     *     provided defaults to {@link Id3v2Settings.defaultEncoding}
25
     */
26
    public static fromFields(
27
        language: string,
28
        textEncoding: StringType = Id3v2Settings.defaultEncoding
12✔
29
    ): TermsOfUseFrame {
30
        const f = new TermsOfUseFrame(new Id3v2FrameHeader(FrameIdentifiers.USER));
16✔
31
        f.textEncoding = textEncoding;
16✔
32
        f._language = language;
16✔
33
        return f;
16✔
34
    }
35

36
    /**
37
     * Constructs and initializes a new instance by reading its raw data in a specified ID3v2
38
     * version. This method allows for offset reading from the data byte vector.
39
     * @param data Raw representation of the new frame
40
     * @param offset What offset in `data` the frame actually begins. Must be positive,
41
     *     safe integer
42
     * @param header Header of the frame found at `data` in the data
43
     * @param version ID3v2 version the frame was originally encoded with
44
     */
45
    public static fromOffsetRawData(
46
        data: ByteVector,
47
        offset: number,
48
        header: Id3v2FrameHeader,
49
        version: number
50
    ): TermsOfUseFrame {
51
        Guards.truthy(data, "data");
12✔
52
        Guards.uint(offset, "offset");
10✔
53
        Guards.truthy(header, "header");
5✔
54
        Guards.byte(version, "version");
3✔
55

56
        const frame = new TermsOfUseFrame(header);
3✔
57
        frame.setData(data, offset, false, version);
3✔
58
        return frame;
2✔
59
    }
60

61
    /**
62
     * Constructs and initializes a new instance by reading its raw data in a specified
63
     * ID3v2 version.
64
     * @param data Raw representation of the new frame
65
     * @param version ID3v2 version the raw frame is encoded with, must be a positive 8-bit integer
66
     */
67
    public static fromRawData(data: ByteVector, version: number): TermsOfUseFrame {
68
        Guards.truthy(data, "data");
8✔
69
        Guards.byte(version, "version");
6✔
70

71
        const frame = new TermsOfUseFrame(Id3v2FrameHeader.fromData(data, version));
3✔
72
        frame.setData(data, 0, true, version);
3✔
73
        return frame;
2✔
74
    }
75

76
    // #endregion
77

78
    // #region Properties
79

80
    /** @inheritDoc */
81
    public get frameClassType(): FrameClassType { return FrameClassType.TermsOfUseFrame; }
7✔
82

83
    /**
84
     * Gets the ISO-639-2 language code stored in the current instance.
85
     */
86
    public get language(): string {
87
        return this._language && this._language.length > 2
19✔
88
            ? this._language.substring(0, 3)
19✔
89
            : "XXX";
90
    }
91
    /**
92
     * Sets the ISO-639-2 language code stored in the current instance.
93
     * There should only be one frame with a matching ISO-639-2 language code per tag.
94
     */
95
    public set language(value: string) { this._language = value; }
5✔
96

97
    /**
98
     * Gets the text of the terms of use
99
     */
100
    public get text(): string { return this._text || ""; }
12✔
101
    /**
102
     * Sets the text of the terms of use
103
     */
104
    public set text(value: string) { this._text = value; }
9✔
105

106
    /**
107
     * Gets the text encoding to use when storing the current instance.
108
     */
109
    public get textEncoding(): StringType { return this._textEncoding; }
14✔
110
    /**
111
     * Sets the text encoding to use when storing the current instance.
112
     * This encoding is overridden when rendering if {@link Id3v2Settings.forceDefaultEncoding} is
113
     * `true` or the render version does not support it.
114
     * @param value Text encoding to use when storing the current instance
115
     */
116
    public set textEncoding(value: StringType) { this._textEncoding = value; }
21✔
117

118
    // #endregion
119

120
    // #region Public Methods
121

122
    /**
123
     * Gets a specified terms of use frame from the list of frames
124
     * @param frames List of frames to search
125
     * @param language Optionally, the ISO-639-2 language code to match
126
     * @returns A matching frame if found or `undefined` if a matching frame was not found
127
     */
128
    public static find(frames: TermsOfUseFrame[], language?: string): TermsOfUseFrame {
129
        Guards.truthy(frames, "frames");
4✔
130
        return frames.find((f) => !language || f.language === language);
3✔
131
    }
132

133
    /**
134
     * Gets a specified terms of use frame from the list of frames, trying to match the language but
135
     * accepting one with a different language if a match was not found.
136
     * @param frames List of frames to search
137
     * @param language ISO-639-2 language code to match
138
     * @returns Frame containing the matching frame or `undefined` if a match was not found
139
     */
140
    public static findPreferred(frames: TermsOfUseFrame[], language: string): TermsOfUseFrame {
141
        Guards.truthy(frames, "frames");
4✔
142

143
        let bestFrame: TermsOfUseFrame;
144
        for (const f of frames) {
3✔
145
            if (f.language === language) {
4✔
146
                return f;
1✔
147
            }
148
            if (!bestFrame) {
3✔
149
                bestFrame = f;
2✔
150
            }
151
        }
152

153
        return bestFrame;
2✔
154
    }
155

156
    /** @inheritDoc */
157
    public clone(): Frame {
158
        const frame = TermsOfUseFrame.fromFields(this._language, this.textEncoding);
1✔
159
        frame.text = this.text;
1✔
160
        return frame;
1✔
161
    }
162

163
    /**
164
     * Returns a string representation of the frame.
165
     */
166
    public toString(): string { return this._text; }
×
167

168
    // #endregion
169

170
    // #region Protected Methods
171

172
    /** @inheritDoc */
173
    protected parseFields(data: ByteVector): void {
174
        if (data.length < 4) {
6✔
175
            throw new CorruptFileError("Not enough bytes in field");
2✔
176
        }
177

178
        this.textEncoding = data.get(0);
4✔
179
        this._language = data.subarray(1, 3).toString(StringType.Latin1);
4✔
180
        this.text = data.subarray(4).toString(this.textEncoding);
4✔
181
    }
182

183
    /** @inheritDoc */
184
    protected renderFields(version: number): ByteVector {
185
        const encoding = Frame.correctEncoding(this.textEncoding, version);
2✔
186

187
        return ByteVector.concatenate(
2✔
188
            encoding,
189
            ByteVector.fromString(this.language, StringType.Latin1),
190
            ByteVector.fromString(this.text, encoding)
191
        );
192
    }
193

194
    // #endregion
195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc