Skip to content

Commit

Permalink
Add unicode builder #69
Browse files Browse the repository at this point in the history
  • Loading branch information
qrac committed Aug 27, 2023
1 parent bcd3699 commit 27f03b3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/unicode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "unicode",
"private": true,
"type": "module",
"scripts": {
"build": "npm run clean && npm run build:src",
"build:src": "tsx ./src/build.ts",
"clean": "rimraf ./dist"
}
}
29 changes: 29 additions & 0 deletions packages/unicode/src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from "node:path"
import fs from "fs-extra"

import { fonts } from "yakuhanjp/project.json" assert { type: "json" }
import { glyphs2unicodes } from "."

async function main() {
const cwd = process.cwd()
const outPath = path.join(cwd, "dist", "data.json")
const data = fonts.map((item) => {
return {
name: item.name,
glyphs: {
str: item.glyphs,
unicode: glyphs2unicodes(item.glyphs),
},
}
})
await fs
.outputJSON(outPath, data, { spaces: 2 })
.then(() => {
console.log(outPath)
})
.catch((err) => {
console.error(err)
})
}

await main()
25 changes: 25 additions & 0 deletions packages/unicode/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest"

import { glyph2unicode, glyphs2unicodes } from "."

describe("glyph2unicode", () => {
it("A", () => {
const result = glyph2unicode("A")
expect(result).toEqual("U+0041")
})
it("、", () => {
const result = glyph2unicode("、")
expect(result).toEqual("U+3001")
})
})

describe("glyphs2unicodes", () => {
it("ABC", () => {
const result = glyphs2unicodes("ABC")
expect(result).toEqual("U+0041,U+0042,U+0043")
})
it("、。!?", () => {
const result = glyphs2unicodes("、。!?")
expect(result).toEqual("U+3001,U+3002,U+ff01,U+ff1f")
})
})
12 changes: 12 additions & 0 deletions packages/unicode/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function glyph2unicode(str: string) {
const hexStr = "000" + str.charCodeAt(0).toString(16)
return "U+" + hexStr.substring(hexStr.length - 4)
}

export function glyphs2unicodes(glyphs: string) {
const splitedGlyphs = glyphs.split("")
return splitedGlyphs
.map((item) => glyph2unicode(item))
.sort()
.join()
}
4 changes: 4 additions & 0 deletions packages/unicode/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src"]
}
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config"

export default defineConfig({
test: {
include: ["./packages/unicode/**/*.test.{ts,tsx}"],
exclude: ["**/node_modules/**", "**/dist/**", "./playground/**/*.*"],
testTimeout: 20000,
},
})

0 comments on commit 27f03b3

Please sign in to comment.