Skip to content

Commit

Permalink
releases v2.1.2
Browse files Browse the repository at this point in the history
feat

- support filter options from type ([view instructions for use](./test/mult.md))
- export utils functions

fix

- miss type boolean
- partial separator not replaced
  • Loading branch information
tolking authored Sep 5, 2022
2 parents ad08ae0 + b581adb commit 9e24c79
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 21 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

### 2.1.2

feat

- support filter options from type ([view instructions for use](./test/mult.md))
- export utils functions

fix

- miss type boolean
- partial separator not replaced

### 2.1.1

feat
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ then in package.json

<details>
<summary>TOC</summary>

- [entry (required)](#entry)
- [outDir (required)](#outdir)
- [name (required)](#name)
Expand Down Expand Up @@ -80,6 +81,7 @@ then in package.json
- [titleRegExp](#titleregexp)
- [tableRegExp](#tableregexp)
- [fileNameRegExp](#filenameregexp)

</details>

### entry
Expand Down Expand Up @@ -187,14 +189,14 @@ name for tags of the vetur
- Type: `string`
- Default: `attributes.json`

name for attributes of the vetur
name for attributes of the Vetur

### webTypes

- Type: `string`
- Default: `web-types.json`

name for web-types of the webstrom
name for web-types of the WebStorm

### props

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "components-helper",
"version": "2.1.1",
"version": "2.1.2",
"description": "Based on the docs to provide code prompt files for vue component library",
"main": "lib/index.js",
"module": "lib/index.es.js",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from './normalize'
export * from './vetur'
export * from './webTypes'
export * from './write'
export * from './utils'
export * from './type'
export default main
49 changes: 42 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export function isCommonType(type: string): boolean {
return regExp.test(getTypeSymbol(type))
}

export function isEnumType(type: string): boolean {
return /^'\w*'$/.test(type) || /^"\w*"$/.test(type)
}

export function isUnionType(type: string): boolean {
return /(\||&)/.test(type)
}

export function arrayToRegExp(arr: string[], flags?: string): RegExp {
return new RegExp(`^(${arr.join('|')})$`, flags)
}
Expand All @@ -81,7 +89,7 @@ export function normalizeFile(file: string): string {
}

export function splitString(
options: Options,
options: Pick<Options, 'separator'>,
str?: string,
): string[] | undefined {
if (!str) return undefined
Expand All @@ -91,7 +99,7 @@ export function splitString(
}

export function getComponentsName(
options: Options,
options: Pick<Options, 'reComponentName'>,
title = '',
fileName: string,
path: string,
Expand All @@ -103,7 +111,7 @@ export function getComponentsName(
}

export function getDocUrl(
options: Options,
options: Pick<Options, 'reDocUrl'>,
fileName: string,
header?: string,
path?: string,
Expand All @@ -113,7 +121,7 @@ export function getDocUrl(
}

export function getVeturDescription(
options: Options,
options: Pick<Options, 'reVeturDescription'>,
description?: string,
defaultVal?: string,
docUrl?: string,
Expand All @@ -137,7 +145,7 @@ export function getVeturDescription(
}

export function getWebTypesSource(
options: Options,
options: Pick<Options, 'reWebTypesSource'>,
title = '',
fileName: string,
path: string,
Expand All @@ -149,14 +157,18 @@ export function getWebTypesSource(
}

function getType(type: string): string | BaseContribution {
const isEnum = isEnumType(type)
const isPublicType = isCommonType(type)
const symbol = getTypeSymbol(type)
const isUnion = isUnionType(symbol)

return isPublicType || !symbol ? type : { name: type, source: { symbol } }
return isEnum || isPublicType || !symbol || isUnion
? type
: { name: type, source: { symbol } }
}

export function getWebTypesType(
options: Options,
options: Pick<Options, 'reWebTypesType' | 'separator'>,
type?: string,
): undefined | Array<string | BaseContribution> {
const { reWebTypesType } = options
Expand All @@ -179,3 +191,26 @@ export function getWebTypesType(
return undefined
}
}

export function filterVeturOptions(
options: Pick<Options, 'separator'>,
type?: string,
) {
const list = splitString(options, type)

if (list?.length) {
const result = []

for (let i = 0; i < list.length; i++) {
const item = list[i]
const isEnum = isEnumType(item)
const len = item.length

isEnum && result.push(item.slice(1, len - 1))
}

return result
} else {
return undefined
}
}
9 changes: 4 additions & 5 deletions src/vetur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getVeturDescription,
checkArray,
splitString,
filterVeturOptions,
} from './utils'
import type { Options, NormalizeData, Tags, Props } from './type'

Expand Down Expand Up @@ -62,10 +63,8 @@ export function vetur(
const _name = name + '/' + _item
const _type = (item[propsType] || '').replaceAll(separator, '|')
const _options = item[propsOptions]
const _optionsList =
/string/i.test(_type) && _options
? splitString(options, _options)
: undefined
const _optionsList = splitString(options, _options)
const _typeOptionsList = filterVeturOptions({ separator: '|' }, _type)
const _description = getVeturDescription(
options,
item[propsDescription],
Expand All @@ -76,7 +75,7 @@ export function vetur(
tagsProps.push(_item)
propsList[_name] = {
type: _type || undefined,
options: _optionsList,
options: checkArray(_optionsList || _typeOptionsList),
description: _description,
}
}
Expand Down
26 changes: 20 additions & 6 deletions test/mult.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ This is a description of the Third component

## First Props

| Name | Description | Type | Options | Default |
|----- |------------ |----- |-------- | ------- |
| v-model | bind value | string / number || top |
| Name | Description | Type | Default |
|------|-------------|------|---------|
| size | size of component | 'default' / 'small' / 'large' | default |
| type | type of component | 'primary' \| 'success' \| 'warning' \| 'danger' ||

<!--
Supports the use of separator `/` to split the type, and is compatible with the `\|` split type
To generate better type hints, the `\|` cannot be used with reference types, eg `CommonType \| 'primary' \| 'success'`
-->

## First Slots

Expand All @@ -28,9 +35,16 @@ This is a description of the Third component

## Second Props

| Name | Description | Type | Options | Default |
|----- |------------ |----- |-------- | ------- |
| v-model | bind value | string / number |||
| Name | Description | Type | Default |
|------|------------ |------------| ------- |
| size | size of component | CommonType ||

<!--
Make sure the component library exports the type `CommonType`
eg `export type CommonType = 'default' | 'small' | 'large'`
This way of writing is only valid for the web-types
-->

## Third Events

Expand Down

0 comments on commit 9e24c79

Please sign in to comment.