Skip to content

file

parse

Signature

js
file.parse(filePath)

Description

Parses a file path into its components. Supports both Unix and Windows path separators.

Parameters

NameTypeRequiredDescription
filePathstringyesThe file path to parse

Returns

TypeDescription
objectAn object containing the parsed components
PropertyTypeDescription
fileNamestringThe full file name including extension
extensionstringThe full extension including the dot (e.g. .tar.gz), or '' if none
baseNamestringThe file name without extension
dirstringThe directory path, '.' if no directory, or '' if at root

Throws

Throws a TypeError if filePath is not a string.

Examples

js
file.parse('/foo/bar/baz.txt')
// { fileName: 'baz.txt', extension: '.txt', baseName: 'baz', dir: '/foo/bar' }

file.parse('C:\\foo\\bar\\baz.txt')
// { fileName: 'baz.txt', extension: '.txt', baseName: 'baz', dir: 'C:/foo/bar' }

file.parse('/foo/bar/archive.tar.gz')
// { fileName: 'archive.tar.gz', extension: '.tar.gz', baseName: 'archive', dir: '/foo/bar' }

file.parse('/foo/.bashrc')
// { fileName: '.bashrc', extension: '', baseName: '.bashrc', dir: '/foo' }

file.parse('/baz.txt')
// { fileName: 'baz.txt', extension: '.txt', baseName: 'baz', dir: '' }

file.parse('baz.txt')
// { fileName: 'baz.txt', extension: '.txt', baseName: 'baz', dir: '.' }

formatSize

Signature

js
file.formatSize(bytes)

Description

Converts a size in bytes into a human-readable value and unit. The unit is automatically selected based on the magnitude of the value.

Parameters

NameTypeRequiredDescription
bytesnumberyesThe size in bytes

Returns

TypeDescription
objectAn object containing the formatted value and unit
PropertyTypeDescription
valuenumberThe numeric value, rounded to 2 decimal places if not an integer
unitstringOne of 'B', 'KB', 'MB', 'GB'

Throws

Throws a TypeError if bytes is not a number.

Examples

js
file.formatSize(512)
// { value: 512, unit: 'B' }

file.formatSize(1024)
// { value: 1, unit: 'KB' }

file.formatSize(1500)
// { value: 1.46, unit: 'KB' }

file.formatSize(1024 ** 2)
// { value: 1, unit: 'MB' }

file.formatSize(1024 ** 3)
// { value: 1, unit: 'GB' }