# ts-code-enum

> A TypeScript string enum for compile-time safety when working with event.code

Latest version **0.0.3** (published 2023-09-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-code-enum
pnpm add ts-code-enum
yarn add ts-code-enum
bun add ts-code-enum
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2023-09-29 |
| First published | 2023-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Hubert Yuan |
| Maintainers | hubertyuan |
| Keywords | typescript, javascript, keycodes, keyevent, code, key, enum, ts, event.which, event.code, event.keycode |

## Links

- npm: https://www.npmjs.com/package/ts-code-enum
- Repository: https://github.com/HubertY/ts-code-enum
- Homepage: https://github.com/HubertY/ts-code-enum#readme
- Issues: https://github.com/HubertY/ts-code-enum/issues
- npm.io page: https://npm.io/package/ts-code-enum

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2023-09-29
- 0.0.2 — 2023-09-29

## README

# ts-code-enum

A TypeScript string enum for compile-time safety when working with `event.code`.

Inspired by https://gitlab.com/nfriend/ts-key-enum.

## Installation

```sh
npm install ts-code-enum --save
```

```sh
yarn add ts-code-enum
```

## Usage

For a [basic `enum`](https://www.typescriptlang.org/docs/handbook/enums.html#enums):
```ts
import { KBCode } from "ts-code-enum";
```

For a [`const enum`](https://www.typescriptlang.org/docs/handbook/enums.html#const-enums):
```ts
import { KBCode } from "ts-code-enum/const";
```

Then:
```ts
// if (ev.code === 'Escape') { ... }
if (ev.code === KBCode.Escape) { ... }
```

`const enums` are replaced by plain strings at compile-time. If you are using Typescript and your toolchain supports `const enum`, use `ts-code-enum/const`. Otherwise use `ts-code-enum`.

## About `event.code`

[MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code): The `KeyboardEvent.code` property represents a physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys.

Enum data is obtained by scraping the `code` values for major platforms from [MDN](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values). Values are included if they are present on **all** major platforms. Alphanumeric keys such as `KBCode.KeyQ = "KeyQ"` are included.

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