Skip to content

Commit

Permalink
feat(utils): 新增 characterToCodepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 29, 2023
1 parent d1e1ba5 commit f91d0db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/utils/characterToCodepoint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { characterToCodepoint } from './characterToCodepoint'

describe('characterToCodepoint', () => {
test('ok', () => {
expect(characterToCodepoint('㊗️')).toBe('3297-fe0f')
expect(characterToCodepoint('💡')).toBe('1f4a1')
})
})
12 changes: 12 additions & 0 deletions src/utils/characterToCodepoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { padStart } from 'lodash-uni'

/**
* UTF8 字符转为 Unicode 码点。
*
* @param character 字符,支持多个字符,返回的码点将以 `-` 分割
*/
export function characterToCodepoint(character: string): string {
return [...character]
.map(char => padStart(char.codePointAt(0)!.toString(16), 4, '0'))
.join('-')
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './base64'
export * from './bindEvent'
export * from './Calculator'
export * from './cartesianProduct'
export * from './characterToCodepoint'
export * from './chooseFile'
export * from './chunkEqual'
export * from './cloneDeepFast'
Expand Down

0 comments on commit f91d0db

Please sign in to comment.