@naturalcycles/js-lib
Advanced tools
Comparing version 14.238.0 to 14.239.0
@@ -38,3 +38,23 @@ import { _range } from './array/range'; | ||
} | ||
static of(input) { | ||
/** | ||
* Returns 1 if this > other | ||
* returns 0 if they are equal | ||
* returns -1 if this < other | ||
*/ | ||
cmp(other) { | ||
const { tokens } = semver2.of(other); | ||
for (let i = 0; i < 3; i++) { | ||
if (this.tokens[i] < tokens[i]) | ||
return -1; | ||
if (this.tokens[i] > tokens[i]) | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
toString() { | ||
return this.tokens.join('.'); | ||
} | ||
} | ||
class SemverFactory { | ||
of(input) { | ||
const s = this.parseOrNull(input); | ||
@@ -47,3 +67,3 @@ _assert(s !== null, `Cannot parse "${input}" into Semver`, { | ||
} | ||
static parseOrNull(input) { | ||
parseOrNull(input) { | ||
if (!input) | ||
@@ -57,27 +77,36 @@ return null; | ||
/** | ||
* Returns 1 if this > other | ||
* returns 0 if they are equal | ||
* returns -1 if this < other | ||
* Returns the highest (max) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
cmp(other) { | ||
const { tokens } = Semver.of(other); | ||
for (let i = 0; i < 3; i++) { | ||
if (this.tokens[i] < tokens[i]) | ||
return -1; | ||
if (this.tokens[i] > tokens[i]) | ||
return 1; | ||
} | ||
return 0; | ||
maxOrUndefined(items) { | ||
return items.length ? this.max(items) : undefined; | ||
} | ||
toString() { | ||
return this.tokens.join('.'); | ||
/** | ||
* Returns the highest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
max(items) { | ||
_assert(items.length, 'semver.max called on empty array'); | ||
return items.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item)); | ||
} | ||
/** | ||
* Returns the lowest (min) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
minOrUndefined(items) { | ||
return items.length ? this.min(items) : undefined; | ||
} | ||
/** | ||
* Returns the lowest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
min(items) { | ||
_assert(items.length, 'semver.min called on empty array'); | ||
return items.map(i => this.of(i)).reduce((min, item) => (min.isSameOrBefore(item) ? min : item)); | ||
} | ||
} | ||
const semverFactory = new SemverFactory(); | ||
export const semver2 = semverFactory.of.bind(semverFactory); | ||
// The line below is the blackest of black magic I have ever written in 2024. | ||
// And probably 2023 as well. | ||
Object.setPrototypeOf(semver2, semverFactory); | ||
/** | ||
* Shortcut for Semver.of(input) | ||
*/ | ||
export function _semver(input) { | ||
return Semver.of(input); | ||
} | ||
/** | ||
* Returns 1 if a > b | ||
@@ -91,3 +120,3 @@ * returns 0 if they are equal | ||
*/ | ||
export function _semverCompare(a, b) { | ||
export function _quickSemverCompare(a, b) { | ||
const t1 = a.split('.'); | ||
@@ -94,0 +123,0 @@ const t2 = b.split('.'); |
@@ -21,8 +21,6 @@ export type SemverInput = string | Semver; | ||
tokens: SemverTokens; | ||
private constructor(); | ||
constructor(tokens: SemverTokens); | ||
get major(): number; | ||
get minor(): number; | ||
get patch(): number; | ||
static of(input: SemverInput): Semver; | ||
static parseOrNull(input: SemverInput | undefined | null): Semver | null; | ||
isAfter: (other: SemverInput) => boolean; | ||
@@ -42,7 +40,29 @@ isSameOrAfter: (other: SemverInput) => boolean; | ||
} | ||
declare class SemverFactory { | ||
of(input: SemverInput): Semver; | ||
parseOrNull(input: SemverInput | undefined | null): Semver | null; | ||
/** | ||
* Returns the highest (max) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
maxOrUndefined(items: SemverInput[]): Semver | undefined; | ||
/** | ||
* Returns the highest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
max(items: SemverInput[]): Semver; | ||
/** | ||
* Returns the lowest (min) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
minOrUndefined(items: SemverInput[]): Semver | undefined; | ||
/** | ||
* Returns the lowest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
min(items: SemverInput[]): Semver; | ||
} | ||
interface SemverFn extends SemverFactory { | ||
(input: SemverInput): Semver; | ||
} | ||
export declare const semver2: SemverFn; | ||
/** | ||
* Shortcut for Semver.of(input) | ||
*/ | ||
export declare function _semver(input: SemverInput): Semver; | ||
/** | ||
* Returns 1 if a > b | ||
@@ -56,2 +76,3 @@ * returns 0 if they are equal | ||
*/ | ||
export declare function _semverCompare(a: string, b: string): -1 | 0 | 1; | ||
export declare function _quickSemverCompare(a: string, b: string): -1 | 0 | 1; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports._semverCompare = exports._semver = exports.Semver = void 0; | ||
exports._quickSemverCompare = exports.semver2 = exports.Semver = void 0; | ||
const range_1 = require("./array/range"); | ||
@@ -41,18 +41,2 @@ const assert_1 = require("./error/assert"); | ||
} | ||
static of(input) { | ||
const s = this.parseOrNull(input); | ||
(0, assert_1._assert)(s !== null, `Cannot parse "${input}" into Semver`, { | ||
userFriendly: true, | ||
input, | ||
}); | ||
return s; | ||
} | ||
static parseOrNull(input) { | ||
if (!input) | ||
return null; | ||
if (input instanceof Semver) | ||
return input; | ||
const t = input.split('.'); | ||
return new Semver((0, range_1._range)(3).map(i => parseInt(t[i]) || 0)); | ||
} | ||
/** | ||
@@ -64,3 +48,3 @@ * Returns 1 if this > other | ||
cmp(other) { | ||
const { tokens } = Semver.of(other); | ||
const { tokens } = exports.semver2.of(other); | ||
for (let i = 0; i < 3; i++) { | ||
@@ -79,9 +63,53 @@ if (this.tokens[i] < tokens[i]) | ||
exports.Semver = Semver; | ||
/** | ||
* Shortcut for Semver.of(input) | ||
*/ | ||
function _semver(input) { | ||
return Semver.of(input); | ||
class SemverFactory { | ||
of(input) { | ||
const s = this.parseOrNull(input); | ||
(0, assert_1._assert)(s !== null, `Cannot parse "${input}" into Semver`, { | ||
userFriendly: true, | ||
input, | ||
}); | ||
return s; | ||
} | ||
parseOrNull(input) { | ||
if (!input) | ||
return null; | ||
if (input instanceof Semver) | ||
return input; | ||
const t = input.split('.'); | ||
return new Semver((0, range_1._range)(3).map(i => parseInt(t[i]) || 0)); | ||
} | ||
/** | ||
* Returns the highest (max) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
maxOrUndefined(items) { | ||
return items.length ? this.max(items) : undefined; | ||
} | ||
/** | ||
* Returns the highest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
max(items) { | ||
(0, assert_1._assert)(items.length, 'semver.max called on empty array'); | ||
return items.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item)); | ||
} | ||
/** | ||
* Returns the lowest (min) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
minOrUndefined(items) { | ||
return items.length ? this.min(items) : undefined; | ||
} | ||
/** | ||
* Returns the lowest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
min(items) { | ||
(0, assert_1._assert)(items.length, 'semver.min called on empty array'); | ||
return items.map(i => this.of(i)).reduce((min, item) => (min.isSameOrBefore(item) ? min : item)); | ||
} | ||
} | ||
exports._semver = _semver; | ||
const semverFactory = new SemverFactory(); | ||
exports.semver2 = semverFactory.of.bind(semverFactory); | ||
// The line below is the blackest of black magic I have ever written in 2024. | ||
// And probably 2023 as well. | ||
Object.setPrototypeOf(exports.semver2, semverFactory); | ||
/** | ||
@@ -96,3 +124,3 @@ * Returns 1 if a > b | ||
*/ | ||
function _semverCompare(a, b) { | ||
function _quickSemverCompare(a, b) { | ||
const t1 = a.split('.'); | ||
@@ -108,2 +136,2 @@ const t2 = b.split('.'); | ||
} | ||
exports._semverCompare = _semverCompare; | ||
exports._quickSemverCompare = _quickSemverCompare; |
{ | ||
"name": "@naturalcycles/js-lib", | ||
"version": "14.238.0", | ||
"version": "14.239.0", | ||
"scripts": { | ||
@@ -24,2 +24,3 @@ "prepare": "husky", | ||
"@types/node": "^20.1.0", | ||
"@types/semver": "^7.5.8", | ||
"crypto-js": "^4.1.1", | ||
@@ -26,0 +27,0 @@ "jest": "^29.0.0", |
@@ -24,3 +24,3 @@ import { _range } from './array/range' | ||
export class Semver { | ||
private constructor(public tokens: SemverTokens) {} | ||
constructor(public tokens: SemverTokens) {} | ||
@@ -37,21 +37,2 @@ get major(): number { | ||
static of(input: SemverInput): Semver { | ||
const s = this.parseOrNull(input) | ||
_assert(s !== null, `Cannot parse "${input}" into Semver`, { | ||
userFriendly: true, | ||
input, | ||
}) | ||
return s | ||
} | ||
static parseOrNull(input: SemverInput | undefined | null): Semver | null { | ||
if (!input) return null | ||
if (input instanceof Semver) return input | ||
const t = input.split('.') | ||
return new Semver(_range(3).map(i => parseInt(t[i]!) || 0) as SemverTokens) | ||
} | ||
isAfter = (other: SemverInput): boolean => this.cmp(other) > 0 | ||
@@ -69,3 +50,3 @@ isSameOrAfter = (other: SemverInput): boolean => this.cmp(other) >= 0 | ||
cmp(other: SemverInput): -1 | 0 | 1 { | ||
const { tokens } = Semver.of(other) | ||
const { tokens } = semver2.of(other) | ||
for (let i = 0; i < 3; i++) { | ||
@@ -85,9 +66,67 @@ if (this.tokens[i]! < tokens[i]!) return -1 | ||
/** | ||
* Shortcut for Semver.of(input) | ||
*/ | ||
export function _semver(input: SemverInput): Semver { | ||
return Semver.of(input) | ||
class SemverFactory { | ||
of(input: SemverInput): Semver { | ||
const s = this.parseOrNull(input) | ||
_assert(s !== null, `Cannot parse "${input}" into Semver`, { | ||
userFriendly: true, | ||
input, | ||
}) | ||
return s | ||
} | ||
parseOrNull(input: SemverInput | undefined | null): Semver | null { | ||
if (!input) return null | ||
if (input instanceof Semver) return input | ||
const t = input.split('.') | ||
return new Semver(_range(3).map(i => parseInt(t[i]!) || 0) as SemverTokens) | ||
} | ||
/** | ||
* Returns the highest (max) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
maxOrUndefined(items: SemverInput[]): Semver | undefined { | ||
return items.length ? this.max(items) : undefined | ||
} | ||
/** | ||
* Returns the highest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
max(items: SemverInput[]): Semver { | ||
_assert(items.length, 'semver.max called on empty array') | ||
return items.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item)) | ||
} | ||
/** | ||
* Returns the lowest (min) Semver from the array, or undefined if the array is empty. | ||
*/ | ||
minOrUndefined(items: SemverInput[]): Semver | undefined { | ||
return items.length ? this.min(items) : undefined | ||
} | ||
/** | ||
* Returns the lowest Semver from the array. | ||
* Throws if the array is empty. | ||
*/ | ||
min(items: SemverInput[]): Semver { | ||
_assert(items.length, 'semver.min called on empty array') | ||
return items.map(i => this.of(i)).reduce((min, item) => (min.isSameOrBefore(item) ? min : item)) | ||
} | ||
} | ||
interface SemverFn extends SemverFactory { | ||
(input: SemverInput): Semver | ||
} | ||
const semverFactory = new SemverFactory() | ||
export const semver2 = semverFactory.of.bind(semverFactory) as SemverFn | ||
// The line below is the blackest of black magic I have ever written in 2024. | ||
// And probably 2023 as well. | ||
Object.setPrototypeOf(semver2, semverFactory) | ||
/** | ||
@@ -102,3 +141,3 @@ * Returns 1 if a > b | ||
*/ | ||
export function _semverCompare(a: string, b: string): -1 | 0 | 1 { | ||
export function _quickSemverCompare(a: string, b: string): -1 | 0 | 1 { | ||
const t1 = a.split('.') | ||
@@ -105,0 +144,0 @@ const t2 = b.split('.') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1048540
31545
12