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

feat(): 增加支持中文的base64转换 refs BRICK-555 #83

Merged
merged 5 commits into from
Sep 25, 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 etc/brick-next-pipes.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// @public
export function add(value: number | string, operand: number | string): number | string;

// @public
export function atobUnicode(value: string): string;

// Warning: (ae-internal-missing-underscore) The name "bool" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
Expand All @@ -15,6 +18,9 @@ export const bool: typeof boolean;
// @public
export function boolean(value: unknown): boolean;

// @public
export function btoaUnicode(value: string): string;

// @public
export function cmdbInstanceShowName(value: string | string[]): string;

Expand Down
10 changes: 10 additions & 0 deletions src/pipes/atobUnicode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { atobUnicode } from "./atobUnicode";

describe("atobUnicode", () => {
const testCases: [string, string][] = [
["5L2g5aW9LCDkuJbnlYw=", "你好, 世界"],
];
test.each(testCases)("atobUnicode(%j) should return %j", (input, output) => {
expect(atobUnicode(input)).toEqual(output);
});
});
21 changes: 21 additions & 0 deletions src/pipes/atobUnicode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TextDecoder } from "util";
/**
* 支持base64字符解码为中文
*
* @category Logic
*
* @public
*
* @param value - 需要进行解码的值
*
* @returns 解码后的值
*/
export function atobUnicode(value: string): string {
const binary = window.atob(value);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
const decoder = new TextDecoder();
return decoder.decode(bytes);
}
10 changes: 10 additions & 0 deletions src/pipes/btoaUnicode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { btoaUnicode } from "./btoaUnicode";

describe("btoaUnicode", () => {
const testCases: [string, string][] = [
["你好, 世界", "5L2g5aW9LCDkuJbnlYw="],
];
test.each(testCases)("btoaUnicode(%j) should return %j", (input, output) => {
expect(btoaUnicode(input)).toEqual(output);
});
});
19 changes: 19 additions & 0 deletions src/pipes/btoaUnicode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { TextEncoder } from "util";
/**
* 支持将中文进行base64编码
*
* @category Logic
*
* @public
*
* @param value - 需要进行编码的值
*
* @returns 编码后的值
*/
export function btoaUnicode(value: string): string {
const encoder = new TextEncoder();
const bytes = encoder.encode(value);
let binary = "";
bytes.forEach((byte: any) => (binary += String.fromCharCode(byte)));
return window.btoa(binary);
}
2 changes: 2 additions & 0 deletions src/pipes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ export * from "./unitFormat";
export * from "./yaml";
export * from "./yamlStringify";
export * from "./round";
export * from "./atobUnicode";
export * from "./btoaUnicode";
Loading