forked from facebook/react-native-website
-
Notifications
You must be signed in to change notification settings - Fork 328
/
methodFormatter.js
63 lines (56 loc) · 1.67 KB
/
methodFormatter.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
/**
* Copyright (c) Meta Platforms, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const {typeOf} = require('tokenize-comment/lib/utils');
const magic = require('./magic');
const {formatMultiplePlatform} = require('./utils');
function formatMethodType(param) {
let text, url;
if (param?.type?.name === 'union') {
if (param?.type?.alias) {
const {alias} = param.type;
if (Object.hasOwnProperty.call(magic.linkableTypeAliases, alias)) {
({url, text} = magic.linkableTypeAliases[alias]);
}
if (url) return `[${text}](${url})`;
else return param.type.alias;
}
return param.type.name;
} else {
if (param?.type?.type) return param.type.type;
else return param.type.name;
}
}
function formatMethodName(param) {
let tag = param.description;
if (tag) {
const isMatch = tag.match(/{@platform [a-z ,]*}/);
if (isMatch) {
const platform = isMatch[0].match(/ [a-z ,]*/);
tag = tag.replace(/{@platform [a-z ,]*}/g, '');
tag = formatMultiplePlatform(platform[0].split(','));
return param.name + tag;
}
}
return param.name;
}
function formatMethodDescription(param) {
let tag = param.description;
const isMatch = tag.match(/{@platform [a-z ,]*}/);
if (isMatch) {
const platform = isMatch[0].match(/ [a-z ,]*/);
// Replaces @platform strings with empty string
// and appends type with formatted platform
tag = tag.replace(/{@platform [a-z ,]*}/g, '');
}
return tag;
}
module.exports = {
formatMethodType,
formatMethodName,
formatMethodDescription,
};