Skip to content

Commit

Permalink
✨ 新增 encoding 相关的辅助函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaubee committed Oct 9, 2024
1 parent 3751108 commit 1dfb326
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 6 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dweb/gaubee-util",
"version": "0.3.0",
"version": "0.4.0",
"tasks": {
"build": "deno run -A ./dnt.ts",
"pub-npm": "cd npm && npm publish --access public",
Expand All @@ -14,6 +14,7 @@
"./bigint": "./src/bigint.ts",
"./collections": "./src/collections.ts",
"./date": "./src/date.ts",
"./encoding": "./src/encoding.ts",
"./event_target": "./src/event_target.ts",
"./evt": "./src/evt.ts",
"./func": "./src/func.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ type IterableItem<A> = A extends Iterable<infer T> ? T : never;
*
* 注意,如果不存在,会抛出异常
*/
export const iter_get_first = <T>(items: Iterable<T>) =>
export const iter_get_first = <T>(items: Iterable<T>): T =>
iter_get_first_or_default(items, () => {
throw new RangeError("could not get first element");
});

/**
* 取集合第一个元素
*/
export const iter_get_first_or_null = <T>(items: Iterable<T>) =>
export const iter_get_first_or_null = <T>(items: Iterable<T>): T | undefined =>
iter_get_first_or_default(items, () => void 0);

/**
Expand Down
44 changes: 43 additions & 1 deletion src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
export const date_clone = (date: Date): Date => {
return new Date(date);
};
/**
* 人类可读的时间对象
*/
export type Duration = {
year: number;
month: number;
Expand All @@ -16,7 +19,10 @@ export type Duration = {
* 向一个 date 中添加时间
* 可以是负数
*/
export const date_add = (date: Date, duration: Partial<Duration>): Date => {
export const date_add_duration = (
date: Date,
duration: Partial<Duration>,
): Date => {
const {
year = NaN,
month = NaN,
Expand Down Expand Up @@ -66,3 +72,39 @@ export const date_to_duration = (date: Date): Duration => {
milliseconds,
};
};
/**
* 对一个 date 对象进行写入
*/
export const date_set_duration = (
date: Date,
duration: Partial<Duration>,
): Date => {
const {
year = NaN,
month = NaN,
day = NaN,
minutes = NaN,
seconds = NaN,
milliseconds = NaN,
} = duration;

if (Number.isFinite(year)) {
date.setFullYear(year);
}
if (Number.isFinite(month)) {
date.setMonth(month - 1);
}
if (Number.isFinite(day)) {
date.setDate(day);
}
if (Number.isFinite(minutes)) {
date.setMinutes(minutes);
}
if (Number.isFinite(seconds)) {
date.setSeconds(seconds);
}
if (Number.isFinite(milliseconds)) {
date.setMilliseconds(milliseconds);
}
return date;
};
40 changes: 40 additions & 0 deletions src/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 将二进制转成 base64 字符串
*/
export const binary_to_base64_string = (u8a: Uint8Array): string => {
if ("base64Slice" in u8a) {
return (u8a as any).toString("base64");
}
let binaryString = "";
for (let i = 0; i < u8a.length; i++) {
binaryString += String.fromCharCode(u8a[i]);
}
return btoa(binaryString);
};

/**
* 将二进制转成 utf8 字符串
*/
export const binary_to_utf8_string = (u8a: Uint8Array): string => {
return d.decode(u8a);
};
const d = new TextDecoder();
/**
* 将base64 字符串转成二进制
*/
export const str_to_base64_binary = (str: string): Uint8Array => {
const binaryString = atob(str);
const uint8Array = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
uint8Array[i] = binaryString.charCodeAt(i);
}
return uint8Array;
};

/**
* 将utf8 字符串转成二进制
*/
export const str_to_utf8_binary = (str: string): Uint8Array => {
return e.encode(str);
};
const e = new TextEncoder();
8 changes: 6 additions & 2 deletions src/evt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { iter_map_async } from "./collections.ts";

// import "/ext/array.ext";
/** Evt 的监听函数定义 */
export type EvtFun<T> = (data: T) => unknown;
/** Evt 移除监听函数定义 */
export type EvtOff = () => boolean;
/**
* 个极简的事件监听,支持异步错误捕捉
Expand All @@ -27,7 +28,10 @@ export class Evt<T> implements AsyncIterable<T> {
return;
}
const errors: unknown[] = [];
const results = await iter_map_async(this.#cbs.values(), (cb) => cb(data));
const results = await iter_map_async(
this.#cbs.values(),
(cb) => cb(data),
);
for (const item of results) {
if (item.status === "rejected") {
errors.push(...item.reason);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./ag.ts";
export * from "./bigint.ts";
export * from "./collections.ts";
export * from "./date.ts";
export * from "./encoding.ts";
export * from "./event_target.ts";
export * from "./evt.ts";
export * from "./func.ts";
Expand Down

0 comments on commit 1dfb326

Please sign in to comment.