1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 17:49:16 +02:00

Update file sniffer to be an ES module

This commit is contained in:
codedread 2020-04-02 19:30:21 -07:00
parent 2cc77f905b
commit 0312134ae3
2 changed files with 5 additions and 11 deletions

View file

@ -6,11 +6,6 @@
* Copyright(c) 2020 Google Inc.
*/
var bitjs = bitjs || {};
bitjs.file = bitjs.file || {};
(function() {
// A selection from https://en.wikipedia.org/wiki/List_of_file_signatures.
// Mapping of MIME type to magic numbers. Each file type can have multiple signatures.
// '??' is used as a placeholder value.
@ -79,7 +74,7 @@ for (const mimeType in fileSignatures) {
* @param {ArrayBuffer} ab
* @return {string} The MIME type of the buffer, or undefined.
*/
bitjs.file.findMimeType = function(ab) {
export function findMimeType(ab) {
const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth;
const arr = new Uint8Array(ab).subarray(0, depth);
let curNode = root;
@ -94,5 +89,3 @@ bitjs.file.findMimeType = function(ab) {
if (curNode.mimeType) return curNode.mimeType;
}
};
})();

View file

@ -3,10 +3,11 @@
<head>
<title>Unit tests for bitjs.io.ByteStreamBitStream</title>
<script src="muther.js"></script>
<script src="../file/sniffer.js"></script>
</head>
<body>
<script>
<script type="module">
import { findMimeType } from '../file/sniffer.js';
const assert = muther.assert;
const assertEquals = muther.assertEquals;
const assertThrows = muther.assertThrows;
@ -14,7 +15,7 @@
function sniffTest(mimeType, sig) {
const array = new Uint8Array(16);
array.set(sig);
assertEquals(mimeType, bitjs.file.findMimeType(array.buffer));
assertEquals(mimeType, findMimeType(array.buffer));
}
muther.go({