@naturalcycles/js-lib 14.246.1 → 14.247.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/datetime/localDate.d.ts +5 -0
- package/dist/datetime/localDate.js +15 -0
- package/dist/datetime/localTime.d.ts +2 -0
- package/dist/datetime/localTime.js +6 -0
- package/dist/promise/pMap.js +1 -1
- package/dist/promise/pQueue.js +1 -1
- package/dist/promise/pTimeout.d.ts +1 -1
- package/dist/promise/pTimeout.js +2 -0
- package/dist/semver.d.ts +3 -3
- package/dist/semver.js +18 -16
- package/dist/string/pupa.js +0 -1
- package/dist-esm/datetime/localDate.js +15 -0
- package/dist-esm/datetime/localTime.js +6 -0
- package/dist-esm/promise/pMap.js +1 -1
- package/dist-esm/promise/pQueue.js +1 -1
- package/dist-esm/promise/pTimeout.js +2 -0
- package/dist-esm/semver.js +18 -16
- package/dist-esm/string/pupa.js +0 -1
- package/package.json +2 -2
- package/src/datetime/localDate.ts +16 -0
- package/src/datetime/localTime.ts +7 -0
- package/src/promise/pMap.ts +1 -1
- package/src/promise/pQueue.ts +1 -1
- package/src/promise/pTimeout.ts +3 -2
- package/src/semver.ts +18 -16
- package/src/string/pupa.ts +0 -1
|
@@ -55,6 +55,11 @@ export declare class LocalDate {
|
|
|
55
55
|
* Checks if this localDate is same or younger (>=) than "today" by X units.
|
|
56
56
|
*/
|
|
57
57
|
isSameOrYoungerThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean;
|
|
58
|
+
isToday(): boolean;
|
|
59
|
+
isAfterToday(): boolean;
|
|
60
|
+
isSameOrAfterToday(): boolean;
|
|
61
|
+
isBeforeToday(): boolean;
|
|
62
|
+
isSameOrBeforeToday(): boolean;
|
|
58
63
|
getAgeInYears(today?: LocalDateInput): number;
|
|
59
64
|
getAgeInMonths(today?: LocalDateInput): number;
|
|
60
65
|
getAgeInDays(today?: LocalDateInput): number;
|
|
@@ -113,6 +113,21 @@ class LocalDate {
|
|
|
113
113
|
isSameOrYoungerThan(n, unit, today) {
|
|
114
114
|
return this.isSameOrAfter(exports.localDate.fromInput(today || new Date()).plus(-n, unit));
|
|
115
115
|
}
|
|
116
|
+
isToday() {
|
|
117
|
+
return this.isSame(exports.localDate.today());
|
|
118
|
+
}
|
|
119
|
+
isAfterToday() {
|
|
120
|
+
return this.isAfter(exports.localDate.today());
|
|
121
|
+
}
|
|
122
|
+
isSameOrAfterToday() {
|
|
123
|
+
return this.isSameOrAfter(exports.localDate.today());
|
|
124
|
+
}
|
|
125
|
+
isBeforeToday() {
|
|
126
|
+
return this.isBefore(exports.localDate.today());
|
|
127
|
+
}
|
|
128
|
+
isSameOrBeforeToday() {
|
|
129
|
+
return this.isSameOrBefore(exports.localDate.today());
|
|
130
|
+
}
|
|
116
131
|
getAgeInYears(today) {
|
|
117
132
|
return this.getAgeIn('year', today);
|
|
118
133
|
}
|
|
@@ -172,6 +172,8 @@ export declare class LocalTime {
|
|
|
172
172
|
getAgeInMinutes(now?: LocalTimeInput): number;
|
|
173
173
|
getAgeInSeconds(now?: LocalTimeInput): number;
|
|
174
174
|
getAgeIn(unit: LocalTimeUnit, now?: LocalTimeInput): number;
|
|
175
|
+
isAfterNow(): boolean;
|
|
176
|
+
isBeforeNow(): boolean;
|
|
175
177
|
/**
|
|
176
178
|
* Returns 1 if this > d
|
|
177
179
|
* returns 0 if they are equal
|
|
@@ -479,6 +479,12 @@ class LocalTime {
|
|
|
479
479
|
getAgeIn(unit, now) {
|
|
480
480
|
return exports.localTime.fromInput(now ?? new Date()).diff(this, unit);
|
|
481
481
|
}
|
|
482
|
+
isAfterNow() {
|
|
483
|
+
return this.$date.valueOf() > Date.now();
|
|
484
|
+
}
|
|
485
|
+
isBeforeNow() {
|
|
486
|
+
return this.$date.valueOf() < Date.now();
|
|
487
|
+
}
|
|
482
488
|
/**
|
|
483
489
|
* Returns 1 if this > d
|
|
484
490
|
* returns 0 if they are equal
|
package/dist/promise/pMap.js
CHANGED
package/dist/promise/pQueue.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ErrorData } from '../error/error.model';
|
|
2
2
|
import { TimeoutError } from '../error/error.util';
|
|
3
|
-
import
|
|
3
|
+
import { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
|
|
4
4
|
export interface PTimeoutOptions {
|
|
5
5
|
/**
|
|
6
6
|
* Timeout in milliseconds.
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.pTimeoutFn = pTimeoutFn;
|
|
4
4
|
exports.pTimeout = pTimeout;
|
|
5
5
|
const error_util_1 = require("../error/error.util");
|
|
6
|
+
const types_1 = require("../types");
|
|
6
7
|
/**
|
|
7
8
|
* Decorates a Function with a timeout.
|
|
8
9
|
* Returns a decorated Function.
|
|
@@ -44,6 +45,7 @@ async function pTimeout(fn, opt) {
|
|
|
44
45
|
resolve(onTimeout(err));
|
|
45
46
|
}
|
|
46
47
|
catch (err) {
|
|
48
|
+
(0, types_1._typeCast)(err);
|
|
47
49
|
// keep original stack
|
|
48
50
|
err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
|
|
49
51
|
reject((0, error_util_1._errorDataAppend)(err, opt.errorData));
|
package/dist/semver.d.ts
CHANGED
|
@@ -34,13 +34,13 @@ export declare class Semver {
|
|
|
34
34
|
* returns 0 if they are equal
|
|
35
35
|
* returns -1 if this < other
|
|
36
36
|
*/
|
|
37
|
-
|
|
37
|
+
compare(other: SemverInput): -1 | 0 | 1;
|
|
38
38
|
toJSON: () => string;
|
|
39
39
|
toString(): string;
|
|
40
40
|
}
|
|
41
41
|
declare class SemverFactory {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
fromInput(input: SemverInput): Semver;
|
|
43
|
+
fromInputOrUndefined(input: SemverInputNullable): Semver | undefined;
|
|
44
44
|
/**
|
|
45
45
|
* Returns the highest (max) Semver from the array, or undefined if the array is empty.
|
|
46
46
|
*/
|
package/dist/semver.js
CHANGED
|
@@ -24,11 +24,11 @@ const is_util_1 = require("./is.util");
|
|
|
24
24
|
class Semver {
|
|
25
25
|
constructor(tokens) {
|
|
26
26
|
this.tokens = tokens;
|
|
27
|
-
this.isAfter = (other) => this.
|
|
28
|
-
this.isSameOrAfter = (other) => this.
|
|
29
|
-
this.isBefore = (other) => this.
|
|
30
|
-
this.isSameOrBefore = (other) => this.
|
|
31
|
-
this.isSame = (other) => this.
|
|
27
|
+
this.isAfter = (other) => this.compare(other) > 0;
|
|
28
|
+
this.isSameOrAfter = (other) => this.compare(other) >= 0;
|
|
29
|
+
this.isBefore = (other) => this.compare(other) < 0;
|
|
30
|
+
this.isSameOrBefore = (other) => this.compare(other) <= 0;
|
|
31
|
+
this.isSame = (other) => this.compare(other) === 0;
|
|
32
32
|
this.toJSON = () => this.toString();
|
|
33
33
|
}
|
|
34
34
|
get major() {
|
|
@@ -45,8 +45,8 @@ class Semver {
|
|
|
45
45
|
* returns 0 if they are equal
|
|
46
46
|
* returns -1 if this < other
|
|
47
47
|
*/
|
|
48
|
-
|
|
49
|
-
const { tokens } = exports.semver2.
|
|
48
|
+
compare(other) {
|
|
49
|
+
const { tokens } = exports.semver2.fromInput(other);
|
|
50
50
|
for (let i = 0; i < 3; i++) {
|
|
51
51
|
if (this.tokens[i] < tokens[i])
|
|
52
52
|
return -1;
|
|
@@ -61,16 +61,16 @@ class Semver {
|
|
|
61
61
|
}
|
|
62
62
|
exports.Semver = Semver;
|
|
63
63
|
class SemverFactory {
|
|
64
|
-
|
|
65
|
-
const s = this.
|
|
66
|
-
(0, assert_1._assert)(s
|
|
64
|
+
fromInput(input) {
|
|
65
|
+
const s = this.fromInputOrUndefined(input);
|
|
66
|
+
(0, assert_1._assert)(s, `Cannot parse "${input}" into Semver`, {
|
|
67
67
|
input,
|
|
68
68
|
});
|
|
69
69
|
return s;
|
|
70
70
|
}
|
|
71
|
-
|
|
71
|
+
fromInputOrUndefined(input) {
|
|
72
72
|
if (!input)
|
|
73
|
-
return
|
|
73
|
+
return;
|
|
74
74
|
if (input instanceof Semver)
|
|
75
75
|
return input;
|
|
76
76
|
const t = input.split('.');
|
|
@@ -89,7 +89,9 @@ class SemverFactory {
|
|
|
89
89
|
max(items) {
|
|
90
90
|
const items2 = items.filter(is_util_1._isTruthy);
|
|
91
91
|
(0, assert_1._assert)(items2.length, 'semver.max called on empty array');
|
|
92
|
-
return items2
|
|
92
|
+
return items2
|
|
93
|
+
.map(i => this.fromInput(i))
|
|
94
|
+
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
93
95
|
}
|
|
94
96
|
/**
|
|
95
97
|
* Returns the lowest (min) Semver from the array, or undefined if the array is empty.
|
|
@@ -105,7 +107,7 @@ class SemverFactory {
|
|
|
105
107
|
const items2 = items.filter(is_util_1._isTruthy);
|
|
106
108
|
(0, assert_1._assert)(items2.length, 'semver.min called on empty array');
|
|
107
109
|
return items2
|
|
108
|
-
.map(i => this.
|
|
110
|
+
.map(i => this.fromInput(i))
|
|
109
111
|
.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
|
|
110
112
|
}
|
|
111
113
|
/**
|
|
@@ -113,11 +115,11 @@ class SemverFactory {
|
|
|
113
115
|
*/
|
|
114
116
|
sort(items, dir = 'asc', mutate = false) {
|
|
115
117
|
const mod = dir === 'desc' ? -1 : 1;
|
|
116
|
-
return (mutate ? items : [...items]).sort((a, b) => a.
|
|
118
|
+
return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
121
|
const semverFactory = new SemverFactory();
|
|
120
|
-
exports.semver2 = semverFactory.
|
|
122
|
+
exports.semver2 = semverFactory.fromInput.bind(semverFactory);
|
|
121
123
|
// The line below is the blackest of black magic I have ever written in 2024.
|
|
122
124
|
// And probably 2023 as well.
|
|
123
125
|
Object.setPrototypeOf(exports.semver2, semverFactory);
|
package/dist/string/pupa.js
CHANGED
|
@@ -110,6 +110,21 @@ export class LocalDate {
|
|
|
110
110
|
isSameOrYoungerThan(n, unit, today) {
|
|
111
111
|
return this.isSameOrAfter(localDate.fromInput(today || new Date()).plus(-n, unit));
|
|
112
112
|
}
|
|
113
|
+
isToday() {
|
|
114
|
+
return this.isSame(localDate.today());
|
|
115
|
+
}
|
|
116
|
+
isAfterToday() {
|
|
117
|
+
return this.isAfter(localDate.today());
|
|
118
|
+
}
|
|
119
|
+
isSameOrAfterToday() {
|
|
120
|
+
return this.isSameOrAfter(localDate.today());
|
|
121
|
+
}
|
|
122
|
+
isBeforeToday() {
|
|
123
|
+
return this.isBefore(localDate.today());
|
|
124
|
+
}
|
|
125
|
+
isSameOrBeforeToday() {
|
|
126
|
+
return this.isSameOrBefore(localDate.today());
|
|
127
|
+
}
|
|
113
128
|
getAgeInYears(today) {
|
|
114
129
|
return this.getAgeIn('year', today);
|
|
115
130
|
}
|
|
@@ -476,6 +476,12 @@ export class LocalTime {
|
|
|
476
476
|
getAgeIn(unit, now) {
|
|
477
477
|
return localTime.fromInput(now ?? new Date()).diff(this, unit);
|
|
478
478
|
}
|
|
479
|
+
isAfterNow() {
|
|
480
|
+
return this.$date.valueOf() > Date.now();
|
|
481
|
+
}
|
|
482
|
+
isBeforeNow() {
|
|
483
|
+
return this.$date.valueOf() < Date.now();
|
|
484
|
+
}
|
|
479
485
|
/**
|
|
480
486
|
* Returns 1 if this > d
|
|
481
487
|
* returns 0 if they are equal
|
package/dist-esm/promise/pMap.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _errorDataAppend, TimeoutError } from '../error/error.util';
|
|
2
|
+
import { _typeCast } from '../types';
|
|
2
3
|
/**
|
|
3
4
|
* Decorates a Function with a timeout.
|
|
4
5
|
* Returns a decorated Function.
|
|
@@ -40,6 +41,7 @@ export async function pTimeout(fn, opt) {
|
|
|
40
41
|
resolve(onTimeout(err));
|
|
41
42
|
}
|
|
42
43
|
catch (err) {
|
|
44
|
+
_typeCast(err);
|
|
43
45
|
// keep original stack
|
|
44
46
|
err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
|
|
45
47
|
reject(_errorDataAppend(err, opt.errorData));
|
package/dist-esm/semver.js
CHANGED
|
@@ -20,11 +20,11 @@ import { _isTruthy } from './is.util';
|
|
|
20
20
|
export class Semver {
|
|
21
21
|
constructor(tokens) {
|
|
22
22
|
this.tokens = tokens;
|
|
23
|
-
this.isAfter = (other) => this.
|
|
24
|
-
this.isSameOrAfter = (other) => this.
|
|
25
|
-
this.isBefore = (other) => this.
|
|
26
|
-
this.isSameOrBefore = (other) => this.
|
|
27
|
-
this.isSame = (other) => this.
|
|
23
|
+
this.isAfter = (other) => this.compare(other) > 0;
|
|
24
|
+
this.isSameOrAfter = (other) => this.compare(other) >= 0;
|
|
25
|
+
this.isBefore = (other) => this.compare(other) < 0;
|
|
26
|
+
this.isSameOrBefore = (other) => this.compare(other) <= 0;
|
|
27
|
+
this.isSame = (other) => this.compare(other) === 0;
|
|
28
28
|
this.toJSON = () => this.toString();
|
|
29
29
|
}
|
|
30
30
|
get major() {
|
|
@@ -41,8 +41,8 @@ export class Semver {
|
|
|
41
41
|
* returns 0 if they are equal
|
|
42
42
|
* returns -1 if this < other
|
|
43
43
|
*/
|
|
44
|
-
|
|
45
|
-
const { tokens } = semver2.
|
|
44
|
+
compare(other) {
|
|
45
|
+
const { tokens } = semver2.fromInput(other);
|
|
46
46
|
for (let i = 0; i < 3; i++) {
|
|
47
47
|
if (this.tokens[i] < tokens[i])
|
|
48
48
|
return -1;
|
|
@@ -56,16 +56,16 @@ export class Semver {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
class SemverFactory {
|
|
59
|
-
|
|
60
|
-
const s = this.
|
|
61
|
-
_assert(s
|
|
59
|
+
fromInput(input) {
|
|
60
|
+
const s = this.fromInputOrUndefined(input);
|
|
61
|
+
_assert(s, `Cannot parse "${input}" into Semver`, {
|
|
62
62
|
input,
|
|
63
63
|
});
|
|
64
64
|
return s;
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
fromInputOrUndefined(input) {
|
|
67
67
|
if (!input)
|
|
68
|
-
return
|
|
68
|
+
return;
|
|
69
69
|
if (input instanceof Semver)
|
|
70
70
|
return input;
|
|
71
71
|
const t = input.split('.');
|
|
@@ -84,7 +84,9 @@ class SemverFactory {
|
|
|
84
84
|
max(items) {
|
|
85
85
|
const items2 = items.filter(_isTruthy);
|
|
86
86
|
_assert(items2.length, 'semver.max called on empty array');
|
|
87
|
-
return items2
|
|
87
|
+
return items2
|
|
88
|
+
.map(i => this.fromInput(i))
|
|
89
|
+
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
88
90
|
}
|
|
89
91
|
/**
|
|
90
92
|
* Returns the lowest (min) Semver from the array, or undefined if the array is empty.
|
|
@@ -100,7 +102,7 @@ class SemverFactory {
|
|
|
100
102
|
const items2 = items.filter(_isTruthy);
|
|
101
103
|
_assert(items2.length, 'semver.min called on empty array');
|
|
102
104
|
return items2
|
|
103
|
-
.map(i => this.
|
|
105
|
+
.map(i => this.fromInput(i))
|
|
104
106
|
.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
|
|
105
107
|
}
|
|
106
108
|
/**
|
|
@@ -108,11 +110,11 @@ class SemverFactory {
|
|
|
108
110
|
*/
|
|
109
111
|
sort(items, dir = 'asc', mutate = false) {
|
|
110
112
|
const mod = dir === 'desc' ? -1 : 1;
|
|
111
|
-
return (mutate ? items : [...items]).sort((a, b) => a.
|
|
113
|
+
return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
|
|
112
114
|
}
|
|
113
115
|
}
|
|
114
116
|
const semverFactory = new SemverFactory();
|
|
115
|
-
export const semver2 = semverFactory.
|
|
117
|
+
export const semver2 = semverFactory.fromInput.bind(semverFactory);
|
|
116
118
|
// The line below is the blackest of black magic I have ever written in 2024.
|
|
117
119
|
// And probably 2023 as well.
|
|
118
120
|
Object.setPrototypeOf(semver2, semverFactory);
|
package/dist-esm/string/pupa.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.247.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky",
|
|
6
6
|
"build-prod": "build-prod-esm-cjs",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@naturalcycles/bench-lib": "^3.0.0",
|
|
20
|
-
"@naturalcycles/dev-lib": "^
|
|
20
|
+
"@naturalcycles/dev-lib": "^14.0.0",
|
|
21
21
|
"@naturalcycles/nodejs-lib": "^13.0.1",
|
|
22
22
|
"@naturalcycles/time-lib": "^3.5.1",
|
|
23
23
|
"@types/crypto-js": "^4.1.1",
|
|
@@ -141,6 +141,22 @@ export class LocalDate {
|
|
|
141
141
|
return this.isSameOrAfter(localDate.fromInput(today || new Date()).plus(-n, unit))
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
isToday(): boolean {
|
|
145
|
+
return this.isSame(localDate.today())
|
|
146
|
+
}
|
|
147
|
+
isAfterToday(): boolean {
|
|
148
|
+
return this.isAfter(localDate.today())
|
|
149
|
+
}
|
|
150
|
+
isSameOrAfterToday(): boolean {
|
|
151
|
+
return this.isSameOrAfter(localDate.today())
|
|
152
|
+
}
|
|
153
|
+
isBeforeToday(): boolean {
|
|
154
|
+
return this.isBefore(localDate.today())
|
|
155
|
+
}
|
|
156
|
+
isSameOrBeforeToday(): boolean {
|
|
157
|
+
return this.isSameOrBefore(localDate.today())
|
|
158
|
+
}
|
|
159
|
+
|
|
144
160
|
getAgeInYears(today?: LocalDateInput): number {
|
|
145
161
|
return this.getAgeIn('year', today)
|
|
146
162
|
}
|
|
@@ -538,6 +538,13 @@ export class LocalTime {
|
|
|
538
538
|
return localTime.fromInput(now ?? new Date()).diff(this, unit)
|
|
539
539
|
}
|
|
540
540
|
|
|
541
|
+
isAfterNow(): boolean {
|
|
542
|
+
return this.$date.valueOf() > Date.now()
|
|
543
|
+
}
|
|
544
|
+
isBeforeNow(): boolean {
|
|
545
|
+
return this.$date.valueOf() < Date.now()
|
|
546
|
+
}
|
|
547
|
+
|
|
541
548
|
/**
|
|
542
549
|
* Returns 1 if this > d
|
|
543
550
|
* returns 0 if they are equal
|
package/src/promise/pMap.ts
CHANGED
package/src/promise/pQueue.ts
CHANGED
package/src/promise/pTimeout.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ErrorData } from '../error/error.model'
|
|
2
2
|
import { _errorDataAppend, TimeoutError } from '../error/error.util'
|
|
3
|
-
import
|
|
3
|
+
import { _typeCast, AnyAsyncFunction, NumberOfMilliseconds } from '../types'
|
|
4
4
|
|
|
5
5
|
export interface PTimeoutOptions {
|
|
6
6
|
/**
|
|
@@ -84,6 +84,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
|
|
|
84
84
|
try {
|
|
85
85
|
resolve(onTimeout(err))
|
|
86
86
|
} catch (err: any) {
|
|
87
|
+
_typeCast<Error>(err)
|
|
87
88
|
// keep original stack
|
|
88
89
|
err.stack = fakeError.stack!.replace('Error: TimeoutError', err.name + ': ' + err.message)
|
|
89
90
|
reject(_errorDataAppend(err, opt.errorData))
|
|
@@ -98,7 +99,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
|
|
|
98
99
|
try {
|
|
99
100
|
resolve(await fn())
|
|
100
101
|
} catch (err) {
|
|
101
|
-
reject(err)
|
|
102
|
+
reject(err as Error)
|
|
102
103
|
} finally {
|
|
103
104
|
clearTimeout(timer)
|
|
104
105
|
}
|
package/src/semver.ts
CHANGED
|
@@ -36,19 +36,19 @@ export class Semver {
|
|
|
36
36
|
return this.tokens[2]
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
isAfter = (other: SemverInput): boolean => this.
|
|
40
|
-
isSameOrAfter = (other: SemverInput): boolean => this.
|
|
41
|
-
isBefore = (other: SemverInput): boolean => this.
|
|
42
|
-
isSameOrBefore = (other: SemverInput): boolean => this.
|
|
43
|
-
isSame = (other: SemverInput): boolean => this.
|
|
39
|
+
isAfter = (other: SemverInput): boolean => this.compare(other) > 0
|
|
40
|
+
isSameOrAfter = (other: SemverInput): boolean => this.compare(other) >= 0
|
|
41
|
+
isBefore = (other: SemverInput): boolean => this.compare(other) < 0
|
|
42
|
+
isSameOrBefore = (other: SemverInput): boolean => this.compare(other) <= 0
|
|
43
|
+
isSame = (other: SemverInput): boolean => this.compare(other) === 0
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Returns 1 if this > other
|
|
47
47
|
* returns 0 if they are equal
|
|
48
48
|
* returns -1 if this < other
|
|
49
49
|
*/
|
|
50
|
-
|
|
51
|
-
const { tokens } = semver2.
|
|
50
|
+
compare(other: SemverInput): -1 | 0 | 1 {
|
|
51
|
+
const { tokens } = semver2.fromInput(other)
|
|
52
52
|
for (let i = 0; i < 3; i++) {
|
|
53
53
|
if (this.tokens[i]! < tokens[i]!) return -1
|
|
54
54
|
if (this.tokens[i]! > tokens[i]!) return 1
|
|
@@ -64,18 +64,18 @@ export class Semver {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
class SemverFactory {
|
|
67
|
-
|
|
68
|
-
const s = this.
|
|
67
|
+
fromInput(input: SemverInput): Semver {
|
|
68
|
+
const s = this.fromInputOrUndefined(input)
|
|
69
69
|
|
|
70
|
-
_assert(s
|
|
70
|
+
_assert(s, `Cannot parse "${input}" into Semver`, {
|
|
71
71
|
input,
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
return s
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
if (!input) return
|
|
77
|
+
fromInputOrUndefined(input: SemverInputNullable): Semver | undefined {
|
|
78
|
+
if (!input) return
|
|
79
79
|
if (input instanceof Semver) return input
|
|
80
80
|
|
|
81
81
|
const t = input.split('.')
|
|
@@ -96,7 +96,9 @@ class SemverFactory {
|
|
|
96
96
|
max(items: SemverInputNullable[]): Semver {
|
|
97
97
|
const items2 = items.filter(_isTruthy)
|
|
98
98
|
_assert(items2.length, 'semver.max called on empty array')
|
|
99
|
-
return items2
|
|
99
|
+
return items2
|
|
100
|
+
.map(i => this.fromInput(i))
|
|
101
|
+
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item))
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
/**
|
|
@@ -114,7 +116,7 @@ class SemverFactory {
|
|
|
114
116
|
const items2 = items.filter(_isTruthy)
|
|
115
117
|
_assert(items2.length, 'semver.min called on empty array')
|
|
116
118
|
return items2
|
|
117
|
-
.map(i => this.
|
|
119
|
+
.map(i => this.fromInput(i))
|
|
118
120
|
.reduce((min, item) => (min.isSameOrBefore(item) ? min : item))
|
|
119
121
|
}
|
|
120
122
|
|
|
@@ -123,7 +125,7 @@ class SemverFactory {
|
|
|
123
125
|
*/
|
|
124
126
|
sort(items: Semver[], dir: SortDirection = 'asc', mutate = false): Semver[] {
|
|
125
127
|
const mod = dir === 'desc' ? -1 : 1
|
|
126
|
-
return (mutate ? items : [...items]).sort((a, b) => a.
|
|
128
|
+
return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
|
|
127
129
|
}
|
|
128
130
|
}
|
|
129
131
|
|
|
@@ -133,7 +135,7 @@ interface SemverFn extends SemverFactory {
|
|
|
133
135
|
|
|
134
136
|
const semverFactory = new SemverFactory()
|
|
135
137
|
|
|
136
|
-
export const semver2 = semverFactory.
|
|
138
|
+
export const semver2 = semverFactory.fromInput.bind(semverFactory) as SemverFn
|
|
137
139
|
|
|
138
140
|
// The line below is the blackest of black magic I have ever written in 2024.
|
|
139
141
|
// And probably 2023 as well.
|
package/src/string/pupa.ts
CHANGED