@platform/util.mimetype
Advanced tools
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var test_1 = require("../test"); | ||
| var _1 = require("."); | ||
| describe('Mime', function () { | ||
| it('toType', function () { | ||
| var test = function (input, expected) { | ||
| var res = _1.Mime.toType(input); | ||
| (0, test_1.expect)(res).to.eql(expected); | ||
| }; | ||
| test('foo.yml', 'text/plain'); | ||
| test('foo.yaml', 'text/plain'); | ||
| test('foo.txt', 'text/plain'); | ||
| test('foo.css', 'text/css'); | ||
| test('foo.jpg', 'image/jpeg'); | ||
| test('foo.jpeg', 'image/jpeg'); | ||
| test('foo.gif', 'image/gif'); | ||
| test('foo.svg', 'image/svg+xml'); | ||
| test('foo.webm', 'video/webm'); | ||
| test(' foo.js ', 'application/javascript'); | ||
| test('foo.js', 'application/javascript'); | ||
| test('foo.json', 'application/json'); | ||
| }); | ||
| it('isBinary | isText | isJson', function () { | ||
| var test = function (input, isBinary, isText, isJson) { | ||
| (0, test_1.expect)(_1.Mime.isBinary(input)).to.eql(isBinary); | ||
| (0, test_1.expect)(_1.Mime.isText(input)).to.eql(isText); | ||
| (0, test_1.expect)(_1.Mime.isJson(input)).to.eql(isJson); | ||
| }; | ||
| test('image/jpeg', true, false, false); | ||
| test('image/png', true, false, false); | ||
| test('image/png; charset=utf-8', true, false, false); | ||
| test('video/webm', true, false, false); | ||
| test('application/octet-stream', true, false, false); | ||
| test('application/pdf', true, false, false); | ||
| test('application/javascript', true, false, false); | ||
| test('text/plain; charset=utf-8', false, true, false); | ||
| test('text/css;', false, true, false); | ||
| test('text/html', false, true, false); | ||
| test('text/javascript', false, true, false); | ||
| test('application/json', false, false, true); | ||
| test('application/json;', false, false, true); | ||
| test('application/json; charset=utf-8', false, false, true); | ||
| test('application/vnd.foo.picture+json', false, false, true); | ||
| test('application/vnd.foo.picture+json; charset=utf-8', false, false, true); | ||
| }); | ||
| }); |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var test_1 = require("../test"); | ||
| var _1 = require("."); | ||
| describe('toMimetype (content-type)', function () { | ||
| it('no match - defaultType', function () { | ||
| var res = (0, _1.toMimetype)('foo', 'text/plain'); | ||
| (0, test_1.expect)(res).to.eql('text/plain'); | ||
| }); | ||
| it('no match - <empty>', function () { | ||
| var res = (0, _1.toMimetype)('foo'); | ||
| (0, test_1.expect)(res).to.eql(''); | ||
| }); | ||
| it('matches', function () { | ||
| var test = function (input, expected) { | ||
| var res = (0, _1.toMimetype)(input); | ||
| (0, test_1.expect)(res).to.eql(expected); | ||
| }; | ||
| test(' foo.js ', 'application/javascript'); | ||
| test('foo.js', 'application/javascript'); | ||
| test('foo.json', 'application/json'); | ||
| test('foo.wasm', 'application/wasm'); | ||
| test('foo.yml', 'text/plain'); | ||
| test('foo.yaml', 'text/plain'); | ||
| test('foo.txt', 'text/plain'); | ||
| test('foo.css', 'text/css'); | ||
| test('foo.html', 'text/html'); | ||
| test('foo.htm', 'text/html'); | ||
| test('foo.jpg', 'image/jpeg'); | ||
| test('foo.jpeg', 'image/jpeg'); | ||
| test('foo.gif', 'image/gif'); | ||
| test('foo.svg', 'image/svg+xml'); | ||
| test('foo.webm', 'video/webm'); | ||
| test('foo.zip', 'application/zip'); | ||
| test('foo.pdf', 'application/pdf'); | ||
| test('foo.csv', 'text/csv'); | ||
| test('foo.tsv', 'text/csv'); | ||
| }); | ||
| }); |
+12
-3
@@ -15,3 +15,12 @@ "use strict"; | ||
| Mime.isJson = function (mimetype) { | ||
| return isIncluded(mimetype, ['application/json']); | ||
| var parts = (mimetype || '') | ||
| .trim() | ||
| .split(';') | ||
| .map(function (part) { return part.trim(); }) | ||
| .filter(Boolean); | ||
| return parts.some(function (part) { | ||
| if (!part.startsWith('application/')) | ||
| return false; | ||
| return part.endsWith('/json') || part.endsWith('+json'); | ||
| }); | ||
| }; | ||
@@ -22,5 +31,5 @@ Mime.toType = toMimetype_1.toMimetype; | ||
| exports.Mime = Mime; | ||
| function isIncluded(mimetype, values) { | ||
| function isIncluded(mimetype, match) { | ||
| mimetype = (mimetype || '').trim(); | ||
| return values.some(function (item) { return mimetype.includes(item); }); | ||
| return match.some(function (item) { return mimetype.includes(item); }); | ||
| } |
@@ -10,40 +10,34 @@ "use strict"; | ||
| input = (input || '').trim(); | ||
| if (input.endsWith('.js')) { | ||
| if (input.endsWith('.js')) | ||
| return 'application/javascript'; | ||
| } | ||
| if (input.endsWith('.json')) { | ||
| if (input.endsWith('.wasm')) | ||
| return 'application/wasm'; | ||
| if (input.endsWith('.json')) | ||
| return 'application/json'; | ||
| } | ||
| if (input.endsWith('.yaml') || input.endsWith('.yml')) { | ||
| return 'text/plain'; | ||
| } | ||
| if (input.endsWith('.txt')) { | ||
| return 'text/plain'; | ||
| } | ||
| if (input.endsWith('.html') || input.endsWith('.htm')) { | ||
| return 'text/html'; | ||
| } | ||
| if (input.endsWith('.css')) { | ||
| if (input.endsWith('.css')) | ||
| return 'text/css'; | ||
| } | ||
| if (input.endsWith('.png')) { | ||
| if (input.endsWith('.png')) | ||
| return 'image/png'; | ||
| } | ||
| if (input.endsWith('.jpg') || input.endsWith('.jpeg')) { | ||
| if (input.endsWith('.jpg') || input.endsWith('.jpeg')) | ||
| return 'image/jpeg'; | ||
| } | ||
| if (input.endsWith('.gif')) { | ||
| if (input.endsWith('.svg')) | ||
| return 'image/svg+xml'; | ||
| if (input.endsWith('.gif')) | ||
| return 'image/gif'; | ||
| } | ||
| if (input.endsWith('.zip')) { | ||
| if (input.endsWith('.webm')) | ||
| return 'video/webm'; | ||
| if (input.endsWith('.pdf')) | ||
| return 'application/pdf'; | ||
| if (input.endsWith('.yaml') || input.endsWith('.yml')) | ||
| return 'text/plain'; | ||
| if (input.endsWith('.txt')) | ||
| return 'text/plain'; | ||
| if (input.endsWith('.html') || input.endsWith('.htm')) | ||
| return 'text/html'; | ||
| if (input.endsWith('.zip')) | ||
| return 'application/zip'; | ||
| } | ||
| if (input.endsWith('.pdf')) { | ||
| return 'application/pdf'; | ||
| } | ||
| if (input.endsWith('.csv') || input.endsWith('.tsv')) { | ||
| if (input.endsWith('.csv') || input.endsWith('.tsv')) | ||
| return 'text/csv'; | ||
| } | ||
| return defaultType; | ||
| }; | ||
| exports.toMimetype = toMimetype; |
+3
-4
| { | ||
| "name": "@platform/util.mimetype", | ||
| "version": "0.1.48", | ||
| "version": "0.1.51", | ||
| "description": "Helpers for generating media-types.", | ||
@@ -9,3 +9,2 @@ "main": "lib/index", | ||
| "test": "ts test", | ||
| "tdd": "ts test --watch", | ||
| "lint": "ts lint", | ||
@@ -17,4 +16,4 @@ "build": "ts build --no-esm", | ||
| "devDependencies": { | ||
| "@platform/test": "0.2.10", | ||
| "@platform/ts": "4.1.23" | ||
| "@platform/test": "0.3.2", | ||
| "@platform/ts": "4.7.7" | ||
| }, | ||
@@ -21,0 +20,0 @@ "files": [ |
8543
88.34%18
12.5%196
83.18%