1.4.2 • Published 2 months ago

enum-opt v1.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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
1.4.2

2 months ago

1.4.1

4 months ago

1.4.0

4 months ago

1.3.3

11 months ago

1.3.1-beta.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.2.2

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.2.1

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago