Skip to content

Commit b38939e

Browse files
committed
convert FastBootHeaders to es6 class
1 parent acbb306 commit b38939e

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

src/fastboot-headers.js

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,95 @@
22

33
// Partially implements Headers from the Fetch API
44
// https://developer.mozilla.org/en-US/docs/Web/API/Headers
5-
function FastBootHeaders(headers) {
6-
headers = headers || {};
7-
this.headers = {};
8-
9-
for (var header in headers) {
10-
let value = headers[header];
11-
12-
// Express gives us either a string
13-
// or an array of strings if there are multiple values.
14-
// We want to support the Header spec
15-
// so we will coerce to an array always.
16-
if (typeof value === 'string') {
17-
value = [value];
5+
class FastBootHeaders {
6+
constructor(headers) {
7+
headers = headers || {};
8+
this.headers = {};
9+
10+
for (var header in headers) {
11+
let value = headers[header];
12+
13+
// Express gives us either a string
14+
// or an array of strings if there are multiple values.
15+
// We want to support the Header spec
16+
// so we will coerce to an array always.
17+
if (typeof value === 'string') {
18+
value = [value];
19+
}
20+
21+
this.headers[header] = value;
1822
}
19-
20-
this.headers[header] = value;
2123
}
22-
}
2324

24-
FastBootHeaders.prototype.append = function(header, value) {
25-
header = header.toLowerCase();
26-
if (!this.has(header)) {
27-
this.headers[header] = [];
28-
}
25+
append(header, value) {
26+
let _header = header.toLowerCase();
2927

30-
this.headers[header].push(value);
31-
};
28+
if (!this.has(_header)) {
29+
this.headers[_header] = [];
30+
}
3231

33-
FastBootHeaders.prototype.delete = function(header) {
34-
delete this.headers[header.toLowerCase()];
35-
};
32+
this.headers[_header].push(value);
33+
}
3634

37-
FastBootHeaders.prototype.entries = function() {
38-
var entries = [];
35+
delete(header) {
36+
delete this.headers[header.toLowerCase()];
37+
}
38+
39+
entries() {
40+
var entries = [];
3941

40-
for(var key in this.headers) {
41-
var values = this.headers[key];
42-
for(var index = 0; index < values.length; ++index ) {
43-
entries.push([key, values[index]]);
42+
for(var key in this.headers) {
43+
var values = this.headers[key];
44+
for(var index = 0; index < values.length; ++index ) {
45+
entries.push([key, values[index]]);
46+
}
4447
}
45-
}
4648

47-
return entries[Symbol.iterator]();
48-
};
49+
return entries[Symbol.iterator]();
50+
}
4951

50-
FastBootHeaders.prototype.get = function(header) {
51-
return this.getAll(header)[0] || null;
52-
};
52+
get(header) {
53+
return this.getAll(header)[0] || null;
54+
}
5355

54-
FastBootHeaders.prototype.getAll = function(header) {
55-
return this.headers[header.toLowerCase()] || [];
56-
};
56+
getAll(header) {
57+
return this.headers[header.toLowerCase()] || [];
58+
}
5759

58-
FastBootHeaders.prototype.has = function(header) {
59-
return this.headers[header.toLowerCase()] !== undefined;
60-
};
60+
has(header) {
61+
return this.headers[header.toLowerCase()] !== undefined;
62+
}
6163

62-
FastBootHeaders.prototype.keys = function() {
63-
var entries = [];
64+
keys() {
65+
var entries = [];
6466

65-
for(var key in this.headers) {
66-
var values = this.headers[key];
67-
for(var index = 0; index < values.length; ++index ) {
68-
entries.push(key);
67+
for(var key in this.headers) {
68+
var values = this.headers[key];
69+
for(var index = 0; index < values.length; ++index ) {
70+
entries.push(key);
71+
}
6972
}
70-
}
7173

72-
return entries[Symbol.iterator]();
73-
};
74+
return entries[Symbol.iterator]();
75+
}
7476

75-
FastBootHeaders.prototype.set = function(header, value) {
76-
header = header.toLowerCase();
77-
this.headers[header] = [value];
78-
};
77+
set(header, value) {
78+
header = header.toLowerCase();
79+
this.headers[header] = [value];
80+
}
7981

80-
FastBootHeaders.prototype.values = function() {
81-
var entries = [];
82+
values() {
83+
var entries = [];
8284

83-
for(var key in this.headers) {
84-
var values = this.headers[key];
85-
for(var index = 0; index < values.length; ++index ) {
86-
entries.push(values[index]);
85+
for(var key in this.headers) {
86+
var values = this.headers[key];
87+
for(var index = 0; index < values.length; ++index ) {
88+
entries.push(values[index]);
89+
}
8790
}
88-
}
8991

90-
return entries[Symbol.iterator]();
91-
};
92+
return entries[Symbol.iterator]();
93+
}
94+
}
9295

9396
module.exports = FastBootHeaders;

0 commit comments

Comments
 (0)