-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
helpers.js
186 lines (157 loc) · 4.79 KB
/
helpers.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const upperFirst = require('lodash/upperFirst');
const camelCase = require('lodash/camelCase');
const { CODE_VARIANTS, LANGUAGES } = require('../constants');
function titleize(string) {
if (process.env.NODE_ENV !== 'production') {
if (typeof string !== 'string' || string.length <= 0) {
console.error('titleize(string) expects a non empty string argument.');
}
}
return string
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
function pageToTitle(page) {
if (page.title === false) {
return null;
}
if (page.title) {
return page.title;
}
const path = page.subheader || page.pathname;
const name = path.replace(/.*\//, '');
if (path.indexOf('/api/') !== -1) {
return upperFirst(camelCase(name));
}
return titleize(name);
}
function pageToTitleI18n(page, t) {
const path = page.subheader || page.pathname;
return t(`pages.${path}`, { ignoreWarning: true }) || pageToTitle(page);
}
/**
* @var
* set of packages that ship their own typings instead of using @types/ namespace
* Array because Set([iterable]) is not supported in IE11
*/
const packagesWithBundledTypes = ['@material-ui/core', '@material-ui/lab'];
/**
* WARNING: Always uses `latest` typings.
*
* Adds dependencies to @types packages only for packages that are not listed
* in packagesWithBundledTypes
*
* @see packagesWithBundledTypes in this module namespace
*
* @param {Record<string, string>} deps - list of dependency as `name => version`
*/
function addTypeDeps(deps) {
const packagesWithDTPackage = Object.keys(deps).filter(
(name) => packagesWithBundledTypes.indexOf(name) === -1,
);
packagesWithDTPackage.forEach((name) => {
let resolvedName = name;
// scoped package?
if (name.startsWith('@')) {
// https://github.com/DefinitelyTyped/DefinitelyTyped#what-about-scoped-packages
resolvedName = name.slice(1).replace('/', '__');
}
deps[`@types/${resolvedName}`] = 'latest';
});
}
function includePeerDependencies(deps, versions) {
Object.assign(deps, {
'react-dom': versions['react-dom'],
react: versions.react,
});
if (
deps['@material-ui/lab'] ||
deps['@material-ui/pickers'] ||
deps['@material-ui/x'] ||
deps['@material-ui/x-grid'] ||
deps['@material-ui/x-pickers'] ||
deps['@material-ui/x-tree-view'] ||
deps['@material-ui/data-grid']
) {
deps['@material-ui/core'] = versions['@material-ui/core'];
}
if (deps['@material-ui/x-data-grid-generator']) {
deps['@material-ui/core'] = versions['@material-ui/core'];
deps['@material-ui/icons'] = versions['@material-ui/icons'];
deps['@material-ui/lab'] = versions['@material-ui/lab'];
}
if (deps['@material-ui/pickers']) {
deps['date-fns'] = 'latest';
}
}
/**
* @param {string} raw - ES6 source with es module imports
* @param {objects} options
* @param {'JS' | 'TS'} options.codeLanguage
* @param {'next' | 'latest'} options.reactVersion
* @returns {Record<string, 'latest'>} map of packages with their required version
*/
function getDependencies(raw, options = {}) {
const { codeLanguage = CODE_VARIANTS.JS, reactVersion = 'latest' } = options;
const deps = {};
const versions = {
'react-dom': reactVersion,
react: reactVersion,
'@material-ui/core': 'latest',
'@material-ui/icons': 'latest',
'@material-ui/lab': 'latest',
'@material-ui/styles': 'latest',
'@material-ui/system': 'latest',
'@material-ui/utils': 'latest',
// TODO: remove once @material-ui/pickers v4 is released.
'@date-io/date-fns': 'v1',
};
const re = /^import\s'([^']+)'|import\s[\s\S]*?\sfrom\s+'([^']+)/gm;
let m;
// eslint-disable-next-line no-cond-assign
while ((m = re.exec(raw))) {
let name;
if (m[2]) {
// full import
// handle scope names
name = m[2].charAt(0) === '@' ? m[2].split('/', 2).join('/') : m[2].split('/', 1)[0];
} else {
name = m[1];
}
if (!deps[name]) {
deps[name] = versions[name] ? versions[name] : 'latest';
}
}
includePeerDependencies(deps, versions);
if (codeLanguage === CODE_VARIANTS.TS) {
addTypeDeps(deps);
deps.typescript = 'latest';
}
return deps;
}
function getCookie(name) {
const regex = new RegExp(`(?:(?:^|.*;*)${name}*=*([^;]*).*$)|^.*$`);
return document.cookie.replace(regex, '$1');
}
function pathnameToLanguage(pathname) {
const userLanguage = pathname.substring(1, 3);
if (LANGUAGES.indexOf(userLanguage) !== -1 && pathname.indexOf(`/${userLanguage}/`) === 0) {
return {
userLanguage,
canonical: userLanguage === 'en' ? pathname : pathname.substring(3),
};
}
return {
userLanguage: 'en',
canonical: pathname,
};
}
module.exports = {
titleize,
pageToTitle,
pageToTitleI18n,
getDependencies,
getCookie,
pathnameToLanguage,
};