From 9a7f19e353566e0aa9a3cbd05f028bf4f2677d0f Mon Sep 17 00:00:00 2001 From: tokenosopher Date: Fri, 26 Jan 2024 12:55:23 +0000 Subject: [PATCH 1/2] string to css unicode --- CHANGELOG.md | 6 ++++ README.md | 1 + package.json | 2 +- src/formatters/stringToCSSUnicode.test.ts | 38 +++++++++++++++++++++++ src/formatters/stringToCSSUnicode.ts | 7 +++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/formatters/stringToCSSUnicode.test.ts create mode 100644 src/formatters/stringToCSSUnicode.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 889f3d1..f2cc543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.42.0 + +### Minor Changes + +- added stringToCSSUnicode formatter function + ## 0.41.0 ### Minor Changes diff --git a/README.md b/README.md index 64b08ac..5df2800 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ Contributions always welcome! - `formatNumber()` 1000 => "1,000" or "1K" - `stringToUnicode()` "hello" => "\u0068\u0065\u006c\u006c\u006f" +- `stringToCSSUnicode()` "hello" => "\000068\000065\00006c\00006c\00006f" use this for CSS ### Random data generators diff --git a/package.json b/package.json index 6b33973..690aed0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.41.0", + "version": "0.42.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/formatters/stringToCSSUnicode.test.ts b/src/formatters/stringToCSSUnicode.test.ts new file mode 100644 index 0000000..2ed6630 --- /dev/null +++ b/src/formatters/stringToCSSUnicode.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, test } from "@jest/globals"; +import { stringToCSSUnicode } from "./stringToCSSUnicode"; + +describe("stringToCSSUnicode", () => { + test("converts basic ASCII characters correctly", () => { + expect(stringToCSSUnicode("Hello")).toBe("\\48\\65\\6c\\6c\\6f"); + }); + + test("converts space character correctly", () => { + expect(stringToCSSUnicode(" ")).toBe("\\20"); + }); + + test("converts a string with special characters", () => { + expect(stringToCSSUnicode("!@#$%^&*()_+")).toBe( + "\\21\\40\\23\\24\\25\\5e\\26\\2a\\28\\29\\5f\\2b" + ); + }); + + test("converts non-ASCII characters (e.g., accented letters)", () => { + expect(stringToCSSUnicode("รฉรฅรครถ")).toBe("\\e9\\e5\\e4\\f6"); + }); + + test("converts emojis correctly", () => { + expect(stringToCSSUnicode("๐Ÿ˜€๐Ÿ˜ƒ๐Ÿ˜„")).toBe("\\1f600\\1f603\\1f604"); + }); + + test("handles an empty string", () => { + expect(stringToCSSUnicode("")).toBe(""); + }); + + test("converts a string with mixed character types", () => { + expect(stringToCSSUnicode("Aa1!๐Ÿ˜€")).toBe("\\41\\61\\31\\21\\1f600"); + }); + + test("converts characters outside the Basic Multilingual Plane", () => { + expect(stringToCSSUnicode("๐ ฎท")).toBe("\\20bb7"); + }); +}); diff --git a/src/formatters/stringToCSSUnicode.ts b/src/formatters/stringToCSSUnicode.ts new file mode 100644 index 0000000..7d58c2d --- /dev/null +++ b/src/formatters/stringToCSSUnicode.ts @@ -0,0 +1,7 @@ +export const stringToCSSUnicode = (text: string): string => + Array.from(text) + .map((char: string) => { + const codePoint = char.codePointAt(0); + return codePoint !== undefined ? `\\${codePoint.toString(16)}` : ""; + }) + .join(""); From 0d7f9313f9f8a986ba8d4c8b461c5c17c58614df Mon Sep 17 00:00:00 2001 From: tokenosopher Date: Fri, 26 Jan 2024 16:25:51 +0000 Subject: [PATCH 2/2] export from index --- src/formatters/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/formatters/index.ts b/src/formatters/index.ts index 9de8943..5dc2a82 100644 --- a/src/formatters/index.ts +++ b/src/formatters/index.ts @@ -1,2 +1,3 @@ export * from "./formatNumber"; export * from "./stringToUnicode"; +export * from "./stringToCSSUnicode";