# @scale-codec/core

> Low-level tools to perform serialization and deserialization via SCALE spec

Latest version **2.0.1** (published 2023-02-28) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @scale-codec/core
pnpm add @scale-codec/core
yarn add @scale-codec/core
bun add @scale-codec/core
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2023-02-28 |
| First published | 2021-07-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.5.0 |
| Dependencies | 2 |
| Unpacked size | 82.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | soramitsu-admin |
| Keywords | SCALE, codec, binary, parity-scale-codec, serialization, typescript, rust, encoding |

## Links

- npm: https://www.npmjs.com/package/@scale-codec/core
- Repository: https://github.com/soramitsu/scale-codec-js-library
- Homepage: https://soramitsu.github.io/scale-codec-js-library/
- Issues: https://github.com/soramitsu/scale-codec-js-library/issues
- npm.io page: https://npm.io/package/@scale-codec/core

## Dependencies (2)

- [@scale-codec/enum](https://npm.io/package/@scale-codec/enum.md) ^2.1.1
- [@scale-codec/util](https://npm.io/package/@scale-codec/util.md) ^1.1.2

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2023-02-28
- 2.0.0 — 2023-02-13
- 1.1.1 — 2022-09-10
- 1.1.0 — 2022-08-19
- 1.0.1 — 2022-03-15
- 1.0.0 — 2022-02-14
- 0.4.2 — 2022-01-27
- 0.4.1 — 2021-12-09
- 0.4.0 — 2021-11-05
- 0.3.0 — 2021-11-01
- 0.2.3 — 2021-10-29
- 0.2.2 — 2021-10-13
- 0.2.1 — 2021-09-28
- 0.2.0 — 2021-09-14
- 0.1.0 — 2021-07-21

## README

# @scale-codec/core ![build status](https://img.shields.io/github/checks-status/soramitsu/scale-codec-js-library/master) ![version](https://img.shields.io/npm/v/@scale-codec/core) ![license](https://img.shields.io/npm/l/@scale-codec/core)

Low-level tools to perform serialization and deserialization according to SCALE spec.

## Installation

Available on NPM:

```bash
npm i @scale-codec/core
```

## Example

**Encode:**

```ts
import { createStructEncoder, Encode, encodeStr, encodeU64, WalkerImpl } from '@scale-codec/core'

const foo = { bar: 'baz', foo: 90n }

const encodeFoo: Encode<typeof foo> = createStructEncoder([
  ['bar', encodeStr],
  ['foo', encodeU64],
])

const bytes = WalkerImpl.encode(foo, encodeFoo)
```

**Decode:**

```ts
import { Decode, WalkerImpl, createStructDecoder, decodeStr, decodeU64 } from '@scale-codec/core'

const bytes = new Uint8Array([12, 98, 97, 122, 90, 0, 0, 0, 0, 0, 0, 0])

interface Foo {
  bar: string
  foo: bigint
}

const decodeFoo: Decode<Foo> = createStructDecoder([
  ['bar', decodeStr],
  ['foo', decodeU64],
])

const foo = WalkerImpl.decode(bytes, decodeFoo)
```

## Supported types

**Primitive:**

| Spec      | JS Type             | Details                                                                                                                                                           |
| --------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Int       | `number`, `bigint`  | signed/unsigned, 8/16/32/64/128/etc bits. For integers with 64+ bits, you can only use `bigint`. For integers with less bits, you can use both `number` and `bigint`. |
| Compact   | `number`, `bigint`  | -                                                                                                                                                                 |
| String    | `string`            | -                                                                                                                                                                 |
| Bool      | `boolean`           | -                                                                                                                                                                 |
| Unit type | `null`, `undefined` | `()` in Rust. JavaScript doesn't have zero-cost abstractions, so this codec could be used to handle them.                                            |

**Higher-order:**

| Spec   | JS Type                                                                                                             |
| ------ | ------------------------------------------------------------------------------------------------------------------- |
| Array  | `Array`                                                                                                             |
| Vector | `Array`                                                                                                             |
| Tuple  | `Array`, but tuple in TypeScript                                                                                    |
| Set    | `Set`                                                                                                               |
| Map    | `Map`                                                                                                               |
| Struct | Plain `Object`                                                                                                      |
| Enum   | `Variant` from [`@scale-codec/enum`](https://github.com/soramitsu/scale-codec-js-library/tree/master/packages/enum) |

**Special:**

- Efficient codec for arrays of bytes (`[u8; x]` in Rust and `Uint8Array` in JS)
- Efficient codec for vectors of bytes (`Vec<u8>` in Rust and `Uint8Array` in JS)
- `OptionBool`

## API

[`@scale-codec/core` API](https://soramitsu.github.io/scale-codec-js-library/api/modules/scale_codec_core)

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