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

ts-x-file

v1.0.24

Published

A smart way to manage your File.

Readme

ts-x-file

Easily check the file size in any unit, change the file name or validate format of the file.

Updates from 1.0.14 version:

  • The XFile instance could be initiated from Buffer and now could be used by Node application.

  • In the isExtension() method now can be passed the Array<string> to validate extension with array of allowed values

  • Fixed bug with build in Angular application

  • More cases covered with new tests

Installation

$ npm install -s ts-x-file

Usage

Node example:
export class FileService {
    ...

    loadFile(): XFile {
        const buffer = fs.readFileSync('file.js');
        return new XFile(buffer);
    }
}
Angular example:
<input type="file" (change)="onFileChange($event)"/>
In your controller method:
onFileChange(event: any): void {
    const input = event.tartget as HTMLInputElement;

    // FileList initiation
    const xFile = new XFile(input.files);
    
    // Or you can pass File 
    const xFile = new XFile(input.files.item(0));
}

You can pass a parameter to constructor to change default unit:

import { XFile } from "ts-x-file";

onFileChange(event: any): void {
    const xFile = new XFile(event.target.files, FileSizeUnits.MB);
}

Then if you request a size of file it will be returned in this unit. By default, unit is KiloBytes.

Check if file size is not grater than:
isLessThan(limit: number, unit?: FileSizeUnits): boolean;
import { XFile, FileSizeUnits } from "ts-x-file";

const xFile = new XFile(event.target.files);
xFile.isLessThan(5000);

By default limit parameter is calculated as Bytes (if you did not change it through the constructor), you can change it by passing FileSizeUnit:

import { FileSizeUnits } from "ts-x-file";

const xFile = new XFile(event.target.files);

xFile.isLessThan(5, FileSizeUnits.B);
xFile.isLessThan(5, FileSizeUnits.KB);
xFile.isLessThan(5, FileSizeUnits.MB);
xFile.isLessThan(5, FileSizeUnits.GB);

This way it will ignore the default unit.

Get file size
size(round?: boolean = false, unit?: FileSizeUnits): number;
const fileSize = xFile.size();

By default this method won't round size number, you can do this by passing true on the first place parameter:

const fileSize = xFile.size(true);

The unit of returned value could be ignored, as well, if you pass it as second parameter:

import { FileSizeUnits } from "ts-x-file";

const fileSizeBytes     = xFile.size(true, FileSizeUnits.B);
const fileSizeKiloBytes = xFile.size(true, FileSizeUnits.KB);
const fileSizeMegaBytes = xFile.size(true, FileSizeUnits.MB);
const fileSizeGigaBytes = xFile.size(true, FileSizeUnits.GB);
Get or check extension
getExtension(): string;
const extension = xFile.getExtension();

you are able to check file extension by using method:

isExtension(extension: string | string[]): boolean;
const isPdf = xFile.isExtension('pdf');

of you can pass the array of allowed values:

const isPdf = xFile.isExtension(['jpg', 'JPEG']);

Note: the values are not case sensitive.

Get or change file name

Getting name of the file could be done by simply calling name getter:

console.log(xFile.name);

To set name property of file you can follow:

changeName(name: string, skipExt?: boolean): void;
const xFile = new XFile(event.target.files) // ex: react.js
console.log(xFile.name) // out: react.js

xFile.changeName('angular.js');
console.log(xFile.name) // out: angular.js

If you want to change extension of file, you just need to pass it as name parameter and set skipExt parameter to true:

xFile.changeName('angular.ts', true);
console.log(xFile.name) // out: angular.ts

// If you skip to set `skipExt` parameter you should get something like this:
xFile.changeName('angular.ts');
console.log(xFile.name) // out: angular.ts.js
Get the File instace or Base64

Somehow, on the end you will need an instance or Base64 string, so this is possible with following methods:

getFile(): File;
getBase64(): Promise<string>;

File instance example:

const file: File = xFile.getFile();

Base64 string example:

const urlString = xFile.getBase64().then(console.log) // data:image/png;base64,...
Check if File instance exists:
fileExists(): boolean;

This could be used to validate:

if (xFile.fileExists()) {
...
}

Testing

Run tests with following command:

$ jest

Todos

  • Create collection of XFile instances to perform bulk actions
  • Write MORE Tests

License

MIT

Feel free to contribute!