Skip to content

Commit f7cb548

Browse files
fix: workaround for bun mangling unicode (#104)
<!-- 👋 Hi, thanks for sending a PR to dedent! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #101 - [x] That issue was marked as [`status: accepting prs`](https://github.com/dmnd/dedent/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/dmnd/dedent/blob/main/.github/CONTRIBUTING.md) were taken ## Overview - Adds a workaround to restore unicode characters mangled by Bun with a string replacement. This replacement runs only in a Bun environment. If the issue gets fixed in Bun, then a test for the version of `process.versions.bun` could perhaps be added later. - Adds types for Bun, needed for that environment check to lint. - Adds `test:bun` as a script, so that `pnpm run test:bun` will test decent.test.js with Bun's test runner instead of Jest, as long as Bun is installed. Includes a note in DEVELOPING.md. - Adds a test which failed before the change with Bun (but passed with Node/Jest). --------- Co-authored-by: Josh Goldberg <[email protected]>
1 parent ae92d21 commit f7cb548

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

.github/DEVELOPMENT.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ pnpm run test --coverage
7878
Note that [console-fail-test](https://github.com/JoshuaKGoldberg/console-fail-test) is enabled for all test runs.
7979
Calls to `console.log`, `console.warn`, and other console methods will cause a test to fail.
8080

81+
### Testing with Bun
82+
83+
If you have [Bun](https://bun.com/docs/test) installed on your system, tests can be run with Bun's test runner rather than Node and Jest.
84+
85+
```shell
86+
pnpm run test:bun
87+
```
88+
8189
## Type Checking
8290

8391
You should be able to see suggestions from [TypeScript](https://typescriptlang.org) in your editor for all open files.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"prepare": "husky install",
5959
"should-semantic-release": "should-semantic-release --verbose",
6060
"test": "jest",
61+
"test:bun": "bun test src/dedent.test.ts",
6162
"tsc": "tsc"
6263
},
6364
"lint-staged": {
@@ -69,6 +70,7 @@
6970
"@babel/preset-typescript": "^7.23.3",
7071
"@release-it/conventional-changelog": "^8.0.1",
7172
"@types/babel-plugin-macros": "^3.1.0",
73+
"@types/bun": "^1.3.4",
7274
"@types/eslint": "^8.44.7",
7375
"@types/jest": "^29.5.3",
7476
"@typescript-eslint/eslint-plugin": "^6.10.0",

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dedent.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,14 @@ describe("dedent", () => {
295295
});
296296
},
297297
);
298+
299+
describe("Unicode character preservation", () => {
300+
it("preserves emojis", () => {
301+
expect(dedent`😊`).toBe("😊");
302+
});
303+
304+
it("preserves ideographs", () => {
305+
expect(dedent`弟気`).toBe("弟気");
306+
});
307+
});
298308
});

src/dedent.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ function createDedent(options: DedentOptions) {
8484
result = result.replace(/\\n/g, "\n");
8585
}
8686

87+
// Workaround for Bun issue with Unicode characters
88+
// https://github.com/oven-sh/bun/issues/8745
89+
if (typeof Bun !== "undefined") {
90+
result = result.replace(
91+
// Matches e.g. \\u{1f60a} or \\u5F1F
92+
/\\u(?:\{([\da-fA-F]{1,6})\}|([\da-fA-F]{4}))/g,
93+
(_, braced?: string, unbraced?: string) => {
94+
const hex = braced ?? unbraced ?? "";
95+
return String.fromCodePoint(parseInt(hex, 16));
96+
},
97+
);
98+
}
99+
87100
return result;
88101
}
89102
}

0 commit comments

Comments
 (0)