Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions scripts/build/mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,43 @@ export const getMatchingBrowserVersion = (targetBrowser, sourceVersion) => {
* @param {(string) => (string | false)} versionMapper - Receives the source browser version and returns the target browser version.
* @returns {Notes | null} The notes with replacement performed
*/
const updateNotes = (notes, regex, replace, versionMapper) => {
export const updateNotes = (notes, regex, replace, versionMapper) => {
if (!notes) {
return null;
}

if (Array.isArray(notes)) {
return /** @type {Notes} */ (
notes.map((note) => updateNotes(note, regex, replace, versionMapper))
);
const mapped = notes
.map((note) => updateNotes(note, regex, replace, versionMapper))
.filter(Boolean);

if (mapped.length === 0) {
return null;
}

if (mapped.length === 1) {
return /** @type {Notes} */ (mapped[0]);
}

return /** @type {Notes} */ (mapped);
}

return notes
let hasUnmappedVersion = false;
const result = notes
.replace(regex, replace)
.replace(
new RegExp(`(${replace}|version)\\s(\\d+)`, 'g'),
(match, p1, p2) => p1 + ' ' + versionMapper(p2),
(match, p1, p2) => {
const mapped = versionMapper(p2);
if (mapped === false) {
hasUnmappedVersion = true;
return match;
}
return p1 + ' ' + mapped;
},
);

return hasUnmappedVersion ? null : result;
};

/**
Expand Down Expand Up @@ -306,6 +326,8 @@ export const bumpSupport = (sourceData, sourceBrowser, destination) => {
);
if (newNotes) {
newData.notes = newNotes;
} else {
delete newData.notes;
}
}

Expand Down
71 changes: 70 additions & 1 deletion scripts/build/mirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import assert from 'node:assert/strict';

import bcd from '../../index.js';

import mirrorSupport, { isOSLimitation } from './mirror.js';
import mirrorSupport, { isOSLimitation, updateNotes } from './mirror.js';

describe('mirror', () => {
describe('isOSLimitation', () => {
Expand Down Expand Up @@ -172,6 +172,75 @@ describe('mirror', () => {
}
});

describe('updateNotes', () => {
/** @type {(string) => (string | false)} */
const versionMapper = (v) => (v === '99' ? false : String(Number(v) + 1));
const regex = /\bChrome\b/g;

it('notes with unmapped versions are filtered out', () => {
assert.equal(
updateNotes(
'Supported since Chrome 99.',
regex,
'Opera',
versionMapper,
),
null,
);
});

it('notes without version references are kept unchanged', () => {
assert.equal(
updateNotes(
'Supported on ChromeOS, macOS, and Windows.',
regex,
'Opera',
versionMapper,
),
'Supported on ChromeOS, macOS, and Windows.',
);
});

it('notes with mapped versions are updated', () => {
assert.equal(
updateNotes(
'Supported since Chrome 70.',
regex,
'Opera',
versionMapper,
),
'Supported since Opera 71.',
);
});

it('in an array, only notes with unmapped versions are filtered out', () => {
assert.deepEqual(
updateNotes(
[
'Supported on ChromeOS, macOS, and Windows.',
'Supported since Chrome 99.',
],
regex,
'Opera',
versionMapper,
),
'Supported on ChromeOS, macOS, and Windows.',
);
});

it('returns null when all array notes are filtered out', () => {
assert.equal(
updateNotes(
['Supported since Chrome 99.', 'Available since Chrome 99.'],
regex,
'Opera',
versionMapper,
),
null,
);
});
});

describe('Notes', () => {
it('version numbers in notes are mapped', () => {
/** @type {InternalSupportBlock} */
Expand Down
Loading