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

benrr101 / node-taglib-sharp / 48257345

12 Oct 2023 04:06AM UTC coverage: 92.315% (+0.4%) from 91.944%
48257345

push

appveyor

benrr101
Performer roles (bug fixed), genres

3226 of 4153 branches covered (0.0%)

Branch coverage included in aggregate %.

94 of 94 new or added lines in 2 files covered. (100.0%)

27069 of 28664 relevant lines covered (94.44%)

417.21 hits per line

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

59.7
/src/mpeg4/boxes/mpeg4Box.ts
1
import Mpeg4BoxHeader from "../mpeg4BoxHeader";
1✔
2
import {ByteVector} from "../../byteVector";
1✔
3
import {File} from "../../file";
4
import {Mpeg4BoxClassType} from "../mpeg4BoxClassType";
5
import {ArrayUtils, Guards} from "../../utils";
1✔
6

7
/**
8
 * This class provides a generic implementation of a ISO/IEC 14496-12 box.
9
 */
10
export default abstract class Mpeg4Box {
1✔
11
    private _data: ByteVector;
12
    private _header: Mpeg4BoxHeader;
13
    private _baseDataPosition: number;
14
    private _dataPosition: number;
15

16
    /**
17
     * Gets the position of the data contained in the current instance, after any box specific headers.
18
     */
19
    public get dataPosition(): number {
20
        return this._dataPosition;
×
21
    }
22

23
    /**
24
     * The type of the handler box that applies to the current instance.
25
     */
26
    private _handlerType: ByteVector;
27

28
    /**
29
     * The children of the current instance.
30
     */
31
    private _children: Mpeg4Box[];
32

33
    /**
34
     * Gets a flag indicating which type of box the current instance is.
35
     */
36
    public abstract get boxClassType(): Mpeg4BoxClassType;
37

38
    /**
39
     * Protected constructor to force construction via static functions.
40
     */
41
    protected constructor() {
42
        this._children = [];
1,161✔
43
    }
44

45
    /**
46
     * Initializes a new instance of @see Mpeg4Box with a specified header and handler.
47
     * @param header A @see Mpeg4BoxHeader object describing the new instance.
48
     * @param handlerType Type of the handler box object containing the handler that applies to the
49
     * new instance, or undefined if no handler applies.
50
     */
51
    protected initializeFromHeaderAndHandler(header: Mpeg4BoxHeader, handlerType: ByteVector): void {
52
        this._header = header;
1,161✔
53
        this._baseDataPosition = header.position + header.headerSize;
1,161✔
54
        this._dataPosition = this._baseDataPosition;
1,161✔
55
        this._handlerType = handlerType;
1,161✔
56
    }
57

58
    /**
59
     * Initializes a new instance of @see Mpeg4Box with a specified header.
60
     * @param header A @see Mpeg4BoxHeader object describing the new instance.
61
     */
62
    protected initializeFromHeader(header: Mpeg4BoxHeader): void {
63
        return this.initializeFromHeaderAndHandler(header, undefined);
1,161✔
64
    }
65

66
    /**
67
     * Initializes a new instance of @see Mpeg4Box with a specified box type.
68
     * @param type A @see ByteVector object containing the box type to use for the new instance.
69
     */
70
    protected initializeFromType(type: ByteVector): void {
71
        return this.initializeFromHeader(Mpeg4BoxHeader.fromType(type));
494✔
72
    }
73

74
    // #region Properties
75

76
    /**
77
     * Gets the MPEG-4 box type of the current instance.
78
     */
79
    public get boxType(): ByteVector { return this._header.boxType; }
3,636✔
80

81
    /**
82
     * Gets the child boxes of the current instance.
83
     * @internal
84
     */
85
    public get children(): Mpeg4Box[] { return this._children.slice(0); }
5✔
86

87
    /**
88
     * Gets the data contained in the current instance.
89
     */
90
    public get data(): ByteVector { return this._data; }
1,322✔
91

92
    /**
93
     * Sets the data contained in the current instance.
94
     */
95
    public set data(v: ByteVector) { this._data = v; }
494✔
96
    /**
97
     * Gets the size of the data contained in the current instance, minus the size of any box specific headers.
98
     */
99
    public get dataSize(): number { return this._header.dataSize + this._baseDataPosition - this.dataPosition; }
×
100

101
    /**
102
     * Gets the type of the handler box that applies to the current instance.
103
     */
104
    public get handlerType(): ByteVector { return this._handlerType; }
×
105

106
    /**
107
     * Gets whether or not the current instance has children.
108
     */
109
    public get hasChildren(): boolean { return this._children.length > 0; }
89✔
110

111
    /**
112
     * Gets the header of the current instance.
113
     */
114
    public get header(): Mpeg4BoxHeader { return this._header; }
×
115

116
    /**
117
     * Gets the total size of the current instance as it last appeared on disk.
118
     */
119
    public get size(): number { return this._header.totalBoxSize; }
×
120

121
    // #endregion
122

123
    // #region Public Methods
124

125
    /**
126
     * Adds a specified box to the current instance.
127
     * @param box A @see Mpeg4Box object to add to the current instance.
128
     */
129
    public addChild(box: Mpeg4Box): void {
130
        this._children.push(box);
1,162✔
131
    }
132

133
    /**
134
     * Removes all children from the current instance.
135
     */
136
    public clearChildren(): void {
137
        this._children.splice(0, this._children.length);
1✔
138
    }
139

140
    /**
141
     * Gets a child box from the current instance by finding a matching box type.
142
     * @param type  A @see ByteVector object containing the box type to match.
143
     * @returns  A @see Mpeg4Box object containing the matched box, or undefined if no matching box was found.
144
     */
145
    public getChild<TBox extends Mpeg4Box>(type: ByteVector, predicate?: (b: TBox) => boolean): TBox {
146
        return <TBox>this._children.find((b) => {
1,260✔
147
            return ByteVector.equals(b.boxType, type) && (!predicate || predicate(<TBox>b));
1,875!
148
        });
149
    }
150

151
    /**
152
     * Gets a child box from the current instance by finding a matching box type, searching recursively.
153
     * @param type  A @see ByteVector object containing the box type to match.
154
     * @returns A @see Mpeg4Box object containing the matched box, or undefined if no matching box was found.
155
     */
156
    public getChildRecursively(type: ByteVector): Mpeg4Box {
157
        // Check local children for a match
158
        const localMatch = this._children.find(b => ByteVector.equals(b.boxType, type));
×
159
        if (localMatch) {
×
160
            return localMatch;
×
161
        }
162

163
        // Recurse
164
        for (const box of this._children) {
×
165
            const childBox = box.getChildRecursively(type);
×
166

167
            if (childBox) {
×
168
                return childBox;
×
169
            }
170
        }
171

172
        return undefined;
×
173
    }
174

175
    /**
176
     * Gets all child boxes from the current instance by finding a matching box type.
177
     * @param type A @see ByteVector object containing the box type to match.
178
     * @returns A @see Mpeg4Box[] object containing the matched box, or undefined if no matching boxes was found.
179
     */
180
    public getChildren<TBox extends Mpeg4Box>(type: ByteVector, predicate?: (b: TBox) => boolean): TBox[] {
181
        return <TBox[]>this._children.filter((b) => {
1,086✔
182
            return ByteVector.equals(b.boxType, type) && (!predicate || predicate(<TBox>b));
1,276✔
183
        });
184
    }
185

186
    /**
187
     * Removes a specified box from the current instance.
188
     * @param box A @see Mpeg4Box object to remove from the current instance.
189
     */
190
    public removeChildByBox(box: Mpeg4Box): void {
191
        const index = this._children.indexOf(box);
×
192

193
        if (index > -1) {
×
194
            this._children.splice(index, 1);
×
195
        }
196
    }
197

198
    /**
199
     * Removes all children with a specified box type from the current instance.
200
     * @param type A @see ByteVector object containing the box type to remove.
201
     */
202
    public removeChildByType(type: ByteVector): void {
203
        for (let i = this._children.length - 1; i >= 0; i--) {
182✔
204
            if (ByteVector.equals(this._children[i].boxType, type)) {
295✔
205
                this._children.splice(i, 1);
143✔
206
            }
207
        }
208
    }
209

210
    public removeChildrenByBox(boxes: Mpeg4Box[]): void {
211
        if (ArrayUtils.isFalsyOrEmpty(boxes)) {
87✔
212
            // Nothing to do.
213
            return;
50✔
214
        }
215

216
        ArrayUtils.remove(this._children, e => boxes.includes(e));
115✔
217
    }
218

219
    // #endregion
220

221
    /**
222
     * Loads the data of the current instance from a specified file using the internal data position and size.
223
     * @param file The @see File from which the current instance was read and from which to read the data.
224
     * @returns A @see ByteVector object containing the data read from the file.
225
     */
226
    public loadData(file: File): ByteVector {
227
        Guards.notNullOrUndefined(file, "file");
×
228

229
        file.seek(this.dataPosition);
×
230

231
        return file.readBlock(this.dataSize);
×
232
    }
233

234
    /**
235
     * Increases the data position by a given value. This function can be used by boxes
236
     * which extend from @see Mpeg4Box to increase the data position, because the data
237
     * is located after their box specific headers.
238
     * @param value The value to add to the data position.
239
     * @returns The value of the data position before the increase.
240
     */
241
    public increaseDataPosition(value: number): number {
242
        const dataPositionBeforeIncrease = this._dataPosition;
920✔
243

244
        this._dataPosition += value;
920✔
245

246
        return dataPositionBeforeIncrease;
920✔
247
    }
248

249
    /**
250
     * Generates the headers that are specific to the box type for use in rendering.
251
     * @internal
252
     */
253
    public renderBoxHeaders(): ByteVector[] {
254
        return undefined;
×
255
    }
256
}
257

258

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