From 324d6e338fecfbf9ad6b6e50c0a9f53e08d050c8 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 23 Feb 2024 18:28:44 +0800 Subject: [PATCH] update --- src/lib/utils.js | 23 +++++++++++++++++------ test/index.js | 6 ++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index e9892b2..f0e9a47 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -30,6 +30,20 @@ export function str2unicode(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) { let uArray = str2unicode(str) uArray = uArray.split(';') @@ -41,15 +55,12 @@ export function normalizeUnicode(str) { return toUnicode(str)[0] } -export function toLine(str) { - return str.replace(/([A-Z]{1})/g, (_, s) => '-' + s.toLowerCase()) -} - export function key2underline(obj) { let result = {} for (let k in obj) { - k = toLine(k) - result[k] = obj[k] + let v = obj[k] + k = hyphen(k) + result[k] = v } return result } diff --git a/test/index.js b/test/index.js index 05b0c45..58a1640 100644 --- a/test/index.js +++ b/test/index.js @@ -5,7 +5,7 @@ */ 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' // let svg = fs.cat('test/game.svg').toString() @@ -14,4 +14,6 @@ import fs from 'iofs' // console.log(ttf) -console.log(toUnicode('哈哈')) +console.log(toUnicode('哈哈'), normalizeUnicode('哈哈')) +console.log(toUnicode('哈'), normalizeUnicode(toUnicode('哈')[0])) +console.log(key2underline({ fooBar: '大', FooGoo: '圭' }))