Skip to content

Commit

Permalink
refactor(semver): remove isOperator() and cleanup isComparator() (#…
Browse files Browse the repository at this point in the history
…4038)

refactor: remove `semver.isOperator()`
  • Loading branch information
iuioiua authored Dec 30, 2023
1 parent 756ddf3 commit c9f4020
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 62 deletions.
14 changes: 14 additions & 0 deletions semver/_constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

export const OPERATORS = [
"",
"=",
"==",
"===",
"!==",
"!=",
">",
">=",
"<",
"<=",
] as const;
26 changes: 0 additions & 26 deletions semver/_shared.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { Operator } from "./types.ts";

export function compareNumber(
a: number,
b: number,
Expand Down Expand Up @@ -186,30 +184,6 @@ export function isValidString(value: unknown): value is string {
);
}

export const operators = [
"",
"=",
"==",
"===",
"!==",
"!=",
">",
">=",
"<",
"<=",
] as const;

/**
* Checks to see if the value is a valid Operator string.
*
* Adds a type assertion if true.
* @param value The value to check
* @returns True if the value is a valid Operator string otherwise false.
*/
export function isOperator(value: unknown): value is Operator {
return operators.includes(value as Operator);
}

const NUMERIC_IDENTIFIER_REGEXP = new RegExp(`^(${NUMERIC_IDENTIFIER})$`);
export function parsePrerelease(prerelease: string) {
return prerelease
Expand Down
14 changes: 7 additions & 7 deletions semver/is_comparator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { isSemVer } from "./is_semver.ts";
import { isOperator } from "./_shared.ts";
import { OPERATORS } from "./_constants.ts";
import type { Comparator } from "./types.ts";
import { ALL, NONE } from "./constants.ts";

Expand All @@ -15,14 +15,14 @@ import { ALL, NONE } from "./constants.ts";
* @returns True if the object is a Comparator otherwise false
*/
export function isComparator(value: unknown): value is Comparator {
if (value === null || value === undefined) return false;
if (value === NONE) return true;
if (value === ALL) return true;
if (Array.isArray(value)) return false;
if (typeof value !== "object") return false;
if (
value === null || value === undefined || Array.isArray(value) ||
typeof value !== "object"
) return false;
if (value === NONE || value === ALL) return true;
const { operator, semver } = value as Comparator;
return (
isOperator(operator) &&
OPERATORS.includes(operator) &&
isSemVer(semver)
);
}
27 changes: 0 additions & 27 deletions semver/is_valid_operator_test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions semver/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { operators } from "./_shared.ts";
import { OPERATORS } from "./_constants.ts";

/**
* The possible release types are used as an operator for the
Expand All @@ -19,7 +19,7 @@ export type ReleaseType =
/**
* SemVer comparison operators.
*/
export type Operator = typeof operators[number];
export type Operator = typeof OPERATORS[number];

/**
* The style to use when formatting a SemVer object into a string
Expand Down

0 comments on commit c9f4020

Please sign in to comment.