-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
mime.ts
97 lines (90 loc) · 2.65 KB
/
mime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
/**
* An object which stores MIME data for general application use.
*
* #### Notes
* This class does not attempt to enforce "correctness" of MIME types
* and their associated data. Since this class is designed to transfer
* arbitrary data and objects within the same application, it assumes
* that the user provides correct and accurate data.
*/
export class MimeData {
/**
* Get an array of the MIME types contained within the dataset.
*
* @returns A new array of the MIME types, in order of insertion.
*/
types(): string[] {
return this._types.slice();
}
/**
* Test whether the dataset has an entry for the given type.
*
* @param mime - The MIME type of interest.
*
* @returns `true` if the dataset contains a value for the given
* MIME type, `false` otherwise.
*/
hasData(mime: string): boolean {
return this._types.indexOf(mime) !== -1;
}
/**
* Get the data value for the given MIME type.
*
* @param mime - The MIME type of interest.
*
* @returns The value for the given MIME type, or `undefined` if
* the dataset does not contain a value for the type.
*/
getData(mime: string): any | undefined {
let i = this._types.indexOf(mime);
return i !== -1 ? this._values[i] : undefined;
}
/**
* Set the data value for the given MIME type.
*
* @param mime - The MIME type of interest.
*
* @param data - The data value for the given MIME type.
*
* #### Notes
* This will overwrite any previous entry for the MIME type.
*/
setData(mime: string, data: unknown): void {
this.clearData(mime);
this._types.push(mime);
this._values.push(data);
}
/**
* Remove the data entry for the given MIME type.
*
* @param mime - The MIME type of interest.
*
* #### Notes
* This is a no-op if there is no entry for the given MIME type.
*/
clearData(mime: string): void {
let i = this._types.indexOf(mime);
if (i !== -1) {
this._types.splice(i, 1);
this._values.splice(i, 1);
}
}
/**
* Remove all data entries from the dataset.
*/
clear(): void {
this._types.length = 0;
this._values.length = 0;
}
private _types: string[] = [];
private _values: any[] = [];
}