forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement-order.ts
More file actions
54 lines (47 loc) · 1.65 KB
/
statement-order.ts
File metadata and controls
54 lines (47 loc) · 1.65 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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import fs from 'node:fs';
import { BrowserName, CompatStatement } from '../../types/types.js';
import { IS_WINDOWS } from '../../test/utils.js';
import compareStatements from '../lib/compare-statements.js';
/**
* Return a new "support_block" object whose support statements have
* been ordered in reverse chronological order, moving statements
* with flags, partial support, prefixes, or alternative names lower.
*
* @param {string} key The key in the object
* @param {CompatStatement} value The value of the key
* @returns {CompatStatement} The new value
*/
export const orderStatements = (
key: string,
value: CompatStatement,
): CompatStatement => {
if (key === '__compat') {
for (const browser of Object.keys(value.support) as BrowserName[]) {
const supportData = value.support[browser];
if (Array.isArray(supportData)) {
value.support[browser] = supportData.sort(compareStatements);
}
}
}
return value;
};
/**
* Fix issues with statement order throughout the BCD files
*
* @param {string} filename The name of the file to fix
*/
const fixStatementOrder = (filename: string): void => {
let actual = fs.readFileSync(filename, 'utf-8').trim();
let expected = JSON.stringify(JSON.parse(actual, orderStatements), null, 2);
if (IS_WINDOWS) {
// prevent false positives from git.core.autocrlf on Windows
actual = actual.replace(/\r/g, '');
expected = expected.replace(/\r/g, '');
}
if (actual !== expected) {
fs.writeFileSync(filename, expected + '\n', 'utf-8');
}
};
export default fixStatementOrder;