master
yutent 2024-02-23 18:28:44 +08:00
parent 0cfeb517be
commit 324d6e338f
2 changed files with 21 additions and 8 deletions

View File

@ -30,6 +30,20 @@ export function str2unicode(str) {
return str return str
} }
//驼峰转换为连字符线风格
export function hyphen(target) {
return target.replace(/([a-z\d])([A-Z]+)/g, '$1-$2').toLowerCase()
}
//连字符转换为驼峰风格
export function camelize(target) {
//提前判断,提高效率
if (target.indexOf('-') < 0) {
return target
}
return target.replace(/\-([a-z])/g, (m, s) => s.toUpperCase())
}
export function toUnicode(str) { export function toUnicode(str) {
let uArray = str2unicode(str) let uArray = str2unicode(str)
uArray = uArray.split(';') uArray = uArray.split(';')
@ -41,15 +55,12 @@ export function normalizeUnicode(str) {
return toUnicode(str)[0] return toUnicode(str)[0]
} }
export function toLine(str) {
return str.replace(/([A-Z]{1})/g, (_, s) => '-' + s.toLowerCase())
}
export function key2underline(obj) { export function key2underline(obj) {
let result = {} let result = {}
for (let k in obj) { for (let k in obj) {
k = toLine(k) let v = obj[k]
result[k] = obj[k] k = hyphen(k)
result[k] = v
} }
return result return result
} }

View File

@ -5,7 +5,7 @@
*/ */
import { svg2ttf } from '../src/index.js' import { svg2ttf } from '../src/index.js'
import { toUnicode } from '../src/lib/utils.js' import { toUnicode, key2underline, normalizeUnicode } from '../src/lib/utils.js'
import fs from 'iofs' import fs from 'iofs'
// let svg = fs.cat('test/game.svg').toString() // let svg = fs.cat('test/game.svg').toString()
@ -14,4 +14,6 @@ import fs from 'iofs'
// console.log(ttf) // console.log(ttf)
console.log(toUnicode('哈哈')) console.log(toUnicode('哈哈'), normalizeUnicode('哈哈'))
console.log(toUnicode('哈'), normalizeUnicode(toUnicode('哈')[0]))
console.log(key2underline({ fooBar: '大', FooGoo: '圭' }))