Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enum-opt

Package Overview
Dependencies
Maintainers
0
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enum-opt

typescript enum to options

  • 1.4.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-84.21%
Maintainers
0
Weekly downloads
 
Created
Source

English | 简体中文

enum-opt

typescript enum Convert to an option data structure

Install

npm

npm install enum-opt --save

Using

npx enum-opt yourfile.ts

Or add script in package.json script

"scripts": {
  "enum-opt": "enum-opt"
}
npm run enum-opt

Commands

CommandDescriptionDefault
-oOutput file directory.

Config

Add enumoptconfig.json or.enumoptrc.js to the root directory

enumoptconfig.json

{
  "entry": "enum.ts"
}

.enumoptrc.js

module.exports = {
  entry: "enum.ts"
}
PropertyDescriptionTypeDefault
entryEnumeration file path to be parsed, multiple files can be configured using objectsstring|object-
outDirOutput file directorystring./
fileSuffixThe output file name is the entry file name + the file name suffix (invalid if the entry type is object).string-opt
optionSuffixOutput option variable suffixstringOptions
excludeExcluded enumeration, excluded options will not be generatedstring-

Example

single file

enum.ts

/**
 * action type
 */
export enum ActionType {
  // add
  Add,
  // edit
  Edit,
  // detail
  Detail,
  // perfect
  Perfect,
}
npx enum-opt enum.ts

generate enum-opt.ts

// @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

{
  "entry": {
    "enum-options": "enum.ts",
    "actions-options": "action-enum.ts"
  }
}

enum-options.ts and actions-options.ts will be printed

Formatting

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

Keywords

FAQs

Package last updated on 25 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc