npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

file-extension-inspect-kit

v0.1.0

Published

Inspect file extensions with explicit dotfile, compound extension, and validation policies.

Downloads

111

Readme

file-extension-inspect-kit

License: MPL-2.0 CI

Inspect file extensions from filenames or path-like strings with explicit policies for dotfiles, extensionless names, compound extensions and invalid input.

file-extension-inspect-kit is a small clean-room toolkit for upload forms, import pipelines, asset validators and browser-side developer tools. It is intentionally narrower than a full path parser or MIME detector.

Links: Demo · GitHub

Package quality

  • TypeScript types are generated from the source.
  • ESM-only package with no runtime dependencies.
  • Marked as side-effect free for bundlers.
  • Browser-friendly implementation with no Node-only APIs.
  • CI runs npm ci, typecheck, build, and test.
  • Tested on Node.js 20 and 22 with GitHub Actions.

Publication status

This package is currently a GitHub preview and is queued for npm publication. The browser demo is available now, and the install command below is the command to use once the npm package is published.

Install after npm publication

npm install file-extension-inspect-kit

Quick Start

import {
  createFileExtensionInspector,
  getFileExtension,
  hasFileExtension,
  inspectFileExtension
} from "file-extension-inspect-kit";

getFileExtension("archive.tar.gz");
// "gz"

inspectFileExtension("  report.PDF  ");
// {
//   ok: true,
//   input: "report.PDF",
//   fileName: "report.PDF",
//   extension: "pdf",
//   diagnostics: ["input-trimmed"],
//   ...
// }

inspectFileExtension("/uploads/.env", { dotfile: "name" });
// {
//   ok: true,
//   input: "/uploads/.env",
//   fileName: ".env",
//   stem: ".env",
//   effectiveStem: ".env",
//   extension: "",
//   compoundExtension: "",
//   effectiveExtension: "",
//   segments: [],
//   diagnostics: ["dotfile-treated-as-name"]
// }

const archives = createFileExtensionInspector({
  compoundExtensions: ["tar.gz", "tar.bz2"]
});

archives.inspect("backup.tar.gz");
// {
//   ok: true,
//   fileName: "backup.tar.gz",
//   stem: "backup.tar",
//   effectiveStem: "backup",
//   extension: "gz",
//   compoundExtension: "tar.gz",
//   effectiveExtension: "tar.gz",
//   segments: ["tar", "gz"],
//   diagnostics: ["compound-extension-matched"],
//   ...
// }

hasFileExtension("avatar.PNG", [".jpg", "png"]);
// true

Why This Package

Filename extension checks are deceptively small. Dotfiles, compound extensions and extensionless names often produce quiet edge-case bugs:

  • .env can be treated as a name, an extension, or no extension depending on your product.
  • archive.tar.gz may need gz for a simple check or tar.gz for an archive validator.
  • Makefile might be extensionless or a known extension-like name.
  • Pasted input often contains harmless surrounding whitespace.
  • UI display may need original casing while validation should still be case-insensitive.
  • UI validation often needs diagnostics, not just an empty string.

This package makes those choices explicit and keeps the result inspectable.

API

inspectFileExtension(input, options?)

Returns a structured result. Expected invalid values return { ok: false } instead of throwing.

const result = inspectFileExtension("archive.tar.gz", {
  compoundExtensions: ["tar.gz"]
});

if (result.ok) {
  result.extension;
  // "gz"

  result.effectiveExtension;
  // "tar.gz"
}

Successful results include:

type FileExtensionInfo = {
  ok: true;
  input: string;
  fileName: string;
  stem: string;
  effectiveStem: string;
  extension: string;
  compoundExtension: string;
  effectiveExtension: string;
  segments: string[];
  diagnostics: FileExtensionDiagnostic[];
};

getFileExtension(input, options?)

Returns only the final extension string, or an empty string when no extension is found.

getFileExtension("photo.JPEG");
// "jpeg"

getFileExtension("photo.JPEG", { caseMode: "preserve" });
// "JPEG"

hasFileExtension(input, expected, options?)

Checks one extension or a list of extensions. Leading dots are accepted in expected.

hasFileExtension("avatar.PNG", [".jpg", "png"]);
// true

hasFileExtension("backup.tar.gz", "tar.gz", {
  compoundExtensions: ["tar.gz"]
});
// true

hasFileExtension("avatar.PNG", "png", {
  caseMode: "preserve"
});
// true

hasFileExtension("avatar.PNG", "png", {
  caseMode: "preserve",
  caseSensitive: true
});
// false

splitFileExtension(input, options?)

Returns the final and effective split for successful inputs.

splitFileExtension("index.d.ts", {
  compoundExtensions: ["d.ts"]
});
// {
//   stem: "index.d",
//   effectiveStem: "index",
//   extension: "ts",
//   effectiveExtension: "d.ts"
// }

createFileExtensionInspector(defaultOptions?)

Creates a small inspector object that reuses the same defaults across a form, importer or validator. Per-call options override the defaults.

const inspector = createFileExtensionInspector({
  compoundExtensions: ["d.ts", "tar.gz"],
  dotfile: "name"
});

inspector.get("index.d.ts");
inspector.has("archive.tar.gz", "tar.gz");
inspector.split(".env");

Options

| Option | Default | Description | | --- | --- | --- | | caseMode | "lower" | Lowercase returned extensions. Use "preserve" to keep original casing in returned values. | | caseSensitive | false | Match compoundExtensions and hasFileExtension values case-insensitively by default. | | trim | true | Trim surrounding whitespace before inspecting the filename. | | dotfile | "name" | How single-segment dotfiles such as .env are treated: "name", "extension", or "empty". | | extensionless | "empty" | How names without dots are treated: "empty" or "name". | | compoundExtensions | [] | Known compound extensions such as "tar.gz" or "d.ts". Leading dots are accepted. |

Diagnostics

Diagnostics are stable strings intended for logs, UI hints or tests:

  • empty-input
  • blank-input
  • input-trimmed
  • path-ends-with-separator
  • dotfile-treated-as-name
  • dotfile-treated-as-extension
  • extensionless-treated-as-name
  • compound-extension-matched

Limits

This package scans the final filename segment in a string. It does not:

  • normalize paths;
  • resolve directories;
  • touch the filesystem;
  • infer MIME types;
  • validate whether an extension is safe;
  • parse URLs or remove query strings;
  • replace Node's path.parse.

Use it as a small extension-inspection layer before product-specific validation.

License

MPL-2.0