8000 revert: UTF-8 support breaks some languages, revert PR #120 by ryougifujino · Pull Request #141 · antfu/case-police · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

revert: UTF-8 support breaks some languages, revert PR #120 #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export const DISABLE_KEY = '@case-police-disable'

export const IGNORE_REGEX = /@case-police-ignore\s+([^\s]+)/g

export const UTF8_RANGE = '[\u0080-\uFFFF]'

export function buildRegex(dictionary: Record<string, string>): RegExp {
const keys = Object.keys(dictionary)
const regex = new RegExp(`\\b(${keys.join('|')})\\b`, 'gi')
Expand Down Expand Up @@ -40,9 +38,6 @@ export async function replace(
regex = regex || buildRegex(dict)
let changed = false
code = code.replace(regex, (_, key: string, index: number) => {
if (containsUTF8(code, key, index))
return _

if (!key.match(/[A-Z]/) || !key.match(/[a-z]/))
return _
const lower = key.toLowerCase()
Expand Down Expand Up @@ -88,10 +83,3 @@ export async function loadAllPresets() {
...await Promise.all(files.map(file => resolvePreset(file.split('.')[0]))),
)
}

function containsUTF8(code: string, key: string, index: number) {
const utf8Regex = new RegExp(`${UTF8_RANGE}`)
const head = code.charAt(index - 1)
const tail = code.charAt(index + key.length)
return utf8Regex.test(head) || utf8Regex.test(tail)
}
4 changes: 0 additions & 4 deletions test/dict/utf8.json

This file was deleted.

31 changes: 2 additions & 29 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'path'
import { existsSync, promises as fs } from 'fs'
import { describe, expect, it } from 'vitest'
import { replace, resolvePreset } from '../src/utils'

Expand All @@ -12,6 +10,7 @@ describe('should', () => {
Github GitHub github GITHUB
vscode VScode VS Code VSCODE vs code VS code
nextjs Nextjs NextJS Next.js
GithubのVScode Github:
`,
'',
)
Expand All @@ -21,6 +20,7 @@ nextjs Nextjs NextJS Next.js
GitHub GitHub github GITHUB
vscode VS Code VS Code VSCODE vs code VS Code
nextjs Next.js Next.js Next.js
GitHubのVS Code GitHub:
"
`)
})
Expand All @@ -43,30 +43,3 @@ describe('presets', () => {
`)
})
})

describe('utf8', async () => {
let preset = {}
const presetFilePath = path.join(__dirname, './dict/utf8.json')
if (existsSync(presetFilePath)) {
const content = await fs.readFile(presetFilePath, 'utf-8')
preset = { ...JSON.parse(content) }
}

it('works', async () => {
const replaced = await replace(
`
Romania romania
Domâ Dom âDom ĕDomĕ
`,
'',
preset,
)

expect(replaced).toMatchInlineSnapshot(`
"
România romania
Domâ DOM âDom ĕDomĕ
"
`)
})
})
0