Skip to content
Merged
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
10 changes: 3 additions & 7 deletions src/generators/jsx-ast/utils/buildContent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { buildMetaBarProps } from './buildBarProps.mjs';
import createPropertyTable from './buildPropertyTable.mjs';
import { DOC_NODE_BLOB_BASE_URL } from '../../../constants.mjs';
import { enforceArray } from '../../../utils/array.mjs';
import { sortChanges } from '../../../utils/generators.mjs';
import createQueries from '../../../utils/queries/index.mjs';
import { JSX_IMPORTS } from '../../web/constants.mjs';
import {
Expand Down Expand Up @@ -53,17 +52,14 @@ export const gatherChangeEntries = (entry, remark) => {
* @param {import('unified').Processor} remark - The remark processor
*/
export const createChangeElement = (entry, remark) => {
const changeEntries = gatherChangeEntries(entry, remark);
const changes = gatherChangeEntries(entry, remark);

if (!changeEntries.length) {
if (!changes.length) {
return null;
}

// Sort changes by versions and reverse for newest first
const sortedChanges = sortChanges(changeEntries, 'versions');

return createJSXElement(JSX_IMPORTS.ChangeHistory.name, {
changes: sortedChanges,
changes,
className: 'change-history',
});
};
Expand Down
4 changes: 1 addition & 3 deletions src/metadata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import { u as createTree } from 'unist-builder';

import { sortChanges } from './utils/generators.mjs';

/**
* This method allows us to handle creation of Metadata entries
* within the current scope of API docs being parsed
Expand Down Expand Up @@ -131,7 +129,7 @@ const createMetadata = slugger => {
removed_in: removed,
n_api_version: napiVersion,
updates,
changes: sortChanges(changes),
changes,
heading: internalMetadata.heading,
stability: internalMetadata.stability,
content: section,
Expand Down
35 changes: 0 additions & 35 deletions src/utils/__tests__/generators.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getVersionFromSemVer,
coerceSemVer,
getCompatibleVersions,
sortChanges,
} from '../generators.mjs';

describe('groupNodesByModule', () => {
Expand Down Expand Up @@ -80,37 +79,3 @@ describe('getCompatibleVersions', () => {
assert.equal(result.length, 2);
});
});

describe('sortChanges', () => {
it('sorts changes by version', () => {
const changes = [
{ version: '18.5.0' },
{ version: '16.2.0' },
{ version: '20.1.0' },
];

const result = sortChanges(changes);
assert.equal(result[0].version, '20.1.0');
assert.equal(result[1].version, '18.5.0');
assert.equal(result[2].version, '16.2.0');
});

it('handles array versions', () => {
const changes = [
{ version: ['18.5.0', '18.4.0'] },
{ version: ['16.2.0'] },
];

const result = sortChanges(changes);
assert.equal(result[0].version[0], '18.5.0');
assert.equal(result[1].version[0], '16.2.0');
});

it('sorts by custom key', () => {
const changes = [{ customVersion: '18.0.0' }, { customVersion: '16.0.0' }];

const result = sortChanges(changes, 'customVersion');
assert.equal(result[0].customVersion, '18.0.0');
assert.equal(result[1].customVersion, '16.0.0');
});
});
19 changes: 1 addition & 18 deletions src/utils/generators.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { coerce, compare, major } from 'semver';
import { coerce, major } from 'semver';

import { DOC_API_BASE_URL_VERSION } from '../constants.mjs';

Expand Down Expand Up @@ -80,23 +80,6 @@ export const getCompatibleVersions = (introduced, releases) => {
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.
Expand Down
Loading