Skip to content

Commit 89e8f76

Browse files
committed
feat: add compareVersions, update registerMenuCommand
1 parent a0930aa commit 89e8f76

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

.github/workflows/npm-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: NPM Publish
22
on:
33
push:
44
tags:
5-
- '*.*.*'
5+
- "*.*.*"
66
jobs:
77
publish:
88
runs-on: ubuntu-latest

lib/index.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,19 @@ export function isUrl(text: string | undefined): boolean
142142
export function throttle(func: Function, interval: number): Function
143143

144144
export type MenuCallback = (event?: MouseEvent | KeyboardEvent) => void
145+
export type RegisterMenuCommandOptions = {
146+
id?: string
147+
title?: string
148+
autoClose?: boolean
149+
// O - Tampermonkey
150+
// X - Violentmonkey
151+
accessKey?: string
152+
}
145153
export function registerMenuCommand(
146154
name: string,
147155
callback: MenuCallback,
148-
accessKey?: string
149-
): void
156+
options?: RegisterMenuCommandOptions
157+
): string | undefined
150158

151159
export function extendHistoryApi(): void
152160

@@ -195,3 +203,5 @@ export function parseInt10(
195203

196204
// eslint-disable-next-line @typescript-eslint/naming-convention
197205
export function createHTML(html: string): string
206+
207+
export function compareVersions(v1: string, v2: string): number

lib/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,50 @@ const escapeHTMLPolicy =
499499
export const createHTML = (html) => {
500500
return escapeHTMLPolicy ? escapeHTMLPolicy.createHTML(html) : html
501501
}
502+
503+
/**
504+
* Compare two semantic version strings
505+
* @param {string} v1 - First version string (e.g., "1.2.0")
506+
* @param {string} v2 - Second version string (e.g., "1.1.5")
507+
* @returns {number} - Returns 1 if v1 > v2, -1 if v1 < v2, 0 if equal
508+
* @throws {Error} - Throws error for invalid version strings
509+
*/
510+
export function compareVersions(v1, v2) {
511+
// Input validation
512+
if (typeof v1 !== "string" || typeof v2 !== "string") {
513+
throw new TypeError("Version strings must be of type string")
514+
}
515+
516+
if (!v1.trim() || !v2.trim()) {
517+
throw new Error("Version strings cannot be empty")
518+
}
519+
520+
// Validate version format (basic semantic versioning)
521+
const versionRegex = /^\d+(\.\d+)*$/
522+
if (!versionRegex.test(v1) || !versionRegex.test(v2)) {
523+
throw new Error(
524+
"Invalid version format. Use semantic versioning (e.g., '1.2.3')"
525+
)
526+
}
527+
528+
const v1Parts = v1.split(".").map(Number)
529+
const v2Parts = v2.split(".").map(Number)
530+
const maxLength = Math.max(v1Parts.length, v2Parts.length)
531+
532+
for (let i = 0; i < maxLength; i++) {
533+
const num1 = v1Parts[i] || 0 // Use logical OR for cleaner default assignment
534+
const num2 = v2Parts[i] || 0
535+
536+
if (num1 !== num2) {
537+
return num1 > num2 ? 1 : -1 // Simplified comparison
538+
}
539+
}
540+
541+
return 0 // Versions are equal
542+
}
543+
544+
// Usage:
545+
// console.log(compareVersions("1.2.0", "1.1.5")); // Output: 1
546+
// console.log(compareVersions("1.0", "1.0.0")); // Output: 0
547+
// console.log(compareVersions("2.0", "1.5.10")); // Output: 1
548+
// console.log(compareVersions("1.0.0", "1.0.0.1")); // Output: -1

lib/userscript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const addStyle = (styleText) =>
7171
addElement(null, "style", { textContent: styleText })
7272

7373
// Only register menu on top frame
74-
export const registerMenuCommand = (name, callback, accessKey) => {
74+
export const registerMenuCommand = (name, callback, options) => {
7575
if (globalThis !== top) {
7676
return
7777
}
@@ -81,6 +81,6 @@ export const registerMenuCommand = (name, callback, accessKey) => {
8181
return
8282
}
8383

84-
GM.registerMenuCommand(name, callback, accessKey)
84+
return GM.registerMenuCommand(name, callback, options)
8585
}
8686
/* eslint-enable new-cap, camelcase */

0 commit comments

Comments
 (0)