Skip to content

Commit

Permalink
perf: cache lang checks
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Mar 13, 2019
1 parent 51b60d5 commit 5e3034e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@types/dompurify": "^0.0.31",
"@types/i18next": "^8.4.3",
"@types/lodash": "^4.14.98",
"@types/memoize-one": "^4.1.0",
"@types/memoize-one": "^4.1.1",
"@types/node": "^9.3.0",
"@types/qrcode.react": "^0.8.1",
"@types/react": "^16.3.14",
Expand All @@ -60,6 +60,7 @@
"i18next": "^11.2.2",
"lodash": "^4.17.4",
"md5": "^2.2.1",
"memoize-one": "^5.0.0",
"normalize-scss": "^7.0.1",
"qrcode.react": "^0.9.2",
"react": "^16.4.0",
Expand Down
34 changes: 18 additions & 16 deletions src/_helpers/lang-check.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import memoizeOne from 'memoize-one'

export const testerChinese = /[\u4e00-\u9fa5]/
export const testerEnglish = /[a-zA-Z]/

Expand All @@ -21,40 +23,40 @@ export const testerMinor = /[^\u4e00-\u9fa5a-zA-Z0-9\s\u200b\/\[\]\{\}\$\^\*\+\|

export const testerPunct = /[\/\[\]\{\}\$\^\*\+\|\?\.\-~!@#%&()_='";:><,·_]/

export function isContainChinese (text: string): boolean {
export const isContainChinese = memoizeOne((text: string): boolean => {
return testerChinese.test(text)
}
})

export function isContainEnglish (text: string): boolean {
export const isContainEnglish = memoizeOne((text: string): boolean => {
return testerEnglish.test(text)
}
})

/** Hiragana & Katakana, no Chinese */
export function isContainJapanese (text: string): boolean {
export const isContainJapanese = memoizeOne((text: string): boolean => {
return testJapanese.test(text)
}
})

/** Korean Hangul, no Chinese */
export function isContainKorean (text: string): boolean {
export const isContainKorean = memoizeOne((text: string): boolean => {
return testKorean.test(text)
}
})

/** French, no English àâäèéêëîïôœùûüÿç */
export function isContainFrench (text: string): boolean {
export const isContainFrench = memoizeOne((text: string): boolean => {
return testFrench.test(text)
}
})

/** Deutsch, no English äöüÄÖÜß */
export function isContainDeutsch (text: string): boolean {
export const isContainDeutsch = memoizeOne((text: string): boolean => {
return testDeutsch.test(text)
}
})

/** Spanish, no English áéíóúñü¡¿ */
export function isContainSpanish (text: string): boolean {
export const isContainSpanish = memoizeOne((text: string): boolean => {
return testSpanish.test(text)
}
})

/** Languages excpet Chinese and English */
export function isContainMinor (text: string): boolean {
export const isContainMinor = memoizeOne((text: string): boolean => {
return testerMinor.test(text)
}
})
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@
version "4.14.109"
resolved "http://registry.npm.taobao.org/@types/lodash/download/@types/lodash-4.14.109.tgz#b1c4442239730bf35cabaf493c772b18c045886d"

"@types/memoize-one@^4.1.0":
version "4.1.0"
resolved "http://registry.npm.taobao.org/@types/memoize-one/download/@types/memoize-one-4.1.0.tgz#62119f26055b3193ae43ca1882c5b29b88b71ece"
integrity sha1-YhGfJgVbMZOuQ8oYgsWym4i3Hs4=
"@types/memoize-one@^4.1.1":
version "4.1.1"
resolved "http://registry.npm.taobao.org/@types/memoize-one/download/@types/memoize-one-4.1.1.tgz#41dd138a4335b5041f7d8fc038f9d593d88b3369"
integrity sha1-Qd0TikM1tQQffY/AOPnVk9iLM2k=

"@types/node@*":
version "10.1.3"
Expand Down Expand Up @@ -5928,6 +5928,11 @@ mem@^1.1.0:
dependencies:
mimic-fn "^1.0.0"

memoize-one@^5.0.0:
version "5.0.0"
resolved "http://registry.npm.taobao.org/memoize-one/download/memoize-one-5.0.0.tgz#d55007dffefb8de7546659a1722a5d42e128286e"
integrity sha1-1VAH3/77jedUZlmhcipdQuEoKG4=

memory-fs@^0.4.0, memory-fs@~0.4.1:
version "0.4.1"
resolved "http://registry.npm.taobao.org/memory-fs/download/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"
Expand Down

0 comments on commit 5e3034e

Please sign in to comment.