Skip to content

Commit

Permalink
[deps] Replace compare-versions with semver (elastic#195287)
Browse files Browse the repository at this point in the history
## Summary

We are reducing the number of dependencies by replacing the
`compare-versions` library with the already used `semver` library that
offer the same functionality.
  • Loading branch information
markov00 authored Oct 8, 2024
1 parent 187afe7 commit 31f4f2c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,6 @@
"classnames": "2.2.6",
"color": "^4.2.3",
"commander": "^4.1.1",
"compare-versions": "3.5.1",
"constate": "^3.3.2",
"copy-to-clipboard": "^3.0.8",
"core-js": "^3.37.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/kbn-sort-predicates/src/sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import versionCompare from 'compare-versions';
import valid from 'semver/functions/valid';
import semVerCompare from 'semver/functions/compare';
import semVerCoerce from 'semver/functions/coerce';
import ipaddr, { type IPv4, type IPv6 } from 'ipaddr.js';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
import moment from 'moment';
Expand Down Expand Up @@ -154,7 +155,9 @@ const versionComparison: CompareFn<string> = (v1, v2, direction) => {
if (bInvalid) {
return direction * -1;
}
return versionCompare(valueA, valueB);
const semVerValueA = semVerCoerce(valueA) ?? '';
const semVerValueB = semVerCoerce(valueB) ?? '';
return semVerCompare(semVerValueA, semVerValueB);
};

const openRange = { gte: -Infinity, lt: Infinity };
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/vis_types/vega/public/data_model/vega_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

import _ from 'lodash';
import schemaParser from 'vega-schema-url-parser';
import versionCompare from 'compare-versions';
import semVerCompare from 'semver/functions/compare';
import semVerCoerce from 'semver/functions/coerce';
import hjson from 'hjson';
import { euiPaletteColorBlind } from '@elastic/eui';
import { euiThemeVars } from '@kbn/ui-theme';
import { i18n } from '@kbn/i18n';

import { logger, Warn, None, version as vegaVersion } from 'vega';
import { compile, TopLevelSpec, version as vegaLiteVersion } from 'vega-lite';

import { EsQueryParser } from './es_query_parser';
import { Utils } from './utils';
import { EmsFileParser } from './ems_file_parser';
Expand Down Expand Up @@ -558,8 +560,10 @@ The URL is an identifier only. Kibana and your browser will never access this UR
const schema = schemaParser(spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLiteVersion : vegaVersion;
const schemaSemVer = semVerCoerce(schema.version) ?? '';
const libSemVersion = semVerCoerce(libVersion) ?? '';

if (versionCompare(schema.version, libVersion) > 0) {
if (semVerCompare(schemaSemVer, libSemVersion) > 0) {
this._onWarning(
i18n.translate(
'visTypeVega.vegaParser.notValidLibraryVersionForInputSpecWarningMessage',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/
import React, { useState, useMemo, useEffect } from 'react';
import compareVersions from 'compare-versions';
import { EuiSpacer } from '@elastic/eui';
import { useParams, useHistory, generatePath } from 'react-router-dom';
import type {
Expand All @@ -14,6 +13,8 @@ import type {
RuleStateAttributes,
} from '@kbn/cloud-security-posture-common/schema/rules/latest';
import { extractErrorMessage } from '@kbn/cloud-security-posture-common';
import semVerCompare from 'semver/functions/compare';
import semVerCoerce from 'semver/functions/coerce';
import { benchmarksNavigation } from '../../common/navigation/constants';
import { buildRuleKey } from '../../../common/utils/rules_states';
import { RulesTable } from './rules_table';
Expand Down Expand Up @@ -197,7 +198,9 @@ export const RulesContainer = () => {
return a.localeCompare(b, 'en', { sensitivity: 'base' });
});

const cleanedRuleNumberList = [...new Set(ruleNumberList)].sort(compareVersions);
const cleanedRuleNumberList = [...new Set(ruleNumberList)].sort((a, b) =>
semVerCompare(semVerCoerce(a) ?? '', semVerCoerce(b) ?? '')
);

const rulesPageData = useMemo(
() => getRulesPageData(filteredRulesWithStates, status, error, rulesQuery),
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14706,11 +14706,6 @@ commondir@^1.0.1:
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=

[email protected]:
version "3.5.1"
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-3.5.1.tgz#26e1f5cf0d48a77eced5046b9f67b6b61075a393"
integrity sha512-9fGPIB7C6AyM18CJJBHt5EnCZDG3oiTJYy0NjfIAGjKpzv0tkxWko7TNQHF5ymqm7IH03tqmeuBxtvD+Izh6mg==

compare-versions@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-6.1.0.tgz#3f2131e3ae93577df111dba133e6db876ffe127a"
Expand Down

0 comments on commit 31f4f2c

Please sign in to comment.