File

Utility functions for working with files.

Description:
  • Utility functions for working with files.

Source:

Methods

(static) getFileMd5(options) → {Promise.<string>}

Description:
  • Calculates the MD5 hash of a file and returns a promise that resolves with the hash.

Source:
Example
import { getFileMd5 } from "nsuite";
const md5 = await getFileMd5({ pathFile: "./package.json" });
Parameters:
Name Type Description
options Object

The options for the hash operation.

Properties
Name Type Description
pathFile string

The path to the file for which the hash is to be calculated.

Returns:
  • A promise that resolves with the MD5 hash of the file.
Type
Promise.<string>

(static) getReadableFileSize(size, optionsopt) → {string}

Description:
  • converts file size in bytes to human-readable string

Source:
Example
import { getReadableFileSize } from "nsuite";

getReadableFileSize(0); // "0 B"

// 1024-based, with { standard: "jedec" }
getReadableFileSize(1024, { standard: "jedec" }); // "1 KB"
getReadableFileSize(1024 * 1024, { standard: "jedec" }); // "1 MB"
getReadableFileSize(1024 * 1024 * 1024, { standard: "jedec" }); // "1 GB"

// 1000-based, default
getReadableFileSize(1000); // "1 kB"
getReadableFileSize(1001); // "1 kB"
getReadableFileSize(1010); // "1.01 kB"
getReadableFileSize(1100); // 1.1 kB"
getReadableFileSize(1024); // "1.02 kB"
getReadableFileSize(1024 * 1000); // "1.02 MB"
// 1024 * 1024 = 1048576
getReadableFileSize(1024 * 1024); // "1.05 MB"
// 1024 * 1024 * 1024 = 1073741824
getReadableFileSize(1024 * 1024 * 1024); // "1.07 GB"
Parameters:
Name Type Attributes Description
size number | string
options module:File~CustomFilesizeOptions <optional>
Returns:
Type
string

(static) getSafeFileName(fileName) → {string}

Description:
  • Generates a safe file name by replacing special characters with underscores. Consecutive underscores are also reduced to a single underscore.

Source:
Example
import { getSafeFileName } from "nsuite";
const safeFileName = getSafeFileName("测试有空格 和特殊符号 &.pdf");
Parameters:
Name Type Description
fileName string

The original file name to be sanitized.

Returns:
  • The sanitized file name with special characters replaced by underscores and consecutive underscores reduced to a single underscore.
Type
string

(static) unzipFile(options) → {Promise.<void>}

Description:
  • Unzips a file and returns a promise that resolves when the unzip operation is complete.

Source:
Example
import { unzipFile } from "nsuite";

await unzipFile({
  pathFile: pathDistZip,
  pathOutput: pathOutputDirectory,
});
Parameters:
Name Type Description
options Object

The options for the unzip operation.

Properties
Name Type Description
pathFile string

The path to the zip file to be unzipped.

pathOutput string

The path to the output directory.

Returns:
  • A promise that resolves when the unzip operation is complete.
Type
Promise.<void>

(static) zipFile(options) → {Promise.<number>}

Description:
  • Zips a single file and returns a promise that resolves when the zip operation is complete.

Source:
Example
import { zipFile } from "nsuite";
await zipFile({
  pathInputFile: "./package.json",
  pathOutputFile: "./package.json.zip",
});
Parameters:
Name Type Description
options Object

The options for the zip operation.

Properties
Name Type Description
pathInputFile string

The path to the input file to be zipped.

pathOutputFile string

The path to the output zip file.

Returns:
  • A promise that resolves with final zip file size in Bytes when the zip operation is complete.
Type
Promise.<number>

(static) zipFolder(options) → {Promise.<number>}

Description:
  • Zips a folder and returns a promise that resolves when the zip operation is complete.

Source:
Example
import { zipFolder } from "nsuite";
await zipFolder({
  pathFolder: "./dist",
  pathOutputFile: "./dist.zip",
});
Parameters:
Name Type Description
options Object

The options for the zip operation.

Properties
Name Type Description
pathFolder string

The path to the folder to be zipped.

pathOutputFile string

The path to the output zip file.

Returns:
  • A promise that resolves with final zip file size in Bytes when the zip operation is complete.
Type
Promise.<number>

Type Definitions

CustomFilesizeOptions

Source:
Properties:
Name Type Attributes Description
output string <optional>

output format, only 'string' is supported

Type:
  • module:filesize~FilesizeOptions