Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String to css unicode #13

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./formatNumber";
export * from "./stringToUnicode";
export * from "./stringToCSSUnicode";
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 =>
tokenosopher marked this conversation as resolved.
Show resolved Hide resolved
Array.from(text)
.map((char: string) => {
const codePoint = char.codePointAt(0);
return codePoint !== undefined ? `\\${codePoint.toString(16)}` : "";
})
.join("");
Loading