Skip to content

Commit

Permalink
refactor: move versionCompare to internal helpers (#1256)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck authored Jan 16, 2025
1 parent 39544bf commit bd4d13a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 43 deletions.
25 changes: 25 additions & 0 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,28 @@ export function getMimeForLicenseFile (filename: string): MimeType | undefined {
}

// endregion MIME

// region version compare

type Version = number[]
type CompareResult = -1 | 0 | 1

export function versionCompare (a: Version, b: Version): CompareResult {
let ai: number, bi: number
for (let i = 0, l = Math.max(a.length, b.length); i < l; ++i) {
// make values NaN-save, null-safe, undefined-safe
ai = a[i] || 0 /* eslint-disable-line @typescript-eslint/strict-boolean-expressions */
bi = b[i] || 0 /* eslint-disable-line @typescript-eslint/strict-boolean-expressions */
if (ai < bi) {
// A < B
return -1
}
if (ai > bi) {
// A > B
return +1
}
}
// A == B
return 0
}
// endregion version compare
4 changes: 2 additions & 2 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import {
isString,
loadJsonFile,
structuredClonePolyfill,
tryRemoveSecretsFromUrl
tryRemoveSecretsFromUrl,
versionCompare
} from './_helpers'
import { makeNpmRunner, type runFunc } from './npmRunner'
import { PropertyNames, PropertyValueBool } from './properties'
import { versionCompare } from './versionCompare'

type OmittableDependencyTypes = 'dev' | 'optional' | 'peer'

Expand Down
40 changes: 0 additions & 40 deletions src/versionCompare.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/unit/versionCompare.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.

const { describe, expect, test } = require('@jest/globals')

const { versionCompare } = require('../../dist/versionCompare')
const { versionCompare } = require('../../dist/_helpers')

describe('versionCompare', () => {
test.each([
Expand Down

0 comments on commit bd4d13a

Please sign in to comment.