# @ckitjs/easy-byte

> ## Quick Start

Latest version **0.2.0** (published 2022-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ckitjs/easy-byte
pnpm add @ckitjs/easy-byte
yarn add @ckitjs/easy-byte
bun add @ckitjs/easy-byte
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-03-31 |
| First published | 2021-09-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 76.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | homura, mkxbl |

## Links

- npm: https://www.npmjs.com/package/@ckitjs/easy-byte
- npm.io page: https://npm.io/package/@ckitjs/easy-byte

## Dependencies (1)

- [buffer](https://npm.io/package/buffer.md) ^6.0.3

## Recent versions

- 0.2.0 (latest) — 2022-03-31
- 0.1.0 — 2022-02-08
- 0.0.2 — 2021-09-09
- 0.0.1 — 2021-09-06

## README

# @ckitjs/easy-byte

## Quick Start

### Work With Struct

```ts
import { createFixedStruct, U8, U64LE } from '@ckitjs/easy-byte';

// prettier-ignore
// define the struct first
const messageStruct = createFixedStruct()
  .field('messageHeader', U8)
  .field('messageBody', U64LE);

const buf = messageStruct.encode({
  messageHeader: 0x01,
  // A value of u64 may exceed MAX_SAFE_INTEGER, so use a bigint to store this value
  // The built-in field parsing U64le also only supports reading and writing bigint
  messageBody: 0xffffffffffffffffn,
});

console.log(buf.tostring('hex')); // 01ffffffffffffffff

// also read a bytes and parse to an JS Object
console.log(messageStruct.decode(buf)); // { messageHeader: 0x01, messageBody: 0xffffffffffffffffn }
```

### Custom Work Pipe

```ts
import {
  pipe,
  convertEndian,
  pad0x,
  prependZeroToEvenLength,
  rm0x,
} from '@ckitjs/easy-byte';

const customFormat = pipe(
  rm0x,
  prependZeroToEvenLength,
  convertEndian,
  pad0x,
);
customFormat('0x001020304'); // 0x0403020100
```

### Or Work With `formatByteLike`

```ts
import { formatByteLike } from '@ckitjs/easy-byte';

// format a big endian byte string to big endian
// (0x)  0_01_02_03_04
// (0x) 04_03_02_01_00
const formated = formatByteLike('0x001020304', {
  convertEndian: true,
  rm0x: true,
  byteSize: 8,
});
console.log(formated); // 0403020100000000
```

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