Skip to content

Commit

Permalink
refactor: rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjiang committed Dec 25, 2023
1 parent 5bbd69c commit 3aa2904
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/rules/case-abbrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const generateHandler = (options: Options): Handler => {
const tokenAfter = findTokenAfter(group, token)
if (
tokenAfter &&
tokenAfter.type === CharType.LETTERS_HALF &&
tokenAfter.type === CharType.WESTERN_LETTER &&
!token.spaceAfter
) {
return
Expand Down
2 changes: 1 addition & 1 deletion src/rules/case-html-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const generateHandler = (options: Options): Handler => {
const tokenAfter = findTokenAfter(group, token)
if (
!tokenAfter ||
tokenAfter.type !== CharType.LETTERS_HALF ||
tokenAfter.type !== CharType.WESTERN_LETTER ||
token.spaceAfter
) {
return
Expand Down
4 changes: 2 additions & 2 deletions src/rules/case-zh-units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ const generateHandler = (options: Options): Handler => {
const skippedZhUnits = options?.skipZhUnits || ''
const matcherStr = skippedZhUnits
.split('')
.filter((x) => checkCharType(x) === CharType.LETTERS_FULL)
.filter((x) => checkCharType(x) === CharType.CJK_CHAR)
.join('')
const unitMatcher = new RegExp(`^[${matcherStr}]`)

return (token: MutableToken, _: number, group: MutableGroupToken) => {
// make sure the content is a number
if (token.type === CharType.LETTERS_HALF && token.content.match(/^\d+$/)) {
if (token.type === CharType.WESTERN_LETTER && token.content.match(/^\d+$/)) {
// make sure the content after is a Chinese unit
const tokenAfter = findNonCodeVisibleTokenAfter(group, token)
if (tokenAfter && tokenAfter.content.match(unitMatcher)) {
Expand Down
10 changes: 6 additions & 4 deletions src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import genAbbrsHandler from './case-abbrs'

import genSpaceOfWrapperHandler from './space-wrapper'
import genSpaceOfCodeHandler from './space-code'
import genSpaceOfLettersHandler from './space-letters'
import genSpaceOfLetterHandler from './space-letter'
import genSpaceOfPunctuationHandler from './space-punctuation'
import genSpaceOfQuoteHandler from './space-quote'
import genSpaceOfQuoteHandler from './space-quotation'
import genSpaceOfBracketHandler from './space-bracket'

import genLinebreakHandler from './case-linebreak'
Expand All @@ -30,7 +30,7 @@ const generateHandlers = (options: Options): Handler[] => {

genSpaceOfWrapperHandler(options),
genSpaceOfCodeHandler(options),
genSpaceOfLettersHandler(options),
genSpaceOfLetterHandler(options),
genSpaceOfPunctuationHandler(options),
genSpaceOfQuoteHandler(options),
genSpaceOfBracketHandler(options),
Expand All @@ -41,9 +41,11 @@ const generateHandlers = (options: Options): Handler[] => {
]
}

// TODO: new default options

export const defaultConfig: Options = {
noSinglePair: true,
halfWidthPunctuation: `()`,
halfWidthPunctuation: `()[]{}`,
fullWidthPunctuation: `,。:;?!“”‘’`,
adjustedFullWidthPunctuation: `“”‘’`,
unifiedPunctuation: 'simplified',
Expand Down
2 changes: 2 additions & 0 deletions src/rules/punctuation-unification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from './messages'
import { checkEndContent, checkStartContent, Options } from './util'

// TODO: 【】〖〗, primary/secondary, quotes -> quotations

type UnifiedOptions = 'traditional' | 'simplified'

enum QuoteType {
Expand Down
118 changes: 61 additions & 57 deletions src/rules/punctuation-width.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* This rule will format each punctuation into the right width options.
*
* Options:
* - halfWidthPunctuation: string = `()`
* - fullWidthPunctuation: string = `,。:;?!“”‘’`
* - adjustedFullWidthPunctuation: string = `“”‘’`
* - halfwidthPunctuation: string = `()`
* - fullwidthPunctuation: string = `,。:;?!“”‘’`
* - adjustedFullwidthPunctuation: string = `“”‘’`
*
* Details:
* - skip half-width punctuations between half-width content without space
Expand All @@ -27,36 +27,40 @@ import {
checkContent,
checkEndContent,
checkStartContent,
isHalfWidthPunctuationWithoutSpaceAround,
isSuccessiveHalfWidthPunctuation,
isHalfwidthPunctuationWithoutSpaceAround,
isSuccessiveHalfwidthPunctuation,
Options
} from './util'

type WidthPairList = Array<[halfWidth: string, fullWidth: string]>
type WidthPairList = Array<[halfwidth: string, fullwidth: string]>
type WidthSidePairList = Array<
[halfWidth: string, fullWidthLeftSide: string, fullWidthRightSide: string]
[halfwidth: string, fullwidthLeftSide: string, fullwidthRightSide: string]
>
type AlterMap = Record<string, string>
type AlterPairMap = Record<string, [leftSide: string, rightSide: string]>

const widthPairList: WidthPairList = [
[`(`, `(`],
[`)`, `)`],
[`,`, `,`],
[`.`, `。`],
[`;`, `;`],
[`:`, `:`],
[`?`, `?`],
[`!`, `!`]
[`!`, `!`],
[`(`, `(`],
[`)`, `)`],
[`[`, `[`],
[`]`, `]`],
[`{`, `{`],
[`}`, `}`],
]
const widthSidePairList: WidthSidePairList = [
[`"`, `“`, `”`],
[`'`, `‘`, `’`]
]

const defaultHalfWidthOption = `()`
const defaultFullWidthOption = `,。:;?!“”‘’`
const defaultAdjustedFullWidthOption = `“”‘’`
const defaultHalfwidthOption = `()[]{}`
const defaultFullwidthOption = `,。:;?!“”‘’`
const defaultAdjustedFullwidthOption = `“”‘’`

const checkAdjusted = (token: MutableToken, adjusted: string): void => {
if (adjusted.indexOf(token.modifiedContent) >= 0) {
Expand All @@ -67,57 +71,57 @@ const checkAdjusted = (token: MutableToken, adjusted: string): void => {
const parseOptions = (
options: Options
): {
halfWidthMap: AlterMap
fullWidthMap: AlterMap
fullWidthPairMap: AlterPairMap
halfwidthMap: AlterMap
fullwidthMap: AlterMap
fullwidthPairMap: AlterPairMap
adjusted: string
} => {
const halfWidthOption = options?.halfWidthPunctuation || ''
const fullWidthOption = options?.fullWidthPunctuation || ''
const adjustedFullWidthOption = options?.adjustedFullWidthPunctuation || ''
const halfwidthOption = options?.halfwidthPunctuation || ''
const fullwidthOption = options?.fullwidthPunctuation || ''
const adjustedFullwidthOption = options?.adjustedFullwidthPunctuation || ''

const halfWidthMap: AlterMap = {}
const fullWidthMap: AlterMap = {}
const fullWidthPairMap: AlterPairMap = {}
const halfwidthMap: AlterMap = {}
const fullwidthMap: AlterMap = {}
const fullwidthPairMap: AlterPairMap = {}

widthPairList.forEach(([halfWidth, fullWidth]) => {
if (halfWidthOption.indexOf(halfWidth) >= 0) {
halfWidthMap[fullWidth] = halfWidth
widthPairList.forEach(([halfwidth, fullwidth]) => {
if (halfwidthOption.indexOf(halfwidth) >= 0) {
halfwidthMap[fullwidth] = halfwidth
}
if (fullWidthOption.indexOf(fullWidth) >= 0) {
fullWidthMap[halfWidth] = fullWidth
if (fullwidthOption.indexOf(fullwidth) >= 0) {
fullwidthMap[halfwidth] = fullwidth
}
})
widthSidePairList.forEach(([half, left, right]) => {
if (halfWidthOption.indexOf(half) >= 0) {
halfWidthMap[left] = half
halfWidthMap[right] = half
if (halfwidthOption.indexOf(half) >= 0) {
halfwidthMap[left] = half
halfwidthMap[right] = half
}
if (
fullWidthOption.indexOf(left) >= 0 ||
fullWidthOption.indexOf(right) >= 0
fullwidthOption.indexOf(left) >= 0 ||
fullwidthOption.indexOf(right) >= 0
) {
fullWidthPairMap[half] = [left, right]
fullwidthPairMap[half] = [left, right]
}
})
return {
halfWidthMap,
fullWidthMap,
fullWidthPairMap,
adjusted: adjustedFullWidthOption
halfwidthMap,
fullwidthMap,
fullwidthPairMap,
adjusted: adjustedFullwidthOption
}
}

const generateHandler = (options: Options): Handler => {
const { halfWidthMap, fullWidthMap, fullWidthPairMap, adjusted } =
const { halfwidthMap, fullwidthMap, fullwidthPairMap, adjusted } =
parseOptions(options)

const handleHyperSpaceOption: Handler = (
token: MutableToken,
_,
group: MutableGroupToken
) => {
// skip non-punctuation/quote/bracket situations
// skip non-punctuation/quotation/bracket situations
if (
!isPunctuationType(token.type) &&
token.type !== HyperTokenType.HYPER_WRAPPER_BRACKET &&
Expand All @@ -126,13 +130,13 @@ const generateHandler = (options: Options): Handler => {
return
}

// skip half-width punctuations between half-width content without space
if (isHalfWidthPunctuationWithoutSpaceAround(group, token)) {
// skip halfwidth punctuations between halfwidth content without space
if (isHalfwidthPunctuationWithoutSpaceAround(group, token)) {
return
}

// skip successive multiple half-width punctuations
if (isSuccessiveHalfWidthPunctuation(group, token)) {
if (isSuccessiveHalfwidthPunctuation(group, token)) {
return
}

Expand All @@ -143,18 +147,18 @@ const generateHandler = (options: Options): Handler => {
token.type === HyperTokenType.HYPER_WRAPPER_BRACKET
) {
const content = token.modifiedContent
if (fullWidthMap[content]) {
if (fullwidthMap[content]) {
checkContent(
token,
fullWidthMap[content],
fullwidthMap[content],
CharType.PUNCTUATION_FULL,

Check failure on line 154 in src/rules/punctuation-width.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Property 'PUNCTUATION_FULL' does not exist on type 'typeof CharType'.

Check failure on line 154 in src/rules/punctuation-width.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Property 'PUNCTUATION_FULL' does not exist on type 'typeof CharType'.
PUNCTUATION_FULL_WIDTH
)
checkAdjusted(token, adjusted)
} else if (halfWidthMap[content]) {
} else if (halfwidthMap[content]) {
checkContent(
token,
halfWidthMap[content],
halfwidthMap[content],
CharType.PUNCTUATION_HALF,

Check failure on line 162 in src/rules/punctuation-width.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Property 'PUNCTUATION_HALF' does not exist on type 'typeof CharType'.

Check failure on line 162 in src/rules/punctuation-width.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Property 'PUNCTUATION_HALF' does not exist on type 'typeof CharType'.
PUNCTUATION_HALF_WIDTH
)
Expand All @@ -165,29 +169,29 @@ const generateHandler = (options: Options): Handler => {
// 3. quotes in the alter pair map
const startContent = (token as MutableGroupToken).modifiedStartContent
const endContent = (token as MutableGroupToken).modifiedEndContent
if (fullWidthPairMap[startContent]) {
if (fullwidthPairMap[startContent]) {
checkStartContent(
token,
fullWidthPairMap[startContent][0],
fullwidthPairMap[startContent][0],
PUNCTUATION_FULL_WIDTH
)
} else if (halfWidthMap[startContent]) {
} else if (halfwidthMap[startContent]) {
checkStartContent(
token,
halfWidthMap[startContent][0],
halfwidthMap[startContent][0],
PUNCTUATION_HALF_WIDTH
)
}
if (fullWidthPairMap[endContent]) {
if (fullwidthPairMap[endContent]) {
checkEndContent(
token,
fullWidthPairMap[endContent][1],
fullwidthPairMap[endContent][1],
PUNCTUATION_FULL_WIDTH
)
} else if (halfWidthMap[endContent]) {
} else if (halfwidthMap[endContent]) {
checkEndContent(
token,
halfWidthMap[endContent][1],
halfwidthMap[endContent][1],
PUNCTUATION_HALF_WIDTH
)
}
Expand All @@ -196,9 +200,9 @@ const generateHandler = (options: Options): Handler => {
}

export const defaultConfig: Options = {
halfWidthPunctuation: defaultHalfWidthOption,
fullWidthPunctuation: defaultFullWidthOption,
adjustedFullWidthPunctuation: defaultAdjustedFullWidthOption
halfwidthPunctuation: defaultHalfwidthOption,
fullwidthPunctuation: defaultFullwidthOption,
adjustedFullwidthPunctuation: defaultAdjustedFullwidthOption
}

export default generateHandler
18 changes: 9 additions & 9 deletions src/rules/space-bracket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
CharType,
GroupTokenType,
Handler,
isLettersType,
isFullWidthPair,
isLetterType,
isFullwidthPair,
MarkSideType,
MutableGroupToken,
MutableSingleToken,
Expand All @@ -50,7 +50,7 @@ import {
} from './messages'

const isFullWidth = (char: string, adjusted: string): boolean => {
return isFullWidthPair(char) && adjusted.indexOf(char) === -1
return isFullwidthPair(char) && adjusted.indexOf(char) === -1
}

const shouldSkip = (
Expand All @@ -64,8 +64,8 @@ const shouldSkip = (
return false
}
if (
isFullWidthPair(token.content) ||
isFullWidthPair(token.modifiedContent)
isFullwidthPair(token.content) ||
isFullwidthPair(token.modifiedContent)
) {
return false
}
Expand All @@ -78,13 +78,13 @@ const shouldSkip = (
return (
// x(x
// ^
(before.type === CharType.LETTERS_HALF ||
(before.type === CharType.WESTERN_LETTER ||
// x()
// ^
(before.content === '(' && token.content === ')')) &&
// x)x
// ^
(after.type === CharType.LETTERS_HALF ||
(after.type === CharType.WESTERN_LETTER ||
// ()x
// ^
(token.content === '(' && after.content === ')'))
Expand Down Expand Up @@ -195,7 +195,7 @@ const generateHandler = (options: Options): Handler => {
if (token.markSide === MarkSideType.LEFT) {
if (
contentTokenBefore &&
(isLettersType(contentTokenBefore.type) ||
(isLetterType(contentTokenBefore.type) ||
contentTokenBefore.type === GroupTokenType.GROUP ||
contentTokenBefore.type === HyperTokenType.HYPER_CONTENT_CODE)
) {
Expand Down Expand Up @@ -227,7 +227,7 @@ const generateHandler = (options: Options): Handler => {
} else {
if (
contentTokenAfter &&
(isLettersType(contentTokenAfter.type) ||
(isLetterType(contentTokenAfter.type) ||
contentTokenAfter.type === GroupTokenType.GROUP ||
contentTokenAfter.type === HyperTokenType.HYPER_CONTENT_CODE)
) {
Expand Down
Loading

0 comments on commit 3aa2904

Please sign in to comment.