From 78595b414166bcbf38680ca65bdf5cc0899e9fb7 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 6 Nov 2024 20:48:51 -0800 Subject: [PATCH] Replace punycode dependency with string iteration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The spread operator has been available since Node 5.0.0, and iterates strings over their code points (not code units), as expected here. There’s no need to use the punycode library for this. Signed-off-by: Anders Kaseorg --- package-lock.json | 2 +- package.json | 1 - src/index.ts | 9 +++++---- src/tests/index.test.ts | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index afd9d05..59d994e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,6 @@ "commander": "^12.1.0", "debug": "^4.3.6", "glob": "^11.0.0", - "punycode": "^2.3.1", "sax": "^1.4.1", "svg-pathdata": "^7.0.0", "transformation-matrix-js": "^2.7.6", @@ -7149,6 +7148,7 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "dev": true, "license": "MIT", "engines": { "node": ">=6" diff --git a/package.json b/package.json index e9ef000..807ac7e 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,6 @@ "commander": "^12.1.0", "debug": "^4.3.6", "glob": "^11.0.0", - "punycode": "^2.3.1", "sax": "^1.4.1", "svg-pathdata": "^7.0.0", "transformation-matrix-js": "^2.7.6", diff --git a/src/index.ts b/src/index.ts index 4b4e34d..952a378 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -import punycode from 'punycode/punycode.js'; import { Transform } from 'stream'; import Sax from 'sax'; import { SVGPathData } from 'svg-pathdata'; @@ -562,9 +561,11 @@ export class SVGIcons2SVGFontStream extends Transform { delete glyph.paths; const d = glyphPath.round(this._options.round).encode(); glyph.unicode.forEach((unicode, i) => { - const unicodeStr = punycode.ucs2 - .decode(unicode) - .map((point) => '&#x' + point.toString(16).toUpperCase() + ';') + const unicodeStr = [...unicode] + .map( + (char) => + '&#x' + char.codePointAt(0)!.toString(16).toUpperCase() + ';', + ) .join(''); this.push( diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index ab243cd..31e110b 100755 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -4,7 +4,6 @@ import { Readable } from 'node:stream'; import fs from 'node:fs'; import { mkdir } from 'node:fs/promises'; import { join } from 'node:path'; -import punycode from 'punycode/punycode.js'; import { SVGIcons2SVGFontStream } from '../index.js'; import { SVGIconsDirStream, type SVGIconStream } from '../iconsdir.js'; @@ -572,7 +571,7 @@ describe('Passing code points', () => { svgIconStream.metadata = { name: 'account', - unicode: [punycode.ucs2.encode([0x1f63a])], + unicode: ['\u{1f63a}'], }; const promise = bufferStream(svgFontStream);