diff --git a/etc/brick-next-pipes.api.md b/etc/brick-next-pipes.api.md index fafc90a..318ea6a 100644 --- a/etc/brick-next-pipes.api.md +++ b/etc/brick-next-pipes.api.md @@ -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) @@ -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; diff --git a/src/pipes/atobUnicode.spec.ts b/src/pipes/atobUnicode.spec.ts new file mode 100644 index 0000000..ca35420 --- /dev/null +++ b/src/pipes/atobUnicode.spec.ts @@ -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); + }); +}); diff --git a/src/pipes/atobUnicode.ts b/src/pipes/atobUnicode.ts new file mode 100644 index 0000000..85570d1 --- /dev/null +++ b/src/pipes/atobUnicode.ts @@ -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); +} diff --git a/src/pipes/btoaUnicode.spec.ts b/src/pipes/btoaUnicode.spec.ts new file mode 100644 index 0000000..52d8d9d --- /dev/null +++ b/src/pipes/btoaUnicode.spec.ts @@ -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); + }); +}); diff --git a/src/pipes/btoaUnicode.ts b/src/pipes/btoaUnicode.ts new file mode 100644 index 0000000..28811e1 --- /dev/null +++ b/src/pipes/btoaUnicode.ts @@ -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); +} diff --git a/src/pipes/index.ts b/src/pipes/index.ts index b8fe728..8c58638 100644 --- a/src/pipes/index.ts +++ b/src/pipes/index.ts @@ -73,3 +73,5 @@ export * from "./unitFormat"; export * from "./yaml"; export * from "./yamlStringify"; export * from "./round"; +export * from "./atobUnicode"; +export * from "./btoaUnicode";