From d553fc9a7e02dbce73443161c22e7b691d60279b Mon Sep 17 00:00:00 2001 From: MJmajiong <936233505@qq.com> Date: Wed, 25 Sep 2024 14:24:59 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat():=20=E5=A2=9E=E5=8A=A0=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=B8=AD=E6=96=87=E7=9A=84base64=E8=BD=AC=E6=8D=A2=20?= =?UTF-8?q?refs=20BRICK-555?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pipes/atobUnicode.spec.ts | 10 ++++++++++ src/pipes/atobUnicode.ts | 22 ++++++++++++++++++++++ src/pipes/btoaUnicode.spec.ts | 10 ++++++++++ src/pipes/btoaUnicode.ts | 22 ++++++++++++++++++++++ src/pipes/index.ts | 2 ++ 5 files changed, 66 insertions(+) create mode 100644 src/pipes/atobUnicode.spec.ts create mode 100644 src/pipes/atobUnicode.ts create mode 100644 src/pipes/btoaUnicode.spec.ts create mode 100644 src/pipes/btoaUnicode.ts 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..3a296d2 --- /dev/null +++ b/src/pipes/atobUnicode.ts @@ -0,0 +1,22 @@ +/** + * 支持base64字符解码为中文 + * + * @category Type conversion + * + * @public + * + * @param value - 需要进行解码的值 + * + * @returns 解码后的值 + */ +import { TextDecoder } from "util"; + +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..d76b981 --- /dev/null +++ b/src/pipes/btoaUnicode.ts @@ -0,0 +1,22 @@ +/** + * 支持将中文进行base64编码 + * + * @category Type conversion + * + * @public + * + * @param value - 需要进行编码的值 + * + * @returns 编码后的值 + */ +import { TextEncoder } from "util"; + +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); + + // return window.btoa(unescape(encodeURIComponent(value))); +} 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"; From d36a79f287f0bc6d5c19f3fa8d9030b66f560a42 Mon Sep 17 00:00:00 2001 From: MJmajiong <936233505@qq.com> Date: Wed, 25 Sep 2024 14:40:35 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix():=20=E5=88=A0=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E4=BB=A3=E7=A0=81=20refs=20BRICK-555?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pipes/atobUnicode.ts | 2 +- src/pipes/btoaUnicode.ts | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pipes/atobUnicode.ts b/src/pipes/atobUnicode.ts index 3a296d2..3a27e1e 100644 --- a/src/pipes/atobUnicode.ts +++ b/src/pipes/atobUnicode.ts @@ -1,3 +1,4 @@ +import { TextDecoder } from "util"; /** * 支持base64字符解码为中文 * @@ -9,7 +10,6 @@ * * @returns 解码后的值 */ -import { TextDecoder } from "util"; export function atobUnicode(value: string): string { const binary = window.atob(value); diff --git a/src/pipes/btoaUnicode.ts b/src/pipes/btoaUnicode.ts index d76b981..a819b2a 100644 --- a/src/pipes/btoaUnicode.ts +++ b/src/pipes/btoaUnicode.ts @@ -1,3 +1,4 @@ +import { TextEncoder } from "util"; /** * 支持将中文进行base64编码 * @@ -9,7 +10,6 @@ * * @returns 编码后的值 */ -import { TextEncoder } from "util"; export function btoaUnicode(value: string): string { const encoder = new TextEncoder(); @@ -17,6 +17,4 @@ export function btoaUnicode(value: string): string { let binary = ""; bytes.forEach((byte: any) => (binary += String.fromCharCode(byte))); return window.btoa(binary); - - // return window.btoa(unescape(encodeURIComponent(value))); } From 3c78a5e4a9473d8a24e6a30fe066415722c15ccf Mon Sep 17 00:00:00 2001 From: MJmajiong <936233505@qq.com> Date: Wed, 25 Sep 2024 14:49:54 +0800 Subject: [PATCH 3/5] =?UTF-8?q?docs():=20=E8=B0=83=E6=95=B4=E6=96=87?= =?UTF-8?q?=E6=A1=A3=20refs=20BRICK-555?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pipes/atobUnicode.ts | 3 +-- src/pipes/btoaUnicode.ts | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pipes/atobUnicode.ts b/src/pipes/atobUnicode.ts index 3a27e1e..85570d1 100644 --- a/src/pipes/atobUnicode.ts +++ b/src/pipes/atobUnicode.ts @@ -2,7 +2,7 @@ import { TextDecoder } from "util"; /** * 支持base64字符解码为中文 * - * @category Type conversion + * @category Logic * * @public * @@ -10,7 +10,6 @@ import { TextDecoder } from "util"; * * @returns 解码后的值 */ - export function atobUnicode(value: string): string { const binary = window.atob(value); const bytes = new Uint8Array(binary.length); diff --git a/src/pipes/btoaUnicode.ts b/src/pipes/btoaUnicode.ts index a819b2a..28811e1 100644 --- a/src/pipes/btoaUnicode.ts +++ b/src/pipes/btoaUnicode.ts @@ -2,7 +2,7 @@ import { TextEncoder } from "util"; /** * 支持将中文进行base64编码 * - * @category Type conversion + * @category Logic * * @public * @@ -10,7 +10,6 @@ import { TextEncoder } from "util"; * * @returns 编码后的值 */ - export function btoaUnicode(value: string): string { const encoder = new TextEncoder(); const bytes = encoder.encode(value); From 6c1a5fbcad70ff59da1c22ab13920f01ebe671a4 Mon Sep 17 00:00:00 2001 From: MJmajiong <936233505@qq.com> Date: Wed, 25 Sep 2024 15:10:12 +0800 Subject: [PATCH 4/5] =?UTF-8?q?docs():=20=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E6=A1=A3=20refs=20BRICK-555?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/brick-next-pipes.api.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/etc/brick-next-pipes.api.md b/etc/brick-next-pipes.api.md index fafc90a..2ee72b3 100644 --- a/etc/brick-next-pipes.api.md +++ b/etc/brick-next-pipes.api.md @@ -188,4 +188,10 @@ export function yamlStringify(value: unknown, indent?: number, opts?: { sortKeys?: boolean; }): string; +// @public +export function atobUnicode(string): string; + +// @public +export function btoaUnicode(string): string; + ``` From 9b42cf7d3c8ffa7b691624c844cf7735688b12bc Mon Sep 17 00:00:00 2001 From: MJmajiong <936233505@qq.com> Date: Wed, 25 Sep 2024 15:15:00 +0800 Subject: [PATCH 5/5] =?UTF-8?q?docs():=20=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E6=A1=A3=20refs=20BRICK-555?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/brick-next-pipes.api.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/etc/brick-next-pipes.api.md b/etc/brick-next-pipes.api.md index 2ee72b3..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; @@ -188,10 +194,4 @@ export function yamlStringify(value: unknown, indent?: number, opts?: { sortKeys?: boolean; }): string; -// @public -export function atobUnicode(string): string; - -// @public -export function btoaUnicode(string): string; - ```