# @guanghechen/filepart

> File helper

Latest version **2.0.4** (published 2026-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @guanghechen/filepart
pnpm add @guanghechen/filepart
yarn add @guanghechen/filepart
bun add @guanghechen/filepart
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2026-09-12 |
| First published | 2023-12-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | guanghechen |
| Maintainers | lemonclown |
| Keywords | file helper, split file, merge streams |

## Links

- npm: https://www.npmjs.com/package/@guanghechen/filepart
- Repository: https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.4
- Homepage: https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.4/packages/filepart#readme
- npm.io page: https://npm.io/package/@guanghechen/filepart

## Dependencies (1)

- [@guanghechen/invariant](https://npm.io/package/@guanghechen/invariant.md) ^7.0.4

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 2.0.4 (latest) — 2026-09-12
- 2.0.3 — 2026-07-10
- 2.0.2 — 2026-02-15
- 2.0.1 — 2026-02-07
- 2.0.0 — 2026-02-04
- 1.0.4 — 2025-02-26
- 1.0.3 — 2024-10-03
- 1.0.2 — 2024-09-28
- 1.0.1 — 2024-09-25
- 1.0.0 — 2024-09-20
- 1.0.0-alpha.7 — 2024-09-18
- 1.0.0-alpha.6 — 2024-03-10
- 1.0.0-alpha.5 — 2024-03-09
- 1.0.0-alpha.4 — 2024-02-03
- 1.0.0-alpha.3 — 2024-01-15
- … 2 more at https://npm.io/package/@guanghechen/filepart/versions

## README

<header>
  <h1 align="center">
    <a href="https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.4/packages/filepart#readme">@guanghechen/filepart</a>
  </h1>
  <div align="center">
    <a href="https://www.npmjs.com/package/@guanghechen/filepart">
      <img
        alt="Npm Version"
        src="https://img.shields.io/npm/v/@guanghechen/filepart.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/filepart">
      <img
        alt="Npm Download"
        src="https://img.shields.io/npm/dm/@guanghechen/filepart.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/filepart">
      <img
        alt="Npm License"
        src="https://img.shields.io/npm/l/@guanghechen/filepart.svg"
      />
    </a>
    <a href="#install">
      <img
        alt="Module Formats: cjs, esm"
        src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
      />
    </a>
    <a href="https://github.com/nodejs/node">
      <img
        alt="Node.js Version"
        src="https://img.shields.io/node/v/@guanghechen/filepart"
      />
    </a>
    <a href="https://github.com/facebook/jest">
      <img
        alt="Tested with Jest"
        src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
      />
    </a>
    <a href="https://biomejs.dev/">
      <img
        alt="Code Style: Biome"
        src="https://img.shields.io/badge/code_style-biome-60a5fa.svg?style=flat-square"
      />
    </a>
  </div>
</header>
<br/>

File helper utilities for calculating file part ranges and generating part names. Useful for
splitting large files into smaller chunks.

## Install

- npm

  ```bash
  npm install --save @guanghechen/filepart
  ```

- yarn

  ```bash
  yarn add @guanghechen/filepart
  ```

## Usage

### Calculate File Parts by Size

Split a file into parts of a specific size:

```typescript
import { calcFilePartItemsBySize } from '@guanghechen/filepart'

const fileSize = 1000 // bytes
const partSize = 300  // bytes per part

const parts = [...calcFilePartItemsBySize(fileSize, partSize)]
// Result:
// [
//   { sid: 1, start: 0, end: 300 },
//   { sid: 2, start: 300, end: 600 },
//   { sid: 3, start: 600, end: 900 },
//   { sid: 4, start: 900, end: 1000 }
// ]
```

### Calculate File Parts by Count

Split a file into a specific number of parts. For non-empty files, the part count must not exceed
the file size in bytes; every part contains at least one byte. Empty files produce one empty part.

```typescript
import { calcFilePartItemsByCount } from '@guanghechen/filepart'

const fileSize = 1000 // bytes
const partCount = 3   // number of parts

const parts = [...calcFilePartItemsByCount(fileSize, partCount)]
// Result:
// [
//   { sid: 1, start: 0, end: 334 },
//   { sid: 2, start: 334, end: 668 },
//   { sid: 3, start: 668, end: 1000 }
// ]
```

### Generate Part Names

Generate file part names with padded sequence numbers:

```typescript
import { calcFilePartNames, calcFilePartNamesByCount } from '@guanghechen/filepart'

// From existing parts
const parts = [{ sid: 1 }, { sid: 2 }, { sid: 3 }]
const names = [...calcFilePartNames(parts, '.part')]
// Result: ['.part1', '.part2', '.part3']

// For 14 parts (with zero-padding)
const names14 = [...calcFilePartNamesByCount(14, '.part')]
// Result: ['.part01', '.part02', ... '.part14']

// Single part returns empty string (no suffix needed)
const singlePart = [...calcFilePartNamesByCount(1, '.part')]
// Result: ['']
```

### Constants

```typescript
import { DEFAULT_FILEPART_CODE_PREFIX } from '@guanghechen/filepart'

console.log(DEFAULT_FILEPART_CODE_PREFIX) // '.ghc-part'
```

## Reference

- [homepage][homepage]

[homepage]:
  https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.4/packages/filepart#readme

---
_Source: https://npm.io/package/@guanghechen/filepart · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
