@iftc/yete 0.0.1-alpha.4 → 0.0.1-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/function/md2html.js +6 -0
- package/index.js +678 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -12,6 +12,53 @@ class YeteError extends Error {
|
|
|
12
12
|
this.name = 'YeteError';
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* 检查浏览器兼容性
|
|
17
|
+
* @throws {YeteError} 如果不兼容或无法判断则抛出错误
|
|
18
|
+
*/
|
|
19
|
+
(function checkBrowserCompatibility() {
|
|
20
|
+
const ua = navigator.userAgent;
|
|
21
|
+
let browser = null;
|
|
22
|
+
let version = 0;
|
|
23
|
+
if (/Edg\/(\d+)/.test(ua)) {
|
|
24
|
+
browser = 'Edge';
|
|
25
|
+
version = parseInt(ua.match(/Edg\/(\d+)/)[1], 10);
|
|
26
|
+
} else if (/Chrome\/(\d+)/.test(ua) && !/Chromium/.test(ua)) {
|
|
27
|
+
browser = 'Chrome';
|
|
28
|
+
version = parseInt(ua.match(/Chrome\/(\d+)/)[1], 10);
|
|
29
|
+
} else if (/Firefox\/(\d+)/.test(ua)) {
|
|
30
|
+
browser = 'Firefox';
|
|
31
|
+
version = parseInt(ua.match(/Firefox\/(\d+)/)[1], 10);
|
|
32
|
+
} else if (/Version\/(\d+)/.test(ua) && /Safari/.test(ua)) {
|
|
33
|
+
browser = 'Safari';
|
|
34
|
+
version = parseInt(ua.match(/Version\/(\d+)/)[1], 10);
|
|
35
|
+
} else if (/VVBrowser\/(\d+)/.test(ua)) {
|
|
36
|
+
browser = 'VVBrowser';
|
|
37
|
+
version = parseInt(ua.match(/VVBrowser\/(\d+)/)[1], 10);
|
|
38
|
+
}
|
|
39
|
+
console.log(browser, version);
|
|
40
|
+
const minVersions = {
|
|
41
|
+
'Chrome': 87,
|
|
42
|
+
'Firefox': 140,
|
|
43
|
+
'Safari': 18.4,
|
|
44
|
+
'Edge': 87,
|
|
45
|
+
'VVBrowser': 1
|
|
46
|
+
};
|
|
47
|
+
let isCompatible = false;
|
|
48
|
+
if (browser && minVersions[browser] !== undefined) {
|
|
49
|
+
if (version >= minVersions[browser]) {
|
|
50
|
+
isCompatible = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!isCompatible) {
|
|
54
|
+
const msg = browser
|
|
55
|
+
? `您的浏览器版本过低 (${browser} ${version}),请使用 Chrome 71+, Firefox 69+, Safari 12.1+, Edge 79+ 或 VVBrowser 1+ 浏览器。`
|
|
56
|
+
: `无法识别您的浏览器环境,请使用现代浏览器 (Chrome, Firefox, Safari, Edge, VVBrowser)。`;
|
|
57
|
+
|
|
58
|
+
alert(msg);
|
|
59
|
+
console.error(new YeteError('Browser incompatible'));
|
|
60
|
+
}
|
|
61
|
+
})();
|
|
15
62
|
/**
|
|
16
63
|
* 页面基类
|
|
17
64
|
* @example
|
|
@@ -69,6 +116,8 @@ let custom502Page = null;
|
|
|
69
116
|
|
|
70
117
|
let searchParams = new URLSearchParams("");
|
|
71
118
|
|
|
119
|
+
const hostname = location.hostname;
|
|
120
|
+
|
|
72
121
|
/**
|
|
73
122
|
* 导出的对象
|
|
74
123
|
*/
|
|
@@ -561,6 +610,26 @@ const obj = {
|
|
|
561
610
|
static async readHeaders(response) {
|
|
562
611
|
return response.headers;
|
|
563
612
|
}
|
|
613
|
+
/**
|
|
614
|
+
* 读取响应 Cookie
|
|
615
|
+
* @param {Response} response
|
|
616
|
+
* @returns {Promise<Cookie[]>}
|
|
617
|
+
* @example
|
|
618
|
+
* const cookies = await HTTP.readCookies(res);
|
|
619
|
+
*/
|
|
620
|
+
static async readCookies(response) {
|
|
621
|
+
return response.cookies;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* 获取代理 URL
|
|
625
|
+
* @param {String} url
|
|
626
|
+
* @returns {String}
|
|
627
|
+
* @example
|
|
628
|
+
* const proxyURL = HTTP.proxy("https://example.com");
|
|
629
|
+
*/
|
|
630
|
+
static proxy(url) {
|
|
631
|
+
return "https://iftc.koyeb.app/proxy?url=" + encodeURIComponent(url);
|
|
632
|
+
}
|
|
564
633
|
},
|
|
565
634
|
/**
|
|
566
635
|
* 获取 URL 参数
|
|
@@ -592,6 +661,615 @@ const obj = {
|
|
|
592
661
|
} else {
|
|
593
662
|
return loadUI(str);
|
|
594
663
|
}
|
|
664
|
+
},
|
|
665
|
+
/**
|
|
666
|
+
* 认证管理类
|
|
667
|
+
* @description 提供用户认证、令牌管理、授权验证等功能
|
|
668
|
+
*/
|
|
669
|
+
Auth: class {
|
|
670
|
+
/**
|
|
671
|
+
* 构造函数
|
|
672
|
+
* @param {Object} options 配置选项
|
|
673
|
+
* @property {String} apiBaseUrl API 基础地址
|
|
674
|
+
* @property {String} tokenKey localStorage 中存储 token 的键名
|
|
675
|
+
*/
|
|
676
|
+
constructor(options = {}) {
|
|
677
|
+
this.apiBaseUrl = options.apiBaseUrl || 'https://iftc.koyeb.app/api/auth';
|
|
678
|
+
this.tokenKey = options.tokenKey || 'auth_token';
|
|
679
|
+
this.checkInterval = options.checkInterval || 2000;
|
|
680
|
+
this.onAuthSuccess = options.onAuthSuccess || null;
|
|
681
|
+
this.onAuthFail = options.onAuthFail || null;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* 获取存储的 auth_token
|
|
686
|
+
* @returns {String|null}
|
|
687
|
+
* @example
|
|
688
|
+
* const token = auth.getAuthToken();
|
|
689
|
+
*/
|
|
690
|
+
getAuthToken() {
|
|
691
|
+
return localStorage.getItem(this.tokenKey);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* 设置存储的 auth_token
|
|
696
|
+
* @param {String} token
|
|
697
|
+
* @example
|
|
698
|
+
* auth.setAuthToken('your_token_here');
|
|
699
|
+
*/
|
|
700
|
+
setAuthToken(token) {
|
|
701
|
+
localStorage.setItem(this.tokenKey, token);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* 清除存储的 auth_token
|
|
706
|
+
* @example
|
|
707
|
+
* auth.clearAuthToken();
|
|
708
|
+
*/
|
|
709
|
+
clearAuthToken() {
|
|
710
|
+
localStorage.removeItem(this.tokenKey);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* 检查是否已授权
|
|
715
|
+
* @returns {Promise<Boolean>}
|
|
716
|
+
* @example
|
|
717
|
+
* if (auth.isAuthorized()) { ... }
|
|
718
|
+
*/
|
|
719
|
+
async isAuthorized() {
|
|
720
|
+
return !!await cookieStore.get("ID");
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* 获取授权链接
|
|
725
|
+
* @param {String} redirectUrl 回调地址
|
|
726
|
+
* @returns {String}
|
|
727
|
+
* @example
|
|
728
|
+
* const url = auth.getAuthUrl(location.href);
|
|
729
|
+
*/
|
|
730
|
+
getAuthUrl(redirectUrl) {
|
|
731
|
+
return `${this.apiBaseUrl}/token?redirect=${encodeURIComponent(redirectUrl)}`;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* 发起授权请求
|
|
736
|
+
* @description 跳转到授权页面
|
|
737
|
+
* @param {String} redirectUrl 回调地址
|
|
738
|
+
* @example
|
|
739
|
+
* auth.requestAuth(location.href);
|
|
740
|
+
*/
|
|
741
|
+
async requestAuth(redirectUrl) {
|
|
742
|
+
const url = this.getAuthUrl(redirectUrl);
|
|
743
|
+
const response = await fetch(url);
|
|
744
|
+
const json = await response.json();
|
|
745
|
+
|
|
746
|
+
if (json.code !== 200) {
|
|
747
|
+
throw new YeteError(json.msg || '获取授权令牌失败');
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
this.setAuthToken(json.data.token);
|
|
751
|
+
location.href = json.data.url;
|
|
752
|
+
return json;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* 验证 auth_token 有效性
|
|
757
|
+
* @param {String} token 可选,不传则使用存储的 auth_token
|
|
758
|
+
* @returns {Promise<Boolean>}
|
|
759
|
+
* @example
|
|
760
|
+
* const isValid = await auth.verify();
|
|
761
|
+
*/
|
|
762
|
+
async verify(token = null) {
|
|
763
|
+
const targetToken = token || this.getAuthToken();
|
|
764
|
+
|
|
765
|
+
if (!targetToken) {
|
|
766
|
+
throw new YeteError('未找到授权令牌');
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
const response = await fetch(`${this.apiBaseUrl}/verify?token=${targetToken}`);
|
|
770
|
+
const json = await response.json();
|
|
771
|
+
|
|
772
|
+
if (json.code === 200) {
|
|
773
|
+
await this.setCookie('ID', json.data.id, 365);
|
|
774
|
+
localStorage.setItem("ID", json.data.id);
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* 设置 cookie
|
|
782
|
+
* @param {String} name 键名
|
|
783
|
+
* @param {String} value 键值
|
|
784
|
+
* @param {Number} days 过期天数
|
|
785
|
+
*/
|
|
786
|
+
async setCookie(name, value, days = 1) {
|
|
787
|
+
const expires = new Date();
|
|
788
|
+
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
|
|
789
|
+
|
|
790
|
+
try {
|
|
791
|
+
console.log(location.hostname);
|
|
792
|
+
await cookieStore.set({
|
|
793
|
+
name,
|
|
794
|
+
value,
|
|
795
|
+
expires: expires.toISOString(),
|
|
796
|
+
path: '/',
|
|
797
|
+
domain: hostname,
|
|
798
|
+
secure: true
|
|
799
|
+
});
|
|
800
|
+
} catch (error) {
|
|
801
|
+
console.warn('cookieStore not supported, falling back to document.cookie');
|
|
802
|
+
document.cookie = `${name}=${value}; expires=${expires.toUTCString()}; path=/`;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* 获取 cookie
|
|
808
|
+
* @param {String} name 键名
|
|
809
|
+
* @returns {String|null}
|
|
810
|
+
*/
|
|
811
|
+
async getCookie(name) {
|
|
812
|
+
try {
|
|
813
|
+
const cookies = await cookieStore.get(name);
|
|
814
|
+
return cookies ? cookies.value : null;
|
|
815
|
+
} catch (error) {
|
|
816
|
+
// 如果 cookieStore 不支持,降级到 document.cookie
|
|
817
|
+
console.warn('cookieStore not supported, falling back to document.cookie');
|
|
818
|
+
return this.getCookieFromDocument(name);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* 从 document.cookie 获取 cookie(备用方案)
|
|
824
|
+
* @param {String} name 键名
|
|
825
|
+
* @returns {String|null}
|
|
826
|
+
*/
|
|
827
|
+
getCookieFromDocument(name) {
|
|
828
|
+
const nameEQ = name + "=";
|
|
829
|
+
const ca = document.cookie.split(';');
|
|
830
|
+
for (let i = 0; i < ca.length; i++) {
|
|
831
|
+
let c = ca[i];
|
|
832
|
+
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
|
|
833
|
+
if (c.indexOf(nameEQ) === 0) {
|
|
834
|
+
return c.substring(nameEQ.length, c.length);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
return null;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* 获取用户 ID
|
|
842
|
+
* @returns {String|null}
|
|
843
|
+
*/
|
|
844
|
+
getUserId() {
|
|
845
|
+
return localStorage.getItem("ID");
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/**
|
|
849
|
+
* 检查授权状态并等待授权完成
|
|
850
|
+
* @param {Function} callback 授权成功后的回调
|
|
851
|
+
* @param {Number} timeout 超时时间(毫秒)
|
|
852
|
+
* @returns {Promise<Object>}
|
|
853
|
+
* @example
|
|
854
|
+
* await auth.waitForAuth((userInfo) => {
|
|
855
|
+
* console.log('用户 ID:', userInfo.id);
|
|
856
|
+
* });
|
|
857
|
+
*/
|
|
858
|
+
async waitForAuth(callback, timeout = 60000) {
|
|
859
|
+
const url = new URL(location.href);
|
|
860
|
+
const authParam = url.searchParams.get('auth');
|
|
861
|
+
|
|
862
|
+
if (!authParam) {
|
|
863
|
+
throw new YeteError('缺少 auth 参数');
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
return new Promise((resolve, reject) => {
|
|
867
|
+
const startTime = Date.now();
|
|
868
|
+
const intervalId = setInterval(async () => {
|
|
869
|
+
// 检查超时
|
|
870
|
+
if (Date.now() - startTime > timeout) {
|
|
871
|
+
clearInterval(intervalId);
|
|
872
|
+
reject(new YeteError('授权超时'));
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
try {
|
|
877
|
+
const json = await this.verify();
|
|
878
|
+
if (json.code === 200) {
|
|
879
|
+
clearInterval(intervalId);
|
|
880
|
+
if (callback) callback(json.data);
|
|
881
|
+
if (this.onAuthSuccess) this.onAuthSuccess(json.data);
|
|
882
|
+
resolve(json);
|
|
883
|
+
}
|
|
884
|
+
} catch (error) {
|
|
885
|
+
console.error('验证失败:', error);
|
|
886
|
+
}
|
|
887
|
+
}, this.checkInterval);
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* 处理授权回调
|
|
893
|
+
* @description 在授权回调页面调用此方法
|
|
894
|
+
* @param {Function} successCallback 授权成功回调
|
|
895
|
+
* @param {Function} failCallback 授权失败回调
|
|
896
|
+
* @example
|
|
897
|
+
* auth.handleAuthCallback((userInfo) => {
|
|
898
|
+
* document.write('授权成功,用户 ID: ' + userInfo.id);
|
|
899
|
+
* });
|
|
900
|
+
*/
|
|
901
|
+
async handleAuthCallback(successCallback, failCallback) {
|
|
902
|
+
const url = new URL(location.href);
|
|
903
|
+
const authParam = url.searchParams.get('auth');
|
|
904
|
+
|
|
905
|
+
if (!authParam) {
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
try {
|
|
910
|
+
const result = await this.waitForAuth(successCallback);
|
|
911
|
+
if (successCallback) {
|
|
912
|
+
successCallback(result.data);
|
|
913
|
+
}
|
|
914
|
+
} catch (error) {
|
|
915
|
+
console.error('授权失败:', error);
|
|
916
|
+
if (failCallback) {
|
|
917
|
+
failCallback(error);
|
|
918
|
+
}
|
|
919
|
+
if (this.onAuthFail) {
|
|
920
|
+
this.onAuthFail(error);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* 获取用户信息
|
|
927
|
+
* @returns {Promise<Object>}
|
|
928
|
+
* @example
|
|
929
|
+
* const userInfo = await auth.getUserInfo();
|
|
930
|
+
*/
|
|
931
|
+
async getUserInfo() {
|
|
932
|
+
const res = await fetch(`${this.apiBaseUrl}/api/user/details?ID=${this.getUserId()}`);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* 登出
|
|
937
|
+
* @example
|
|
938
|
+
* auth.logout();
|
|
939
|
+
*/
|
|
940
|
+
logout() {
|
|
941
|
+
this.clearAuthToken();
|
|
942
|
+
location.reload();
|
|
943
|
+
}
|
|
944
|
+
},
|
|
945
|
+
Complex: Complex,
|
|
946
|
+
/**
|
|
947
|
+
* 屏幕颜色选择器
|
|
948
|
+
* @description 该功能必须由用户主动触发(如点击按钮)才行。
|
|
949
|
+
* @returns {Promise<string>} 十六进制颜色值(如#FF0000)
|
|
950
|
+
* @throws {YeteError} 如果用户取消了选择或浏览器不支持 EyeDropper API
|
|
951
|
+
* @example
|
|
952
|
+
* button.addEventListener('click', async () => {
|
|
953
|
+
* const color = await colorPicker();
|
|
954
|
+
* console.log(color);
|
|
955
|
+
* })
|
|
956
|
+
*/
|
|
957
|
+
colorPicker: function () {
|
|
958
|
+
if (!globalThis.EyeDropper) {
|
|
959
|
+
throw new YeteError("Your browser does not support the EyeDropper API. Please use a compatible browser (Chrome 95+, Edge 95+, or Opera 81+).");
|
|
960
|
+
}
|
|
961
|
+
const eyeDropper = new EyeDropper();
|
|
962
|
+
const abortController = new AbortController();
|
|
963
|
+
return eyeDropper.open({ signal: abortController.signal }).then(result => result.sRGBHex).catch(error => {
|
|
964
|
+
if (error.name === 'AbortError') {
|
|
965
|
+
throw new YeteError('用户取消了选择');
|
|
966
|
+
} else {
|
|
967
|
+
throw error;
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* 复数类
|
|
975
|
+
*/
|
|
976
|
+
class Complex {
|
|
977
|
+
/**
|
|
978
|
+
* 复数构造函数
|
|
979
|
+
* @param {number} real 实部
|
|
980
|
+
* @param {number} imag 虚部
|
|
981
|
+
*/
|
|
982
|
+
constructor(real = 0, imag = 0) {
|
|
983
|
+
this.real = real;
|
|
984
|
+
this.imag = imag;
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* 字符串转复数类
|
|
988
|
+
* @param {String} complexString
|
|
989
|
+
* @returns {Complex}
|
|
990
|
+
* @example
|
|
991
|
+
* const complex = new Complex();
|
|
992
|
+
* complex.toClass("2+3i"); // 输出:Complex(2, 3)
|
|
993
|
+
*/
|
|
994
|
+
toClass(complexString) {
|
|
995
|
+
const parts = complexString.split('+');
|
|
996
|
+
const real = parseFloat(parts[0]) || 0;
|
|
997
|
+
const imag = parseFloat(parts[1].slice(0, -1)) || 0;
|
|
998
|
+
this.real = real;
|
|
999
|
+
this.imag = imag;
|
|
1000
|
+
return this;
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* 加法
|
|
1004
|
+
* @param {Complex} other 另一个复数
|
|
1005
|
+
* @returns {Complex} 结果
|
|
1006
|
+
* @example
|
|
1007
|
+
* const c1 = new Complex(2, 3);
|
|
1008
|
+
* const c2 = new Complex(1, 4);
|
|
1009
|
+
* console.log(c1.add(c2)); // 输出:Complex(3, 7)
|
|
1010
|
+
*/
|
|
1011
|
+
add(other) {
|
|
1012
|
+
return new Complex(
|
|
1013
|
+
this.real + other.real,
|
|
1014
|
+
this.imag + other.imag
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* 减法
|
|
1019
|
+
* @param {Complex} other 另一个复数
|
|
1020
|
+
* @returns {Complex} 减法结果
|
|
1021
|
+
* @example
|
|
1022
|
+
* const c1 = new Complex(2, 3);
|
|
1023
|
+
* const c2 = new Complex(1, 4);
|
|
1024
|
+
* console.log(c1.subtract(c2)); // 输出:Complex(1, -1)
|
|
1025
|
+
*/
|
|
1026
|
+
subtract(other) {
|
|
1027
|
+
return new Complex(
|
|
1028
|
+
this.real - other.real,
|
|
1029
|
+
this.imag - other.imag
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* 乘法
|
|
1034
|
+
* @param {Complex} other 乘数
|
|
1035
|
+
* @returns {Complex} 乘法结果
|
|
1036
|
+
* @example
|
|
1037
|
+
* const c1 = new Complex(2, 3);
|
|
1038
|
+
* const c2 = new Complex(1, 4);
|
|
1039
|
+
* console.log(c1.multiply(c2)); // 输出:Complex(5, 14)
|
|
1040
|
+
*/
|
|
1041
|
+
multiply(other) {
|
|
1042
|
+
return new Complex(
|
|
1043
|
+
this.real * other.real - this.imag * other.imag,
|
|
1044
|
+
this.real * other.imag + this.imag * other.real
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* 除法
|
|
1049
|
+
* @param {Complex} other 被除数
|
|
1050
|
+
* @returns {Complex} 除法结果
|
|
1051
|
+
* @example
|
|
1052
|
+
* const c1 = new Complex(2, 3);
|
|
1053
|
+
* const c2 = new Complex(1, 4);
|
|
1054
|
+
* console.log(c1.divide(c2)); // 输出:Complex(0.6, -0.2)
|
|
1055
|
+
*/
|
|
1056
|
+
divide(other) {
|
|
1057
|
+
const denominator = other.real * other.real + other.imag * other.imag;
|
|
1058
|
+
if (denominator === 0) {
|
|
1059
|
+
throw new Error("Division by zero complex number");
|
|
1060
|
+
}
|
|
1061
|
+
return new Complex(
|
|
1062
|
+
(this.real * other.real + this.imag * other.imag) / denominator,
|
|
1063
|
+
(this.imag * other.real - this.real * other.imag) / denominator
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* 复数转为字符串
|
|
1068
|
+
* @returns {string} 复数字符串表示
|
|
1069
|
+
* @example
|
|
1070
|
+
* const c = new Complex(2, 3);
|
|
1071
|
+
* console.log(c.toString()); // 输出:2+3i
|
|
1072
|
+
*/
|
|
1073
|
+
toString() {
|
|
1074
|
+
const sign = this.imag >= 0 ? '+' : '-';
|
|
1075
|
+
return `${this.real} ${sign} ${Math.abs(this.imag)}i`;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* 获取复数的模
|
|
1079
|
+
* @returns {number} 模
|
|
1080
|
+
* @example
|
|
1081
|
+
* const c = new Complex(3, 4);
|
|
1082
|
+
* console.log(c.magnitude()); // 输出:5
|
|
1083
|
+
*/
|
|
1084
|
+
magnitude() {
|
|
1085
|
+
return Math.sqrt(this.real * this.real + this.imag * this.imag);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
obj.Complex = Complex;
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* 生成哈希值
|
|
1093
|
+
* @description 与Java的用法一致
|
|
1094
|
+
* @returns {Number}
|
|
1095
|
+
* @example
|
|
1096
|
+
* const hash = 'hello world'.hashCode();
|
|
1097
|
+
*/
|
|
1098
|
+
String.prototype.hashCode = function () {
|
|
1099
|
+
let hash = 0;
|
|
1100
|
+
for (let i = 0; i < this.length; i++) {
|
|
1101
|
+
const char = this.charCodeAt(i);
|
|
1102
|
+
hash = (hash << 5) - hash + char;
|
|
1103
|
+
hash |= 0;
|
|
1104
|
+
}
|
|
1105
|
+
return hash;
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* 将任意进制的数字转换为十进制(支持小数)
|
|
1110
|
+
* @param {Number} fromRadix - 源进制 (2-36)
|
|
1111
|
+
* @param {Number} toRadix - 目标进制 (2-36)
|
|
1112
|
+
* @returns {String} - 转换后的目标进制的字符串
|
|
1113
|
+
* @example
|
|
1114
|
+
* const result = "10.1".convertBase(2, 10); // 二进制 "10.1" 转换为十进制 "2.5"
|
|
1115
|
+
*/
|
|
1116
|
+
String.prototype.convertBase = function (fromRadix = 10, toRadix = 10) {
|
|
1117
|
+
return convertBase(this, fromRadix, toRadix);
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
/**
|
|
1121
|
+
* 将任意进制的数字转换为另一种进制的数字
|
|
1122
|
+
* @param {string} numStr - 输入的数字字符串(例如 "10.1")
|
|
1123
|
+
* @param {number} fromRadix - 源进制 (2-36)
|
|
1124
|
+
* @param {number} toRadix - 目标进制 (2-36)
|
|
1125
|
+
* @returns {string} - 转换后的字符串
|
|
1126
|
+
*/
|
|
1127
|
+
function convertBase(numStr, fromRadix, toRadix) {
|
|
1128
|
+
if (fromRadix < 2 || fromRadix > 36 || toRadix < 2 || toRadix > 36) {
|
|
1129
|
+
throw new YeteError("进制必须在 2 到 36 之间");
|
|
1130
|
+
}
|
|
1131
|
+
const decimalValue = convertToDecimal(numStr, fromRadix);
|
|
1132
|
+
return decimalValue.toString(toRadix);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
/**
|
|
1136
|
+
* 将任意进制(2-36)的字符串转换为十进制数字(支持小数)
|
|
1137
|
+
* @param {string} str - 输入的数字字符串 (例如 "10.1")
|
|
1138
|
+
* @param {number} radix - 进制数 (例如 2, 8, 16)
|
|
1139
|
+
* @returns {number} - 转换后的十进制数
|
|
1140
|
+
*/
|
|
1141
|
+
function convertToDecimal(str, radix) {
|
|
1142
|
+
if (radix < 2 || radix > 36) {
|
|
1143
|
+
throw new YeteError("进制必须在 2 到 36 之间");
|
|
1144
|
+
}
|
|
1145
|
+
const input = str.toLowerCase();
|
|
1146
|
+
const parts = input.split('.');
|
|
1147
|
+
const integerPart = parts[0];
|
|
1148
|
+
const fractionalPart = parts[1] || '';
|
|
1149
|
+
let result = 0;
|
|
1150
|
+
let isNegative = input.startsWith('-');
|
|
1151
|
+
if (integerPart && integerPart !== '-') {
|
|
1152
|
+
const cleanInt = integerPart.replace('-', '');
|
|
1153
|
+
result = parseInt(cleanInt, radix);
|
|
1154
|
+
if (isNaN(result)) {
|
|
1155
|
+
throw new YeteError(`整数部分 "${integerPart}" 包含非法字符`);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
let fractionValue = 0;
|
|
1159
|
+
for (let i = 0; i < fractionalPart.length; i++) {
|
|
1160
|
+
const char = fractionalPart[i];
|
|
1161
|
+
const digitValue = parseInt(char, 36);
|
|
1162
|
+
if (isNaN(digitValue) || digitValue >= radix) {
|
|
1163
|
+
throw new YeteError(`小数部分 "${char}" 超出了进制 ${radix} 的范围`);
|
|
1164
|
+
}
|
|
1165
|
+
fractionValue += digitValue * Math.pow(radix, -(i + 1));
|
|
1166
|
+
}
|
|
1167
|
+
result = result + fractionValue;
|
|
1168
|
+
if (isNegative) {
|
|
1169
|
+
result = -result;
|
|
1170
|
+
}
|
|
1171
|
+
return result;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* 响应事件监听
|
|
1176
|
+
*/
|
|
1177
|
+
Response.prototype.events = {};
|
|
1178
|
+
/**
|
|
1179
|
+
* 是否正在监听响应事件
|
|
1180
|
+
*/
|
|
1181
|
+
Response.prototype.listening = false;
|
|
1182
|
+
/**
|
|
1183
|
+
* 添加响应事件监听
|
|
1184
|
+
* @param {string} eventName 事件名
|
|
1185
|
+
* @param {function} callback 回调函数
|
|
1186
|
+
* @example
|
|
1187
|
+
* response.on('eventName', function() {})
|
|
1188
|
+
*/
|
|
1189
|
+
Response.prototype.on = function (eventName, callback) {
|
|
1190
|
+
if (!this.events[eventName]) {
|
|
1191
|
+
this.events[eventName] = [];
|
|
1192
|
+
}
|
|
1193
|
+
this.events[eventName].push(callback);
|
|
1194
|
+
};
|
|
1195
|
+
/**
|
|
1196
|
+
* 触发事件
|
|
1197
|
+
* @param {string} eventName 事件名
|
|
1198
|
+
* @param {...any} args 参数
|
|
1199
|
+
* @example
|
|
1200
|
+
* response.emit('eventName', '参数1', '参数2', ...);
|
|
1201
|
+
*/
|
|
1202
|
+
Response.prototype.emit = function (eventName, ...args) {
|
|
1203
|
+
if (this.events[eventName]) {
|
|
1204
|
+
this.events[eventName].forEach(callback => callback(...args));
|
|
1205
|
+
}
|
|
1206
|
+
};
|
|
1207
|
+
/**
|
|
1208
|
+
* 移除事件监听
|
|
1209
|
+
* @param {string} eventName 事件名
|
|
1210
|
+
* @param {Function} callback 回调函数
|
|
1211
|
+
* @example
|
|
1212
|
+
* response.off('eventName', callback);
|
|
1213
|
+
*/
|
|
1214
|
+
Response.prototype.off = function (eventName, callback) {
|
|
1215
|
+
if (this.events[eventName]) {
|
|
1216
|
+
this.events[eventName] = this.events[eventName].filter(cb => cb !== callback);
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
/**
|
|
1220
|
+
* 开始同步监听(开启后将无法使用异步)
|
|
1221
|
+
* @returns {Promise<void>}
|
|
1222
|
+
* @example
|
|
1223
|
+
* response.listen();
|
|
1224
|
+
*/
|
|
1225
|
+
Response.prototype.listen = async function () {
|
|
1226
|
+
if (this.listening) {
|
|
1227
|
+
throw new YeteError('已开启同步监听,请勿重复开启');
|
|
1228
|
+
}
|
|
1229
|
+
const res = this;
|
|
1230
|
+
const headers = res.headers;
|
|
1231
|
+
this.listening = true;
|
|
1232
|
+
const contentType = headers.get('Content-Type');
|
|
1233
|
+
if (contentType && contentType.includes('application/json')) {
|
|
1234
|
+
const json = await res.json();
|
|
1235
|
+
res.emit('res', {
|
|
1236
|
+
type: 'json',
|
|
1237
|
+
data: json,
|
|
1238
|
+
});
|
|
1239
|
+
} else if (contentType && contentType.includes('text/html')) {
|
|
1240
|
+
const text = await res.text();
|
|
1241
|
+
const doc = new DOMParser().parseFromString(text, "text/html");
|
|
1242
|
+
res.emit('res', {
|
|
1243
|
+
type: 'html',
|
|
1244
|
+
data: doc,
|
|
1245
|
+
});
|
|
1246
|
+
} else if (contentType && contentType.includes('text/plain')) {
|
|
1247
|
+
const text = await res.text();
|
|
1248
|
+
res.emit('res', {
|
|
1249
|
+
type: 'text',
|
|
1250
|
+
data: text,
|
|
1251
|
+
})
|
|
1252
|
+
} else if (contentType && contentType.includes('text/xml')) {
|
|
1253
|
+
const text = await res.text();
|
|
1254
|
+
const xmlDoc = new DOMParser().parseFromString(text, "text/xml");
|
|
1255
|
+
res.emit('res', {
|
|
1256
|
+
type: 'xml',
|
|
1257
|
+
data: xmlDoc,
|
|
1258
|
+
});
|
|
1259
|
+
} else if (contentType && contentType.includes('text/event-stream')) {
|
|
1260
|
+
obj.HTTP.readStream(res, ({ chunk, done }) => {
|
|
1261
|
+
res.emit('res', {
|
|
1262
|
+
type: 'stream',
|
|
1263
|
+
data: chunk,
|
|
1264
|
+
done: done,
|
|
1265
|
+
});
|
|
1266
|
+
});
|
|
1267
|
+
} else {
|
|
1268
|
+
const blob = await res.blob();
|
|
1269
|
+
res.emit('res', {
|
|
1270
|
+
type: 'blob',
|
|
1271
|
+
data: blob,
|
|
1272
|
+
});
|
|
595
1273
|
}
|
|
596
1274
|
};
|
|
597
1275
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iftc/yete",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"IFTC",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"dexie": "^4.3.0",
|
|
19
|
-
"esbuild": "^0.27.3"
|
|
19
|
+
"esbuild": "^0.27.3",
|
|
20
|
+
"marked": "^17.0.5"
|
|
20
21
|
}
|
|
21
22
|
}
|