ts-x-file
Easily check the file size in any unit, change the file name or validate format of the file.
1.0.14
version:
Updates from -
The XFile instance could be initiated from Buffer and now could be used by Node application.
-
In the
isExtension()
method now can be passed theArray<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:
... : XFile const buffer = fs; return buffer;
Angular example:
<input type="file" (change)="onFileChange($event)"/>
In your controller method:
: void const input = eventtartget as HTMLInputElement; // FileList initiation const xFile = inputfiles; // Or you can pass File const xFile = inputfiles;
You can pass a parameter to constructor to change default unit:
; : void const xFile = eventtargetfiles FileSizeUnitsMB;
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:
isLessThanlimit: number, unit?: FileSizeUnits: boolean;
; const xFile = eventtargetfiles;xFile;
By default limit parameter is calculated as Bytes (if you did not change it through the constructor), you can change it by passing FileSizeUnit:
; const xFile = eventtargetfiles; xFile;xFile;xFile;xFile;
This way it will ignore the default unit.
Get file size
sizeround?: boolean = false, unit?: FileSizeUnits: number;
const fileSize = xFilesize;
By default this method won't round size number, you can do this by passing true on the first place parameter:
;
The unit of returned value could be ignored, as well, if you pass it as second parameter:
; const fileSizeBytes = xFilesizetrue FileSizeUnitsB;const fileSizeKiloBytes = xFilesizetrue FileSizeUnitsKB;const fileSizeMegaBytes = xFilesizetrue FileSizeUnitsMB;const fileSizeGigaBytes = xFilesizetrue FileSizeUnitsGB;
Get or check extension
getExtension: string;
const extension = xFile;
you are able to check file extension by using method:
isExtensionextension: string | string: boolean;
const isPdf = xFile;
of you can pass the array of allowed values:
const isPdf = xFile;
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;
To set name property of file you can follow:
changeNamename: string, skipExt?: boolean: void;
const xFile = eventtargetfiles // ex: react.jsconsole // out: react.js xFile;console // 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;console // out: angular.ts // If you skip to set `skipExt` parameter you should get something like this:xFile;console // out: angular.ts.js
File
instace or Base64
Get the 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;
Base64
string example:
const urlString = xFile // data:image/png;base64,...
File
instance exists:
Check if fileExists: boolean;
This could be used to validate:
if xFile ...
Testing
Run tests with following command:
$ jest
Todos
- Create collection of XFile instances to perform bulk actions
- Write MORE Tests
License
MIT