@igorskyflyer/my-file-path
v1.0.2
Published
๐ A package that strongly types file paths! ๐ฅ
Maintainers
Readme
๐ Table of Contents
๐ค Features
- ๐ Typeโsafe file path handling via the
FilePath<ext>template literal type - ๐ Extension validation with
isMyFilePath()for caseโinsensitive file path checks - ๐ก๏ธ Assertion with error handling using
assertMyFilePath(), supporting custom error messages - ๐ฟ Conditional file path creation through
createMyFilePath(), returning a typed path or empty string - ๐จ Customisable error formatting powered by the external
zingutility
๐ต๐ผ Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/my-file-path
yarn add @igorskyflyer/my-file-path
npm i @igorskyflyer/my-file-path๐คน๐ผ API
The module exposes a generic type and 3 generic functions.
Type
type FilePath<ext extends string>Use this type for compile-time safety.
index.ts
type JsonFile = FilePath<'json'>
const filePathJson: JsonFile = 'abc.json' // works in the IDE
const filePathTxt: JsonFile = 'abc.txt' // will not work in the IDEFunctions
isMyFilePath
function isMyFilePath<Ext extends string>(
filePath: string,
extension: Ext
): booleanChecks whether the provided filepath conforms with the provided file extension.
Generic parameter:
Ext - the file extension to use with the generic, without a leading dot.
Function parameters:
filePath - the filepath to check.
extension - the file extension to perform the check with.
Will not throw.
Returns a boolean.
createMyFilePath
function createMyFilePath<Ext extends string>(
filePath: string,
extension: Ext
): FilePath<Ext>Checks whether the provided filepath conforms with the provided file extension and returns it if true. Otherwise it returns an empty string.
Generic parameter:
Ext - the file extension to use with the generic, without a leading dot.
Function parameters:
filePath - the filepath to check.
extension - the file extension to perform the check with.
Will not throw.
Returns a FilePath<Ext>.
assertMyFilePath
function assertMyFilePath<Ext extends string>(
filePath: string,
extension: Ext,
errorMessage?: string
): voidAsserts whether the provided filepath conforms with the provided file extension.
Generic parameter:
Ext - the file extension to use with the generic, without a leading dot.
Function parameters:
filePath - the filepath to check.
extension - the file extension to perform the check with.
errorMessage - the error message to throw.
Will throw if the filepath doesn't conform the provided extension.
Returns a void.
๐ Changelog
๐ The changelog is available here, CHANGELOG.md.
๐ชช License
Licensed under the MIT license which is available here, MIT license.
๐ Support
๐งฌ Related
๐ Provides a universal way of formatting file-paths in Unix-like and Windows operating systems as an alternative to the built-in path.normalize(). ๐งฌ
๐ Provides ways of checking whether a path is a legacy Windows device. ๐พ
๐ A package that strongly types file paths! ๐ฅ
@igorskyflyer/recursive-readdir
๐ Provides recursive readdir() and readdirSync() functions. ๐
๐ Opens a given resource (URL, file, etc.) via the default OS handler. ๐
๐จ๐ปโ๐ป Author
Created by Igor Dimitrijeviฤ (@igorskyflyer).
