Skip to content

Commit

Permalink
string to css unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
tokenosopher committed Jan 26, 2024
1 parent 5ecdc80 commit 9a7f19e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.42.0

### Minor Changes

- added stringToCSSUnicode formatter function

## 0.41.0

### Minor Changes
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
38 changes: 38 additions & 0 deletions src/formatters/stringToCSSUnicode.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
7 changes: 7 additions & 0 deletions src/formatters/stringToCSSUnicode.ts
Original file line number Diff line number Diff line change
@@ -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("");

0 comments on commit 9a7f19e

Please sign in to comment.