Skip to content

Commit

Permalink
package type: module
Browse files Browse the repository at this point in the history
  • Loading branch information
luncheon committed Apr 22, 2023
1 parent 3018066 commit 6a935e6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 241 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/node_modules/
/index.js
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// https://knowledge.autodesk.com/ja/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2019/JPN/AutoCAD-Core/files/GUID-968CBC1D-BA99-4519-ABDD-88419EB2BF92-htm.html
// https://knowledge.autodesk.com/ja/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2019/JPN/AutoCAD-Core/files/GUID-518E1A9D-398C-4A8A-AC32-2D85590CDBE1-htm.html
const dxfTextControlCodeSymbolMap = {
d: '°',
c: '⌀',
p: '±',
};
export const decodeDxfTextCharacterCodes = (text, mbcsEncoding) => {
text = decodeDxfTextUnicodeCodePoints(text);
return mbcsEncoding ? decodeDxfTextMbcsCharacterCodes(text, mbcsEncoding) : text;
};
export const decodeDxfTextUnicodeCodePoints = (text) => text.replace(/\\[uU]\+([0-9a-fA-F]{4})/g, (_, codePoint) => String.fromCodePoint(parseInt(codePoint, 16)));
export const decodeDxfTextMbcsCharacterCodes = (text, encoding) => {
let decoder = encoding instanceof TextDecoder ? encoding : undefined;
return text.replace(/\\[mM]\+1([0-9a-fA-F]{2})([0-9a-fA-F]{2})/g, (_, a, b) => (decoder = decoder || new TextDecoder(encoding)).decode(new Uint8Array([parseInt(a, 16), parseInt(b, 16)])));
};
export const parseDxfTextContent = (text, options) => {
text = decodeDxfTextCharacterCodes(text, options?.encoding);
let previousIndex = 0;
let currentContent = { text: '' };
const contents = [currentContent];
for (const match of text.matchAll(/%%(\d\d\d|.)/g)) {
currentContent.text += text.slice(previousIndex, match.index);
const c = match[1];
const code = c.toLowerCase();
const symbol = dxfTextControlCodeSymbolMap[code];
if (symbol) {
currentContent.text += symbol;
}
else if (code.length === 3) {
currentContent.text += String.fromCodePoint(+code);
}
else if (code === 'k' || code === 'o' || code === 'u') {
currentContent = { ...currentContent, text: '' };
if (currentContent[code]) {
delete currentContent[code];
}
else {
currentContent[code] = 1;
}
contents.push(currentContent);
}
else {
currentContent.text += code;
}
previousIndex = match.index + match[0].length;
}
currentContent.text += text.slice(previousIndex);
return contents.filter(content => content.text);
};
11 changes: 0 additions & 11 deletions index.mjs

This file was deleted.

231 changes: 8 additions & 223 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"keywords": [
"dxf"
],
"main": "index.mjs",
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"build": "tsc -p . && terser -c -m --toplevel -b beautify=false,semicolons=false --warn -o index.mjs index.js"
"build": "tsc -p ."
},
"devDependencies": {
"terser": "^5.14.2",
"typescript": "^4.7.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2020",
"module": "es2020",
"target": "es2022",
"module": "es2022",
"moduleResolution": "node",
"declaration": true,
"strict": true,
Expand Down

0 comments on commit 6a935e6

Please sign in to comment.