-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string to unicode formatter (#11)
* string to unicode * update readme * bump
- Loading branch information
1 parent
6c1b74c
commit 6c57e4c
Showing
6 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# deverything | ||
|
||
## 0.38.0 | ||
|
||
### Minor Changes | ||
|
||
- add string to unicode formatter | ||
|
||
## 0.37.0 | ||
|
||
### Minor Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./formatNumber"; | ||
export * from "./stringToUnicode"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import { stringToUnicode } from "./stringToUnicode"; | ||
|
||
describe("stringToUnicode", () => { | ||
test("converts basic ASCII characters correctly", () => { | ||
expect(stringToUnicode("Hello")).toBe( | ||
"\\u{48}\\u{65}\\u{6c}\\u{6c}\\u{6f}" | ||
); | ||
}); | ||
|
||
test("converts space character correctly", () => { | ||
expect(stringToUnicode(" ")).toBe("\\u{20}"); | ||
}); | ||
|
||
test("converts a string with special characters", () => { | ||
expect(stringToUnicode("!@#$%^&*()_+")).toBe( | ||
"\\u{21}\\u{40}\\u{23}\\u{24}\\u{25}\\u{5e}\\u{26}\\u{2a}\\u{28}\\u{29}\\u{5f}\\u{2b}" | ||
); | ||
}); | ||
|
||
test("converts non-ASCII characters (e.g., accented letters)", () => { | ||
expect(stringToUnicode("éåäö")).toBe("\\u{e9}\\u{e5}\\u{e4}\\u{f6}"); | ||
}); | ||
|
||
test("converts emojis correctly", () => { | ||
expect(stringToUnicode("😀😃😄")).toBe("\\u{1f600}\\u{1f603}\\u{1f604}"); | ||
}); | ||
|
||
test("handles an empty string", () => { | ||
expect(stringToUnicode("")).toBe(""); | ||
}); | ||
|
||
test("converts a string with mixed character types", () => { | ||
expect(stringToUnicode("Aa1!😀")).toBe( | ||
"\\u{41}\\u{61}\\u{31}\\u{21}\\u{1f600}" | ||
); | ||
}); | ||
|
||
test("converts characters outside the Basic Multilingual Plane", () => { | ||
expect(stringToUnicode("𠮷")).toBe("\\u{20bb7}"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const stringToUnicode = (text: string): string => | ||
Array.from(text) | ||
.map((char: string) => { | ||
const codePoint = char.codePointAt(0); | ||
return codePoint !== undefined ? `\\u{${codePoint.toString(16)}}` : ""; | ||
}) | ||
.join(""); |