Skip to content

Commit

Permalink
Add string to unicode formatter (#11)
Browse files Browse the repository at this point in the history
* string to unicode

* update readme

* bump
  • Loading branch information
tokenosopher authored Dec 27, 2023
1 parent 6c1b74c commit 6c57e4c
Show file tree
Hide file tree
Showing 6 changed files with 58 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.38.0

### Minor Changes

- add string to unicode formatter

## 0.37.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 @@ -97,6 +97,7 @@ Contributions always welcome!
### Formatters

- `formatNumber()` 1000 => "1,000" or "1K"
- `stringToUnicode()` "hello" => "\u0068\u0065\u006c\u006c\u006f"

### 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.37.0",
"version": "0.38.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 +1,2 @@
export * from "./formatNumber";
export * from "./stringToUnicode";
42 changes: 42 additions & 0 deletions src/formatters/stringToUnicode.test.ts
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}");
});
});
7 changes: 7 additions & 0 deletions src/formatters/stringToUnicode.ts
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("");

0 comments on commit 6c57e4c

Please sign in to comment.