Replace built-in utility with foxts

This commit is contained in:
SukkaW
2024-12-11 17:05:22 +08:00
parent 9bbdb10631
commit d726dcb64c
12 changed files with 60 additions and 106 deletions

View File

@@ -2,24 +2,6 @@ import path, { dirname } from 'node:path';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import { OUTPUT_CLASH_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../constants/dir';
import type { HeadersInit } from 'undici';
export const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;
export function fastStringArrayJoin(arr: string[], sep: string) {
const len = arr.length;
if (len === 0) {
return '';
}
let result = arr[0];
for (let i = 1; i < len; i++) {
result += sep;
result += arr[i];
}
return result;
}
export function fastStringCompare(a: string, b: string) {
const lenA = a.length;
@@ -86,22 +68,6 @@ export function domainWildCardToRegex(domain: string) {
return result;
}
export const identity = <T, R = T>(x: T): R => x as any;
export function appendArrayFromSet<T>(dest: T[], source: Set<T> | Array<Set<T>>, transformer: (item: T) => T = identity) {
const casted = Array.isArray(source) ? source : [source];
for (let i = 0, len = casted.length; i < len; i++) {
const iterator = casted[i].values();
let step: IteratorResult<T, undefined>;
while ((step = iterator.next(), !step.done)) {
dest.push(transformer(step.value));
}
}
return dest;
}
export function output(id: string, type: 'non_ip' | 'ip' | 'domainset') {
return [
path.join(OUTPUT_SURGE_DIR, type, id + '.conf'),
@@ -122,30 +88,3 @@ export function withBannerArray(title: string, description: string[] | readonly
'################## EOF ##################'
];
};
export function mergeHeaders<T extends RequestInit['headers'] | HeadersInit>(headersA: T | undefined, headersB: T | undefined): T {
if (headersA == null) {
return headersB!;
}
if (Array.isArray(headersB)) {
throw new TypeError('Array headers is not supported');
}
const result = new Headers(headersA as any);
if (headersB instanceof Headers) {
headersB.forEach((value, key) => {
result.set(key, value);
});
return result as T;
}
for (const key in headersB) {
if (Object.hasOwn(headersB, key)) {
result.set(key, (headersB as Record<string, any>)[key]);
}
}
return result as T;
}