Skip to content

Commit

Permalink
feat: add utf8bom and utf16 detection detection
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsKoelpin committed Sep 26, 2023
1 parent 78222e2 commit e8415f9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,22 @@ describe("Tests the public API", () => {
expect(result).toHaveLength(0);
expect(result).toEqual([]);
});

it("detects utf8", () => {
const file = getBytes("a.utf8");
const result = filetypemime(file);
expect(result).toContain("text/plain; charset=UTF-8");
});

it("detects utf16le", () => {
const file = getBytes("a.utf16le");
const result = filetypemime(file);
expect(result).toContain("text/plain; charset=UTF-16LE");
});

it("detects utf16be", () => {
const file = getBytes("a.utf16be");
const result = filetypemime(file);
expect(result).toContain("text/plain; charset=UTF-16BE");
});
});
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,20 @@ const walkTree = (

export default filetypeinfo;

export const filetypename = (bytes: number[] | Uint8Array | Uint8ClampedArray): string[] =>
filetypeinfo(bytes).map((e) => e.typename);
export const filetypemime = (bytes: number[] | Uint8Array | Uint8ClampedArray): string[] =>
filetypeinfo(bytes).map((e) => (e.mime ? e.mime : ""));
export const filetypeextension = (bytes: number[] | Uint8Array | Uint8ClampedArray): string[] =>
filetypeinfo(bytes).map((e) => (e.extension ? e.extension : ""));
export const filetypename = (
bytes: number[] | Uint8Array | Uint8ClampedArray
): string[] => filetypeinfo(bytes).map((e) => e.typename);

export const filetypemime = (
bytes: number[] | Uint8Array | Uint8ClampedArray
): string[] =>
filetypeinfo(bytes)
.map((e) => (e.mime ? e.mime : null))
.filter((x) => x !== null) as string[];

export const filetypeextension = (
bytes: number[] | Uint8Array | Uint8ClampedArray
): string[] =>
filetypeinfo(bytes)
.map((e) => (e.extension ? e.extension : null))
.filter((x) => x !== null) as string[];
23 changes: 21 additions & 2 deletions src/model/pattern-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,25 @@ add(
0x4
);

add("hl2demo", ["48", "4C", "32", "44", "45", "4D", "4F"]);

add("hl2demo", ["0x48", "0x4C", "0x32", "0x44", "0x45", "0x4D", "0x4F"]);
add("txt", ["0xEF", "0xBB", "0xBF"], {
mime: "text/plain; charset=UTF-8",
extension: "txt",
});
add("txt", ["0xFF", "0xFE"], {
mime: "text/plain; charset=UTF-16LE",
extension: "txt",
});
add("txt", ["0xFE", "0xFF"], {
mime: "text/plain; charset=UTF-16BE",
extension: "txt",
});
add("txt", ["0xFF", "0xFE", "0x00", "0x00"], {
mime: "text/plain; charset=UTF-32LE",
extension: "txt",
});
add("txt", ["00", "00", "FE", "FF"], {
mime: "text/plain; charset=UTF-32BE",
extension: "txt",
});
export default (): Tree => tree as Tree;
Binary file added src/testfiles/a.utf16be
Binary file not shown.
Binary file added src/testfiles/a.utf16le
Binary file not shown.
1 change: 1 addition & 0 deletions src/testfiles/a.utf8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
thusisutf8

0 comments on commit e8415f9

Please sign in to comment.