# enum-opt

> typescript enum to options

Latest version **1.4.2** (published 2024-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install enum-opt
pnpm add enum-opt
yarn add enum-opt
bun add enum-opt
```

Provides the command `enum-opt`.

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.2 |
| Published | 2024-09-25 |
| First published | 2023-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | TYF |
| Maintainers | tyfcxl |
| Keywords | typescript, enum, options, transform |

## Links

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

## Dependencies (5)

- [ejs](https://npm.io/package/ejs.md) ^3.1.8
- [core-js](https://npm.io/package/core-js.md) ^3.33.3
- [typescript](https://npm.io/package/typescript.md) ^4.9.4
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.20.13
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.20.13

## 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

- 1.4.2 (latest) — 2024-09-25
- 1.3.1-beta.0 (beta) — 2023-11-21
- 1.4.1 — 2024-07-12
- 1.4.0 — 2024-07-12
- 1.3.3 — 2023-12-18
- 1.3.2 — 2023-11-22
- 1.3.1 — 2023-11-22
- 1.3.0 — 2023-11-21
- 1.2.2 — 2023-11-17
- 1.2.1 — 2023-06-16
- 1.2.0 — 2023-06-16
- 1.1.0 — 2023-06-15
- 1.0.7 — 2023-02-21
- 1.0.6 — 2023-02-07
- 1.0.5 — 2023-02-07
- … 5 more at https://npm.io/package/enum-opt/versions

## README

English | [简体中文](./README-zh_CN.md)

# enum-opt

typescript enum Convert to an option data structure

## Install

### npm

```
npm install enum-opt --save
```

## Using

```bash
npx enum-opt yourfile.ts
```

Or add script in package.json script

```json
"scripts": {
  "enum-opt": "enum-opt"
}
```

```bash
npm run enum-opt
```

## Commands

| Command | Description           | Default |
| ------- | --------------------- | ------- |
| -o      | Output file directory | .       |

## Config

Add enumoptconfig.json or.enumoptrc.js to the root directory

enumoptconfig.json

```json
{
  "entry": "enum.ts"
}
```

 .enumoptrc.js

```js
module.exports = {
  entry: "enum.ts"
}
```

| Property         | Description                                                         | Type       | Default |
| ------------ | ------------------------------------------------------------ | -------------- | ------- |
| entry        | Enumeration file path to be parsed, multiple files can be configured using objects | string\|object | -       |
| outDir       | Output file directory                          | string         | ./      |
| fileSuffix   | The output file name is the entry file name + the file name suffix (invalid if the entry type is object). | string         | -opt    |
| optionSuffix | Output option variable suffix                | string         | Options |
| exclude      | Excluded enumeration, excluded options will not be generated | string         | -       |

## Example

### single file

enum.ts

```typescript
/**
 * action type
 */
export enum ActionType {
  // add
  Add,
  // edit
  Edit,
  // detail
  Detail,
  // perfect
  Perfect,
}
```

```bash
npx enum-opt enum.ts
```

generate enum-opt.ts

```typescript
// @ts-ignore
import { ActionType } from 'enum'
/**
* action type
*/
export const actionTypeOptions = [
  {
    label: 'add',
    value: ActionType.Add,
  },
  {
    label: 'edit',
    value: ActionType.Edit,
  },
  {
    label: 'detail',
    value: ActionType.Detail,
  },
  {
    label: 'perfect',
    value: ActionType.Perfect,
  },
]
```

### multiple file

enumoptconfig.json

```json
{
  "entry": {
    "enum-options": "enum.ts",
    "actions-options": "action-enum.ts"
  }
}
```

enum-options.ts and actions-options.ts will be printed

## Formatting

```typescript
import { valueToLabel, labelToValue } from 'enum-opt'
import { actionTypeOptions } from './util/enum-opt'
import { ActionType } from './enum'

valueToLabel(ActionType.Add, actionTypeOptions) // add

labelToValue('add', actionTypeOptions) // 1
```

## notice

- Enumeration comments should be above the enumeration value, otherwise the build may fail

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