-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgenerators.mjs
More file actions
173 lines (157 loc) · 4.85 KB
/
generators.mjs
File metadata and controls
173 lines (157 loc) · 4.85 KB
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
'use strict';
import { coerce, compare, major } from 'semver';
import { DOC_API_BASE_URL_VERSION } from '../constants.mjs';
/**
* Groups all the API metadata nodes by module (`api` property) so that we can process each different file
* based on the module it belongs to.
*
* @param {Array<ApiDocMetadataEntry>} nodes The API metadata Nodes to be grouped
*/
export const groupNodesByModule = nodes => {
/** @type {Map<string, Array<ApiDocMetadataEntry>>} */
const groupedNodes = new Map();
for (const node of nodes) {
if (!groupedNodes.has(node.api)) {
groupedNodes.set(node.api, []);
}
groupedNodes.get(node.api).push(node);
}
return groupedNodes;
};
/**
* Parses the SemVer string into a Node.js-alike version
*
* @param {import('semver').SemVer} version The version to be parsed
*/
export const getVersionFromSemVer = version =>
version.minor === 0
? `${version.major}.x`
: `${version.major}.${version.minor}.x`;
/**
* Gets the documentation URL for an API and version
*
* @param {string} version The version to be parsed
* @param {string} api The document
*/
export const getVersionURL = (version, api) =>
`${DOC_API_BASE_URL_VERSION}${version}/api/${api}.html`;
/**
* @TODO: This should not be necessary, and indicates errors within the API docs
* @TODO: Hookup into a future Validation/Linting API
*
* This is a safe fallback to ensure that we always have a SemVer compatible version
* even if the input is not a valid SemVer string
*
* @param {string|import('semver').SemVer} version SemVer compatible version (maybe)
* @returns {import('semver').SemVer} SemVer compatible version
*/
export const coerceSemVer = version => {
const coercedVersion = coerce(version);
if (coercedVersion === null) {
// @TODO: Linter to complain about invalid SemVer strings
return coerce('0.0.0-REPLACEME');
}
return coercedVersion;
};
/**
* Gets compatible versions for an entry
*
* @param {string | import('semver').SemVer} introduced
* @param {Array<ApiDocReleaseEntry>} releases
* @param {Boolean} [includeNonMajor=false]
* @returns {Array<ApiDocReleaseEntry>}
*/
export const getCompatibleVersions = (introduced, releases) => {
const coercedMajor = major(coerceSemVer(introduced));
// All Node.js versions that support the current API; If there's no "introduced_at" field,
// we simply show all versions, as we cannot pinpoint the exact version
return releases.filter(release => release.version.major >= coercedMajor);
};
/**
* Maps `updates` into `changes` format, merges them and sorts them by version
* ç
* @param {Array<ApiDocMetadataChange>} changes Changes to be merged into updates
* @param {[string='version']} key The key where versions are stored
* @returns {Array<ApiDocMetadataChange>} Mapped, merged and sorted changes
*/
export const sortChanges = (changes, key = 'version') => {
// Sorts the updates and changes by the first version on a given entry
return changes.toSorted((a, b) => {
const aVersion = Array.isArray(a[key]) ? a[key][0] : a[key];
const bVersion = Array.isArray(b[key]) ? b[key][0] : b[key];
return compare(coerceSemVer(bVersion), coerceSemVer(aVersion));
});
};
/**
* Assigns properties from one or more source objects to the target object
* **without overwriting existing keys** in the target.
*
* Similar to `Object.assign`, but preserves the target's existing keys.
* The target object is mutated in place.
*
* @param {Object} target - The object to assign properties to.
* @param {Object} source - The source object
*/
export const leftHandAssign = (target, source) =>
Object.keys(source).forEach(k => k in target || (target[k] = source[k]));
/**
* Transforms an object to JSON output consistent with the JSON version.
* @param {Object} section - The source object
* @param section.api
* @param section.type
* @param section.source
* @param section.introduced_in
* @param section.meta
* @param section.stability
* @param section.stabilityText
* @param section.classes
* @param section.methods
* @param section.properties
* @param section.miscs
* @param section.modules
* @param section.globals
* @returns {string} - The JSON output
*/
export const legacyToJSON = ({
api,
type,
source,
introduced_in,
meta,
stability,
stabilityText,
classes,
methods,
properties,
miscs,
modules,
globals,
}) =>
JSON.stringify(
api == null
? {
// all.json special order
miscs,
modules,
classes,
globals,
methods,
}
: {
type,
source,
introduced_in,
meta,
stability,
stabilityText,
classes,
methods,
properties,
miscs,
// index.json shouldn't have a `modules` key:
...(api === 'index' ? undefined : { modules }),
globals,
},
null,
2
);