Skip to content

Commit

Permalink
reduce the size of the compiled output
Browse files Browse the repository at this point in the history
  • Loading branch information
jkalis-rm committed Feb 19, 2020
1 parent 6030c87 commit 3986993
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/index.mjs
Expand Up @@ -10,13 +10,13 @@ export default function(url, options) {
request.open(options.method || 'get', url, true);

request.onload = () => {
const head = { all: [], keys: [], raw: {} };
const all = [], keys = [], raw = {};
request.getAllResponseHeaders().replace(regex, (m, key, value) => {
head.all.push([key, value]);
head.keys.push(key = key.toLowerCase());
head.raw[key] = head.raw[key] ? `${head.raw[key]},${value}` : value;
all.push([key, value]);
keys.push(key = key.toLowerCase());
raw[key] = raw[key] ? `${raw[key]},${value}` : value;
});
resolve(response(request, head));
resolve(response(request, all, keys, raw));
};

request.onerror = reject;
Expand Down
12 changes: 6 additions & 6 deletions src/lib/response.mjs
@@ -1,4 +1,4 @@
export default function response (request, headers) {
export default function response (request, all, keys, raw) {
return {
ok: (request.status/100|0) == 2, // 200-299
statusText: request.statusText,
Expand All @@ -7,12 +7,12 @@ export default function response (request, headers) {
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(request.responseText).then(JSON.parse),
blob: () => Promise.resolve(new Blob([request.response])),
clone: () => response(request, headers),
clone: () => response(request, all, keys, raw),
headers: {
keys: () => headers.keys,
entries: () => headers.all,
get: n => headers.raw[n.toLowerCase()],
has: n => n.toLowerCase() in headers.raw
keys: () => keys,
entries: () => all,
get: n => raw[n.toLowerCase()],
has: n => n.toLowerCase() in raw
}
};
}
20 changes: 11 additions & 9 deletions test/lib/response.js
@@ -1,7 +1,12 @@
import response from '../../src/lib/response.mjs';

describe('response()', () => {
it('returns text()', () => response({ responseText: 'A passing test.' })
const raw = { 'x-foo': 'foo', 'x-bar': 'bar' };
const keys = Object.keys(raw);
const all = Object.entries(raw);
const resp = response({}, all, keys, raw);

it('returns text()', () => response({ responseText: 'A passing test.' }, [], [], {})
.text()
.then((text) => expect(text).toBe('A passing test.'))
);
Expand All @@ -12,19 +17,16 @@ describe('response()', () => {
);

it('returns headers', () => {
const all = [['x-foo', 'bar'], ['x-baz', 'boo']];
const result = response({}, { all }).headers.entries();
expect(result).toEqual(all);
expect(resp.headers.entries()).toEqual(all);
});

it('returns header keys', () => {
const result = response({}, { keys: ['x-foo'] }).headers.keys();
expect(result).toEqual(['x-foo']);
expect(resp.headers.keys()).toEqual(['x-foo', 'x-bar']);
});

it('returns headers has', () => {
const raw = { 'x-foo': 'bar', 'x-baz': 'boo' };
const test = response({}, { raw }).headers;
expect(test.has('x-foo')).toBe(true);
expect(resp.headers.has('x-foo')).toBe(true);
expect(resp.headers.has('x-bar')).toBe(true);
expect(resp.headers.has('x-baz')).toBe(false);
});
});

0 comments on commit 3986993

Please sign in to comment.