Skip to content

Commit

Permalink
♻️ ✨ ag=>generator;新增string.ts和string.global.ts; collection 新增 iter_f…
Browse files Browse the repository at this point in the history
…irst_not_null
  • Loading branch information
Gaubee committed Oct 11, 2024
1 parent e79f8d6 commit f541d7a
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 6 deletions.
5 changes: 3 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gaubee/util",
"version": "0.9.4",
"version": "0.10.0",
"tasks": {
"build": "deno run -A ./dnt.ts",
"npm": "deno task build && deno task pub-npm",
Expand All @@ -11,20 +11,21 @@
"@deno/dnt": "jsr:@deno/dnt@^0.41.3"
},
"exports": {
"./ag": "./src/ag.ts",
"./bigint": "./src/bigint.ts",
"./collections": "./src/collections.ts",
"./date": "./src/date.ts",
"./encoding": "./src/encoding.ts",
"./event_target": "./src/event_target.ts",
"./func": "./src/func.ts",
"./generator": "./src/generator.ts",
"./lrc": "./src/lrc.ts",
"./map": "./src/map.ts",
"./number": "./src/number.ts",
"./object": "./src/object.ts",
"./promise": "./src/promise.ts",
"./readable_stream": "./src/readable_stream.ts",
"./shared_flow": "./src/shared_flow.ts",
"./string": "./src/string.ts",
".": "./src/index.ts"
},
"fmt": {
Expand Down
24 changes: 24 additions & 0 deletions src/collections.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ import {
arr_get_first_or_null,
arr_get_last,
arr_get_last_or_null,
iter_first_not_null,
iter_map_not_null,
} from "./collections.ts";
import { curryThisFn, extendsMethod } from "./func.ts";
import { GF } from "./generator.ts";

extendsMethod(Array.prototype, "first", curryThisFn(arr_get_first));
extendsMethod(Array.prototype, "firstOrNull", curryThisFn(arr_get_first_or_null));
extendsMethod(Array.prototype, "last", curryThisFn(arr_get_last));
extendsMethod(Array.prototype, "lastOrNull", curryThisFn(arr_get_last_or_null));
const mapNotNull = curryThisFn(iter_map_not_null);
extendsMethod(Array.prototype, "mapNotNull", mapNotNull);
extendsMethod(Map.prototype, "mapNotNull", mapNotNull);
extendsMethod(Set.prototype, "mapNotNull", mapNotNull);
extendsMethod(GF.prototype, "mapNotNull", mapNotNull);
const firstNotNull = curryThisFn(iter_first_not_null);
extendsMethod(Array.prototype, "firstNotNull", firstNotNull);
extendsMethod(Map.prototype, "firstNotNull", firstNotNull);
extendsMethod(Set.prototype, "firstNotNull", firstNotNull);
extendsMethod(GF.prototype, "firstNotNull", firstNotNull);

declare global {
interface Array<T> {
Expand All @@ -21,5 +31,19 @@ declare global {
readonly last: T;
readonly lastOrNull: T | undefined;
mapNotNull: typeof mapNotNull;
firstNotNull: typeof firstNotNull;
}

interface Map<K, V> {
mapNotNull: typeof mapNotNull;
firstNotNull: typeof firstNotNull;
}
interface Set<T> {
mapNotNull: typeof mapNotNull;
firstNotNull: typeof firstNotNull;
}
interface Generator<T = unknown, TReturn = any, TNext = unknown> extends Iterator<T, TReturn, TNext> {
mapNotNull: typeof mapNotNull;
firstNotNull: typeof firstNotNull;
}
}
11 changes: 11 additions & 0 deletions src/collections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assert from "node:assert";
import { iter_first_not_null } from "./collections.ts";

Deno.test("iter_first_not_null", () => {
const result = iter_first_not_null([1, 2, 3, 4], (v) => {
if (v > 2) {
return `data:${v}`;
}
});
assert.equal(result, "data:3");
});
26 changes: 26 additions & 0 deletions src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,29 @@ export const iter_map_not_null = <TS extends Iterable<T>, T = IterableItem<TS>,
}
return result;
};

/**
* 类似与 map not null,但只取第一个
*
* 支持任何可迭代的对象
*/
export const iter_first_not_null = <TS extends Iterable<T>, T = IterableItem<TS>, R = unknown>(
values: TS,
callbackfn: (
value: T,
index: number,
values: TS,
) => R,
): R | undefined => {
let index = 0;
for (const value of values) {
const r = callbackfn(
value,
index++,
values,
);
if (r != null) {
return r;
}
}
};
2 changes: 1 addition & 1 deletion src/ag.global.ts → src/generator.global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ag_done, ag_then, AGF } from "./ag.ts";
import { ag_done, ag_then, AGF } from "./generator.ts";
import { curryThisFn, extendsMethod } from "./func.ts";

const done = curryThisFn(ag_done);
Expand Down
17 changes: 16 additions & 1 deletion src/ag.ts → src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { str_reverse } from "./string.ts";

/**
* GeneratorFunction
*/
export const GF = (() => {
try {
Function(
str_reverse("rotcurtsnoc.)}{)(*noitcnuf( nruter"),
)();
} catch {
return (function* () {}).constructor;
}
})() as GeneratorFunction;

/**
* AsyncGeneratorFunction
*/
export const AGF = (() => {
try {
Function(
"return (async function*(){}).constructor",
str_reverse("rotcurtsnoc.)}{)(*noitcnuf cnysa( nruter"),
)();
} catch {
return (async function* () {}).constructor;
Expand Down
3 changes: 2 additions & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./ag.global.ts";
import "./generator.global.ts";
import "./collections.global.ts";
import "./encoding.global.ts";
import "./object.global.ts";
import "./readable_stream.global.ts";
import "./string.global.ts";
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./ag.ts";
export * from "./generator.ts";
export * from "./bigint.ts";
export * from "./collections.ts";
export * from "./date.ts";
Expand All @@ -12,3 +12,4 @@ export * from "./object.ts";
export * from "./promise.ts";
export * from "./readable_stream.ts";
export * from "./shared_flow.ts";
export * from "./string.ts";
11 changes: 11 additions & 0 deletions src/string.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { curryThisFn, extendsMethod } from "./func.ts";
import { str_reverse } from "./string.ts";

const reverse = curryThisFn(str_reverse);
extendsMethod(String.prototype, "reverse", reverse);

declare global {
interface String {
reverse(): string;
}
}
11 changes: 11 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 反转字符串
*/
export const str_reverse = (str: string): string => {
let restr = "";
/// 不能用 i++ < length 迭代,会有Unicode字符问题
for (const char of str) {
restr = char + restr;
}
return restr;
};

0 comments on commit f541d7a

Please sign in to comment.