From 6c57e4c6364b64fbcdfb5315900f3c5430f5e303 Mon Sep 17 00:00:00 2001 From: tokenosopher <47037769+tokenosopher@users.noreply.github.com> Date: Wed, 27 Dec 2023 20:14:55 +0000 Subject: [PATCH] Add string to unicode formatter (#11) * string to unicode * update readme * bump --- CHANGELOG.md | 6 ++++ README.md | 1 + package.json | 2 +- src/formatters/index.ts | 1 + src/formatters/stringToUnicode.test.ts | 42 ++++++++++++++++++++++++++ src/formatters/stringToUnicode.ts | 7 +++++ 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/formatters/stringToUnicode.test.ts create mode 100644 src/formatters/stringToUnicode.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3419827..ca9ea91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.38.0 + +### Minor Changes + +- add string to unicode formatter + ## 0.37.0 ### Minor Changes diff --git a/README.md b/README.md index f0ba16e..dd0f144 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ Contributions always welcome! ### Formatters - `formatNumber()` 1000 => "1,000" or "1K" +- `stringToUnicode()` "hello" => "\u0068\u0065\u006c\u006c\u006f" ### Random data generators diff --git a/package.json b/package.json index 9e0330b..fd06ba3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.37.0", + "version": "0.38.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/formatters/index.ts b/src/formatters/index.ts index 9cbb2fc..9de8943 100644 --- a/src/formatters/index.ts +++ b/src/formatters/index.ts @@ -1 +1,2 @@ export * from "./formatNumber"; +export * from "./stringToUnicode"; diff --git a/src/formatters/stringToUnicode.test.ts b/src/formatters/stringToUnicode.test.ts new file mode 100644 index 0000000..4762ac7 --- /dev/null +++ b/src/formatters/stringToUnicode.test.ts @@ -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}"); + }); +}); diff --git a/src/formatters/stringToUnicode.ts b/src/formatters/stringToUnicode.ts new file mode 100644 index 0000000..03de15e --- /dev/null +++ b/src/formatters/stringToUnicode.ts @@ -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("");