forked from ant-design/ant-design-mobile-rn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.jsx
100 lines (90 loc) · 2.7 KB
/
utils.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* eslint no-useless-escape: 0 */
export function getMenuItems(moduleData, locale) {
const menuMeta = moduleData.map(item => item.meta);
const menuItems = {};
menuMeta.sort((a, b) => (a.order || 0) - (b.order || 0)).forEach((meta) => {
const category = (meta.category && meta.category[locale]) || meta.category || 'topLevel';
if (!menuItems[category]) {
menuItems[category] = {};
}
const type = meta.type || 'topLevel';
if (!menuItems[category][type]) {
menuItems[category][type] = [];
}
menuItems[category][type].push(meta);
});
return menuItems;
}
export function isZhCN(pathname) {
return /-cn\/?$/.test(pathname);
}
export function getLocalizedPathname(path, zhCN) {
const pathname = path.startsWith('/') ? path : `/${path}`;
if (!zhCN) { // to enUS
return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
} else if (pathname === '/') {
return '/index-cn';
} else if (pathname.endsWith('/')) {
return pathname.replace(/\/$/, '-cn/');
}
return `${pathname}-cn`;
}
export function ping(callback) {
// eslint-disable-next-line
const url = 'https://private-a' + 'lipay' + 'objects.alip' + 'ay.com/alip' + 'ay-rmsdeploy-image/rmsportal/RKuAiriJqrUhyqW.png';
const img = new Image();
let done;
const finish = (status) => {
if (!done) {
done = true;
img.src = '';
callback(status);
}
};
img.onload = () => finish('responded');
img.onerror = () => finish('error');
img.src = url;
return setTimeout(() => finish('timeout'), 1500);
}
export function isLocalStorageNameSupported() {
const testKey = 'test';
const storage = window.localStorage;
try {
storage.setItem(testKey, '1');
storage.removeItem(testKey);
return true;
} catch (error) {
return false;
}
}
export function collectDocs(docs) {
// locale copy from layout
const locale = (window.localStorage && localStorage.getItem('locale') !== 'en-US') ?
'zh-CN' : 'en-US';
const docsList = Object.keys(docs)
.map(key => docs[key])
.map((value) => {
if (typeof value !== 'function') {
return value[locale] || value.index[locale] || value.index;
}
return value;
})
.map(fn => fn());
return docsList;
}
export function getQuery(key) {
const val = window.location.search
.replace(/^\?/, '')
.split('&')
.filter(item => item)
.map(item => item.split('='))
.find(item => item[0] && item[0] === key);
return val && val[1];
}
export function injectPreactDevtool() {
/* eslint-disable no-undef,global-require, no-console */
if (typeof PREACT_DEVTOOLS !== 'undefined' && PREACT_DEVTOOLS) {
console.log('inject preact devtools');
require('preact/debug');
}
}