qidian-shared 1.1.2 → 1.1.4

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/dist/index.mjs CHANGED
@@ -19,6 +19,7 @@ import { b2gb, b2kb, b2mb, cutFileSuffix, cutFilename, formatByte, isTypeByName
19
19
  import { formatGlob } from "./utils/glob.mjs";
20
20
  import { decrypt, encrypt } from "./utils/jsencrypt.mjs";
21
21
  import { parseRelaxedJSON } from "./utils/json.mjs";
22
+ import { getPlatformInfo } from "./utils/platformInfo.mjs";
22
23
  import { abortWrapper, suspectedWrapperPromise, to, waitTime } from "./utils/promise.mjs";
23
24
  import { camelize, copy2Clipboard, deleteSpace, initialConversion, kebabCase, levenshtein } from "./utils/str.mjs";
24
25
  import { identifyType, isFunction, isObject, isPromise, objToStr, toTypeStr } from "./utils/type.mjs";
@@ -60,6 +61,7 @@ export {
60
61
  generateAesKey,
61
62
  generateRandomString,
62
63
  getHtmlTxt,
64
+ getPlatformInfo,
63
65
  getScrollEl,
64
66
  getUrlParam,
65
67
  getUrlParams,
@@ -9,6 +9,7 @@ export * from './file';
9
9
  export * from './glob';
10
10
  export * from './jsencrypt';
11
11
  export * from './json';
12
+ export * from './platformInfo';
12
13
  export * from './promise';
13
14
  export * from './str';
14
15
  export * from './type';
@@ -0,0 +1,183 @@
1
+ const platformInfo = {
2
+ system: "",
3
+ beWrapped: false,
4
+ engine: "",
5
+ supporter: "",
6
+ shell: "",
7
+ shellVs: "",
8
+ networkType: "",
9
+ systemVs: "",
10
+ platform: "",
11
+ client: "",
12
+ engineVs: "",
13
+ supporterVs: "",
14
+ webp: true
15
+ };
16
+ const navigator = typeof window === "undefined" ? void 0 : window.navigator;
17
+ const ua = navigator?.userAgent.toLowerCase() || "";
18
+ const testUa = (regexp) => regexp.test(ua);
19
+ const testVs = (regexp) => {
20
+ let res = ua.match(regexp) || "";
21
+ res = res.toString().replace(/[^0-9|_.]/g, "").replace(/_/g, ".");
22
+ const index = res.indexOf(".");
23
+ if (index === 0) res = res.slice(index + 1);
24
+ return res;
25
+ };
26
+ function getPlatformInfo() {
27
+ platformInfo.system = getSystem();
28
+ platformInfo.beWrapped = getBeWrapped();
29
+ platformInfo.networkType = getNetworkType();
30
+ platformInfo.systemVs = getSystemVs(platformInfo.system);
31
+ Object.assign(
32
+ platformInfo,
33
+ getPlatformClient(platformInfo.system),
34
+ getEngineSupporter(),
35
+ getShell()
36
+ );
37
+ platformInfo.engineVs = getEngineVs(platformInfo.engine);
38
+ platformInfo.supporterVs = getSupporterVs(platformInfo.supporter);
39
+ platformInfo.webp = getWebp();
40
+ return platformInfo;
41
+ }
42
+ function getNetworkType() {
43
+ let type = navigator?.connection?.type || navigator?.connection?.effectiveType;
44
+ if (type) type = type.toUpperCase();
45
+ else type = "unknow";
46
+ return type;
47
+ }
48
+ function getSystem() {
49
+ let system = "unknow";
50
+ if (testUa(/windows|win32|win64|wow32|wow64/g)) system = "Windows";
51
+ else if (testUa(/macintosh|macintel/g)) system = "MacOS";
52
+ else if (testUa(/x11/g)) system = "Linux";
53
+ else if (testUa(/android|adr/g)) system = "Android";
54
+ else if (testUa(/openharmony/g)) system = "HarmonyOS";
55
+ else if (testUa(/ios|iphone|ipod|iwatch/g)) system = "iOS";
56
+ else if (testUa(/ipad/g)) system = "iPadOS";
57
+ return system;
58
+ }
59
+ function getBeWrapped() {
60
+ return !(window.self === window.top);
61
+ }
62
+ function getSystemVs(system) {
63
+ let systemVs = "unknow";
64
+ if (system === "Windows") {
65
+ if (testUa(/windows nt 5.0|windows 2000/g)) systemVs = "2000";
66
+ else if (testUa(/windows nt 5.1|windows xp/g)) systemVs = "xp";
67
+ else if (testUa(/windows nt 5.2|windows 2003/g)) systemVs = "2003";
68
+ else if (testUa(/windows nt 6.0|windows vista/g)) systemVs = "vista";
69
+ else if (testUa(/windows nt 6.1|windows 7/g)) systemVs = "7";
70
+ else if (testUa(/windows nt 6.2|windows 8/g)) systemVs = "8";
71
+ else if (testUa(/windows nt 6.3|windows 8.1/g)) systemVs = "8.1";
72
+ else if (testUa(/windows nt 10.0|windows 10/g)) systemVs = "10";
73
+ } else if (system === "MacOS") {
74
+ systemVs = testVs(/os x [\d._]+/g);
75
+ } else if (system === "Android") {
76
+ systemVs = testVs(/android [\d._]+/g);
77
+ } else if (system === "HarmonyOS") {
78
+ systemVs = testVs(/openharmony [\d._]+/g);
79
+ } else if (system === "iOS" || system === "iPadOS") {
80
+ systemVs = testVs(/os [\d._]+/g);
81
+ }
82
+ return systemVs;
83
+ }
84
+ function getPlatformClient(system) {
85
+ let platform = "unknow";
86
+ let client = "unknow";
87
+ if (["Windows", "MacOS", "Linux"].indexOf(system) > -1) {
88
+ platform = "Desktop";
89
+ client = "PC";
90
+ } else if (testUa(/tablet/g) || testUa(/ipad/g)) {
91
+ platform = "Pad";
92
+ client = "Mobile";
93
+ } else if (["Android", "iOS", "HarmonyOS"].indexOf(system) > -1 || testUa(/mobile/g)) {
94
+ platform = "Mobile";
95
+ client = "Mobile";
96
+ }
97
+ return { platform, client };
98
+ }
99
+ function getEngineSupporter() {
100
+ let engine = "unknow";
101
+ let supporter = "unknow";
102
+ if (testUa(/applewebkit/g)) {
103
+ engine = "Webkit";
104
+ if (testUa(/edge/g)) supporter = "Edge";
105
+ else if (testUa(/opr/g)) supporter = "Opera";
106
+ else if (testUa(/chrome/g)) supporter = "Chrome";
107
+ else if (testUa(/safari/g)) supporter = "Safari";
108
+ } else if (testUa(/gecko/g) && testUa(/firefox/g)) {
109
+ engine = "Gecko";
110
+ supporter = "Firefox";
111
+ } else if (testUa(/presto/g)) {
112
+ engine = "Presto";
113
+ supporter = "Opera";
114
+ } else if (testUa(/trident|compatible|msie/g)) {
115
+ engine = "Trident";
116
+ supporter = "IExplore";
117
+ }
118
+ return { engine, supporter };
119
+ }
120
+ function getEngineVs(engine) {
121
+ let engineVs = "unknow";
122
+ if (engine === "Webkit") engineVs = testVs(/applewebkit\/[\d._]+/g);
123
+ else if (engine === "Gecko") engineVs = testVs(/gecko\/[\d._]+/g);
124
+ else if (engine === "Presto") engineVs = testVs(/presto\/[\d._]+/g);
125
+ else if (engine === "Trident") engineVs = testVs(/trident\/[\d._]+/g);
126
+ return engineVs;
127
+ }
128
+ function getSupporterVs(supporter) {
129
+ let supporterVs = "unknow";
130
+ if (supporter === "Chrome") supporterVs = testVs(/chrome\/[\d._]+/g);
131
+ else if (supporter === "Safari") supporterVs = testVs(/version\/[\d._]+/g);
132
+ else if (supporter === "Firefox") supporterVs = testVs(/firefox\/[\d._]+/g);
133
+ else if (supporter === "Opera") supporterVs = testVs(/opr\/[\d._]+/g);
134
+ else if (supporter === "IExplore") supporterVs = testVs(/(msie [\d._]+)|(rv:[\d._]+)/g);
135
+ else if (supporter === "Edge") supporterVs = testVs(/edge\/[\d._]+/g);
136
+ return supporterVs;
137
+ }
138
+ function getShell() {
139
+ let shell = "none";
140
+ let shellVs = "unknow";
141
+ if (testUa(/miniprogram/g)) {
142
+ if (testUa(/MicroMessenger/i)) {
143
+ shell = "MP-WEIXIN";
144
+ } else if (testUa(/AliApp/i)) {
145
+ shell = "MP-ALIPAY";
146
+ } else {
147
+ shell = "MP";
148
+ }
149
+ shellVs = testVs(/miniprogram\/[\d._]+/g) || testVs(/micromessenger\/[\d._]+/g);
150
+ } else if (testUa(/micromessenger/g)) {
151
+ shell = "Weixin";
152
+ shellVs = testVs(/micromessenger\/[\d._]+/g);
153
+ } else if (testUa(/qqbrowser/g)) {
154
+ shell = "QQ";
155
+ shellVs = testVs(/qqbrowser\/[\d._]+/g);
156
+ } else if (testUa(/ucbrowser/g)) {
157
+ shell = "UC";
158
+ shellVs = testVs(/ucbrowser\/[\d._]+/g);
159
+ } else if (testUa(/qihu 360se/g)) {
160
+ shell = "360";
161
+ } else if (testUa(/2345explorer/g)) {
162
+ shell = "2345";
163
+ shellVs = testVs(/2345explorer\/[\d._]+/g);
164
+ } else if (testUa(/metasr/g)) {
165
+ shell = "Sougou";
166
+ } else if (testUa(/lbbrowser/g)) {
167
+ shell = "Liebao";
168
+ } else if (testUa(/maxthon/g)) {
169
+ shell = "Maxthon";
170
+ shellVs = testVs(/maxthon\/[\d._]+/g);
171
+ }
172
+ return { shell, shellVs: shell === "none" ? "none" : shellVs };
173
+ }
174
+ function getWebp() {
175
+ try {
176
+ return document.createElement("canvas").toDataURL("image/webp", 0.5).indexOf("data:image/webp") === 0;
177
+ } catch {
178
+ return false;
179
+ }
180
+ }
181
+ export {
182
+ getPlatformInfo
183
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qidian-shared",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "QiDian 共享工具函数和钩子",
5
5
  "type": "module",
6
6
  "author": "qidian",