@luxass/utils 2.5.0 → 2.6.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/guards.d.ts +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +3 -293
- package/dist/number.d.ts +1 -1
- package/dist/object.d.ts +1 -1
- package/dist/path-BS-_BQ_d.js +128 -0
- package/dist/path-DqJkV3NT.d.ts +96 -0
- package/dist/path.d.ts +2 -0
- package/dist/path.js +3 -0
- package/dist/{string-DDSo2EBf.d.ts → string-Cwp8CD7H.d.ts} +1 -63
- package/dist/{string-DCBui1sH.js → string-pQApOGKP.js} +1 -75
- package/dist/string.d.ts +2 -2
- package/dist/string.js +2 -2
- package/dist/types.d.ts +1 -1
- package/package.json +5 -2
- /package/dist/{guards-x9QZDUSt.d.ts → guards-SWdmRZ5e.d.ts} +0 -0
- /package/dist/{number-DzUC7V5F.d.ts → number-Culbli-Y.d.ts} +0 -0
- /package/dist/{object-BT3S70pM.d.ts → object-BtzfqVfB.d.ts} +0 -0
- /package/dist/{types-DCrM3M9i.d.ts → types-BtiMC8Uf.d.ts} +0 -0
package/dist/guards.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-
|
|
1
|
+
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-SWdmRZ5e.js";
|
|
2
2
|
export { isNotNull, isNotNullish, isNotUndefined, isTruthy };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-
|
|
2
|
-
import { clamp } from "./number-
|
|
3
|
-
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-
|
|
4
|
-
import { appendTrailingSlash,
|
|
5
|
-
import {
|
|
1
|
+
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-SWdmRZ5e.js";
|
|
2
|
+
import { clamp } from "./number-Culbli-Y.js";
|
|
3
|
+
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-BtzfqVfB.js";
|
|
4
|
+
import { appendTrailingSlash, prependLeadingSlash, trimLeadingSlash, trimTrailingSlash } from "./path-DqJkV3NT.js";
|
|
5
|
+
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-Cwp8CD7H.js";
|
|
6
|
+
import { ElementOf, InferArguments, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature } from "./types-BtiMC8Uf.js";
|
|
6
7
|
import pRetry from "p-retry";
|
|
7
8
|
|
|
8
9
|
//#region src/common.d.ts
|
package/dist/index.js
CHANGED
|
@@ -1,34 +1,10 @@
|
|
|
1
1
|
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-O1HGJraI.js";
|
|
2
|
-
import {
|
|
2
|
+
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-pQApOGKP.js";
|
|
3
3
|
import { clamp } from "./number-BS9T5WGO.js";
|
|
4
4
|
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-CyGLe77G.js";
|
|
5
|
+
import { appendTrailingSlash, prependLeadingSlash, trimLeadingSlash, trimTrailingSlash } from "./path-BS-_BQ_d.js";
|
|
6
|
+
import pRetry from "p-retry";
|
|
5
7
|
|
|
6
|
-
//#region rolldown:runtime
|
|
7
|
-
var __create = Object.create;
|
|
8
|
-
var __defProp = Object.defineProperty;
|
|
9
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
12
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
13
|
-
var __commonJS = (cb, mod) => function() {
|
|
14
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
-
};
|
|
16
|
-
var __copyProps = (to, from, except, desc) => {
|
|
17
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
18
|
-
key = keys[i];
|
|
19
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
20
|
-
get: ((k) => from[k]).bind(null, key),
|
|
21
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
return to;
|
|
25
|
-
};
|
|
26
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
27
|
-
value: mod,
|
|
28
|
-
enumerable: true
|
|
29
|
-
}) : target, mod));
|
|
30
|
-
|
|
31
|
-
//#endregion
|
|
32
8
|
//#region src/common.ts
|
|
33
9
|
var InvariantError = class extends Error {
|
|
34
10
|
constructor(message, ...positionals) {
|
|
@@ -50,271 +26,5 @@ function invariant(predicate, message, ...positionals) {
|
|
|
50
26
|
if (!predicate) throw new InvariantError(message, ...positionals);
|
|
51
27
|
}
|
|
52
28
|
|
|
53
|
-
//#endregion
|
|
54
|
-
//#region node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry_operation.js
|
|
55
|
-
var require_retry_operation = __commonJS({ "node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry_operation.js"(exports, module) {
|
|
56
|
-
function RetryOperation$1(timeouts, options) {
|
|
57
|
-
if (typeof options === "boolean") options = { forever: options };
|
|
58
|
-
this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
|
|
59
|
-
this._timeouts = timeouts;
|
|
60
|
-
this._options = options || {};
|
|
61
|
-
this._maxRetryTime = options && options.maxRetryTime || Infinity;
|
|
62
|
-
this._fn = null;
|
|
63
|
-
this._errors = [];
|
|
64
|
-
this._attempts = 1;
|
|
65
|
-
this._operationTimeout = null;
|
|
66
|
-
this._operationTimeoutCb = null;
|
|
67
|
-
this._timeout = null;
|
|
68
|
-
this._operationStart = null;
|
|
69
|
-
this._timer = null;
|
|
70
|
-
if (this._options.forever) this._cachedTimeouts = this._timeouts.slice(0);
|
|
71
|
-
}
|
|
72
|
-
module.exports = RetryOperation$1;
|
|
73
|
-
RetryOperation$1.prototype.reset = function() {
|
|
74
|
-
this._attempts = 1;
|
|
75
|
-
this._timeouts = this._originalTimeouts.slice(0);
|
|
76
|
-
};
|
|
77
|
-
RetryOperation$1.prototype.stop = function() {
|
|
78
|
-
if (this._timeout) clearTimeout(this._timeout);
|
|
79
|
-
if (this._timer) clearTimeout(this._timer);
|
|
80
|
-
this._timeouts = [];
|
|
81
|
-
this._cachedTimeouts = null;
|
|
82
|
-
};
|
|
83
|
-
RetryOperation$1.prototype.retry = function(err) {
|
|
84
|
-
if (this._timeout) clearTimeout(this._timeout);
|
|
85
|
-
if (!err) return false;
|
|
86
|
-
var currentTime = (/* @__PURE__ */ new Date()).getTime();
|
|
87
|
-
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
|
|
88
|
-
this._errors.push(err);
|
|
89
|
-
this._errors.unshift(/* @__PURE__ */ new Error("RetryOperation timeout occurred"));
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
this._errors.push(err);
|
|
93
|
-
var timeout = this._timeouts.shift();
|
|
94
|
-
if (timeout === void 0) if (this._cachedTimeouts) {
|
|
95
|
-
this._errors.splice(0, this._errors.length - 1);
|
|
96
|
-
timeout = this._cachedTimeouts.slice(-1);
|
|
97
|
-
} else return false;
|
|
98
|
-
var self = this;
|
|
99
|
-
this._timer = setTimeout(function() {
|
|
100
|
-
self._attempts++;
|
|
101
|
-
if (self._operationTimeoutCb) {
|
|
102
|
-
self._timeout = setTimeout(function() {
|
|
103
|
-
self._operationTimeoutCb(self._attempts);
|
|
104
|
-
}, self._operationTimeout);
|
|
105
|
-
if (self._options.unref) self._timeout.unref();
|
|
106
|
-
}
|
|
107
|
-
self._fn(self._attempts);
|
|
108
|
-
}, timeout);
|
|
109
|
-
if (this._options.unref) this._timer.unref();
|
|
110
|
-
return true;
|
|
111
|
-
};
|
|
112
|
-
RetryOperation$1.prototype.attempt = function(fn, timeoutOps) {
|
|
113
|
-
this._fn = fn;
|
|
114
|
-
if (timeoutOps) {
|
|
115
|
-
if (timeoutOps.timeout) this._operationTimeout = timeoutOps.timeout;
|
|
116
|
-
if (timeoutOps.cb) this._operationTimeoutCb = timeoutOps.cb;
|
|
117
|
-
}
|
|
118
|
-
var self = this;
|
|
119
|
-
if (this._operationTimeoutCb) this._timeout = setTimeout(function() {
|
|
120
|
-
self._operationTimeoutCb();
|
|
121
|
-
}, self._operationTimeout);
|
|
122
|
-
this._operationStart = (/* @__PURE__ */ new Date()).getTime();
|
|
123
|
-
this._fn(this._attempts);
|
|
124
|
-
};
|
|
125
|
-
RetryOperation$1.prototype.try = function(fn) {
|
|
126
|
-
console.log("Using RetryOperation.try() is deprecated");
|
|
127
|
-
this.attempt(fn);
|
|
128
|
-
};
|
|
129
|
-
RetryOperation$1.prototype.start = function(fn) {
|
|
130
|
-
console.log("Using RetryOperation.start() is deprecated");
|
|
131
|
-
this.attempt(fn);
|
|
132
|
-
};
|
|
133
|
-
RetryOperation$1.prototype.start = RetryOperation$1.prototype.try;
|
|
134
|
-
RetryOperation$1.prototype.errors = function() {
|
|
135
|
-
return this._errors;
|
|
136
|
-
};
|
|
137
|
-
RetryOperation$1.prototype.attempts = function() {
|
|
138
|
-
return this._attempts;
|
|
139
|
-
};
|
|
140
|
-
RetryOperation$1.prototype.mainError = function() {
|
|
141
|
-
if (this._errors.length === 0) return null;
|
|
142
|
-
var counts = {};
|
|
143
|
-
var mainError = null;
|
|
144
|
-
var mainErrorCount = 0;
|
|
145
|
-
for (var i = 0; i < this._errors.length; i++) {
|
|
146
|
-
var error = this._errors[i];
|
|
147
|
-
var message = error.message;
|
|
148
|
-
var count = (counts[message] || 0) + 1;
|
|
149
|
-
counts[message] = count;
|
|
150
|
-
if (count >= mainErrorCount) {
|
|
151
|
-
mainError = error;
|
|
152
|
-
mainErrorCount = count;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return mainError;
|
|
156
|
-
};
|
|
157
|
-
} });
|
|
158
|
-
|
|
159
|
-
//#endregion
|
|
160
|
-
//#region node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry.js
|
|
161
|
-
var require_retry$1 = __commonJS({ "node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry.js"(exports) {
|
|
162
|
-
var RetryOperation = require_retry_operation();
|
|
163
|
-
exports.operation = function(options) {
|
|
164
|
-
var timeouts = exports.timeouts(options);
|
|
165
|
-
return new RetryOperation(timeouts, {
|
|
166
|
-
forever: options && (options.forever || options.retries === Infinity),
|
|
167
|
-
unref: options && options.unref,
|
|
168
|
-
maxRetryTime: options && options.maxRetryTime
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
exports.timeouts = function(options) {
|
|
172
|
-
if (options instanceof Array) return [].concat(options);
|
|
173
|
-
var opts = {
|
|
174
|
-
retries: 10,
|
|
175
|
-
factor: 2,
|
|
176
|
-
minTimeout: 1 * 1e3,
|
|
177
|
-
maxTimeout: Infinity,
|
|
178
|
-
randomize: false
|
|
179
|
-
};
|
|
180
|
-
for (var key in options) opts[key] = options[key];
|
|
181
|
-
if (opts.minTimeout > opts.maxTimeout) throw new Error("minTimeout is greater than maxTimeout");
|
|
182
|
-
var timeouts = [];
|
|
183
|
-
for (var i = 0; i < opts.retries; i++) timeouts.push(this.createTimeout(i, opts));
|
|
184
|
-
if (options && options.forever && !timeouts.length) timeouts.push(this.createTimeout(i, opts));
|
|
185
|
-
timeouts.sort(function(a, b) {
|
|
186
|
-
return a - b;
|
|
187
|
-
});
|
|
188
|
-
return timeouts;
|
|
189
|
-
};
|
|
190
|
-
exports.createTimeout = function(attempt, opts) {
|
|
191
|
-
var random = opts.randomize ? Math.random() + 1 : 1;
|
|
192
|
-
var timeout = Math.round(random * Math.max(opts.minTimeout, 1) * Math.pow(opts.factor, attempt));
|
|
193
|
-
timeout = Math.min(timeout, opts.maxTimeout);
|
|
194
|
-
return timeout;
|
|
195
|
-
};
|
|
196
|
-
exports.wrap = function(obj, options, methods) {
|
|
197
|
-
if (options instanceof Array) {
|
|
198
|
-
methods = options;
|
|
199
|
-
options = null;
|
|
200
|
-
}
|
|
201
|
-
if (!methods) {
|
|
202
|
-
methods = [];
|
|
203
|
-
for (var key in obj) if (typeof obj[key] === "function") methods.push(key);
|
|
204
|
-
}
|
|
205
|
-
for (var i = 0; i < methods.length; i++) {
|
|
206
|
-
var method = methods[i];
|
|
207
|
-
var original = obj[method];
|
|
208
|
-
obj[method] = function retryWrapper(original$1) {
|
|
209
|
-
var op = exports.operation(options);
|
|
210
|
-
var args = Array.prototype.slice.call(arguments, 1);
|
|
211
|
-
var callback = args.pop();
|
|
212
|
-
args.push(function(err) {
|
|
213
|
-
if (op.retry(err)) return;
|
|
214
|
-
if (err) arguments[0] = op.mainError();
|
|
215
|
-
callback.apply(this, arguments);
|
|
216
|
-
});
|
|
217
|
-
op.attempt(function() {
|
|
218
|
-
original$1.apply(obj, args);
|
|
219
|
-
});
|
|
220
|
-
}.bind(obj, original);
|
|
221
|
-
obj[method].options = options;
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
} });
|
|
225
|
-
|
|
226
|
-
//#endregion
|
|
227
|
-
//#region node_modules/.pnpm/retry@0.13.1/node_modules/retry/index.js
|
|
228
|
-
var require_retry = __commonJS({ "node_modules/.pnpm/retry@0.13.1/node_modules/retry/index.js"(exports, module) {
|
|
229
|
-
module.exports = require_retry$1();
|
|
230
|
-
} });
|
|
231
|
-
|
|
232
|
-
//#endregion
|
|
233
|
-
//#region node_modules/.pnpm/is-network-error@1.1.0/node_modules/is-network-error/index.js
|
|
234
|
-
const objectToString = Object.prototype.toString;
|
|
235
|
-
const isError = (value) => objectToString.call(value) === "[object Error]";
|
|
236
|
-
const errorMessages = new Set([
|
|
237
|
-
"network error",
|
|
238
|
-
"Failed to fetch",
|
|
239
|
-
"NetworkError when attempting to fetch resource.",
|
|
240
|
-
"The Internet connection appears to be offline.",
|
|
241
|
-
"Load failed",
|
|
242
|
-
"Network request failed",
|
|
243
|
-
"fetch failed",
|
|
244
|
-
"terminated"
|
|
245
|
-
]);
|
|
246
|
-
function isNetworkError(error) {
|
|
247
|
-
const isValid = error && isError(error) && error.name === "TypeError" && typeof error.message === "string";
|
|
248
|
-
if (!isValid) return false;
|
|
249
|
-
if (error.message === "Load failed") return error.stack === void 0;
|
|
250
|
-
return errorMessages.has(error.message);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
//#endregion
|
|
254
|
-
//#region node_modules/.pnpm/p-retry@6.2.1/node_modules/p-retry/index.js
|
|
255
|
-
var import_retry = __toESM(require_retry(), 1);
|
|
256
|
-
var AbortError = class extends Error {
|
|
257
|
-
constructor(message) {
|
|
258
|
-
super();
|
|
259
|
-
if (message instanceof Error) {
|
|
260
|
-
this.originalError = message;
|
|
261
|
-
({message} = message);
|
|
262
|
-
} else {
|
|
263
|
-
this.originalError = new Error(message);
|
|
264
|
-
this.originalError.stack = this.stack;
|
|
265
|
-
}
|
|
266
|
-
this.name = "AbortError";
|
|
267
|
-
this.message = message;
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
const decorateErrorWithCounts = (error, attemptNumber, options) => {
|
|
271
|
-
const retriesLeft = options.retries - (attemptNumber - 1);
|
|
272
|
-
error.attemptNumber = attemptNumber;
|
|
273
|
-
error.retriesLeft = retriesLeft;
|
|
274
|
-
return error;
|
|
275
|
-
};
|
|
276
|
-
async function pRetry(input, options) {
|
|
277
|
-
return new Promise((resolve, reject) => {
|
|
278
|
-
options = { ...options };
|
|
279
|
-
options.onFailedAttempt ??= () => {};
|
|
280
|
-
options.shouldRetry ??= () => true;
|
|
281
|
-
options.retries ??= 10;
|
|
282
|
-
const operation = import_retry.operation(options);
|
|
283
|
-
const abortHandler = () => {
|
|
284
|
-
operation.stop();
|
|
285
|
-
reject(options.signal?.reason);
|
|
286
|
-
};
|
|
287
|
-
if (options.signal && !options.signal.aborted) options.signal.addEventListener("abort", abortHandler, { once: true });
|
|
288
|
-
const cleanUp = () => {
|
|
289
|
-
options.signal?.removeEventListener("abort", abortHandler);
|
|
290
|
-
operation.stop();
|
|
291
|
-
};
|
|
292
|
-
operation.attempt(async (attemptNumber) => {
|
|
293
|
-
try {
|
|
294
|
-
const result = await input(attemptNumber);
|
|
295
|
-
cleanUp();
|
|
296
|
-
resolve(result);
|
|
297
|
-
} catch (error) {
|
|
298
|
-
try {
|
|
299
|
-
if (!(error instanceof Error)) throw new TypeError(`Non-error was thrown: "${error}". You should only throw errors.`);
|
|
300
|
-
if (error instanceof AbortError) throw error.originalError;
|
|
301
|
-
if (error instanceof TypeError && !isNetworkError(error)) throw error;
|
|
302
|
-
decorateErrorWithCounts(error, attemptNumber, options);
|
|
303
|
-
if (!await options.shouldRetry(error)) {
|
|
304
|
-
operation.stop();
|
|
305
|
-
reject(error);
|
|
306
|
-
}
|
|
307
|
-
await options.onFailedAttempt(error);
|
|
308
|
-
if (!operation.retry(error)) throw operation.mainError();
|
|
309
|
-
} catch (finalError) {
|
|
310
|
-
decorateErrorWithCounts(finalError, attemptNumber, options);
|
|
311
|
-
cleanUp();
|
|
312
|
-
reject(finalError);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
|
|
319
29
|
//#endregion
|
|
320
30
|
export { InvariantError, appendTrailingSlash, capitalize, clamp, dedent, dedentRaw, formatStr, getChangedKeys, getOwnProperty, hasOwnProperty, invariant, isNotNull, isNotNullish, isNotUndefined, isTruthy, omit, prependLeadingSlash, pRetry as promiseRetry, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash };
|
package/dist/number.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { clamp } from "./number-
|
|
1
|
+
import { clamp } from "./number-Culbli-Y.js";
|
|
2
2
|
export { clamp };
|
package/dist/object.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-
|
|
1
|
+
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-BtzfqVfB.js";
|
|
2
2
|
export { getChangedKeys, getOwnProperty, hasOwnProperty, omit };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//#region src/path.ts
|
|
2
|
+
/**
|
|
3
|
+
* Removes trailing slashes from a string
|
|
4
|
+
* @param {string} path - The string to remove trailing slashes from
|
|
5
|
+
* @returns {string} The string with trailing slashes removed
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { trimTrailingSlash } from "@luxass/utils/path";
|
|
10
|
+
*
|
|
11
|
+
* trimTrailingSlash("path/to/file/") // "path/to/file"
|
|
12
|
+
* trimTrailingSlash("path/to/file///") // "path/to/file"
|
|
13
|
+
* trimTrailingSlash("path/to/file") // "path/to/file"
|
|
14
|
+
* trimTrailingSlash("") // ""
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function trimTrailingSlash(path) {
|
|
18
|
+
if (!path || path === "/") return "/";
|
|
19
|
+
return path.replace(/\/+$/, "");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Removes leading slashes from a string
|
|
23
|
+
* @param {string} path - The string to remove leading slashes from
|
|
24
|
+
* @returns {string} The string with leading slashes removed
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { trimLeadingSlash } from "@luxass/utils/path";
|
|
29
|
+
*
|
|
30
|
+
* trimLeadingSlash("/path/to/file") // "path/to/file"
|
|
31
|
+
* trimLeadingSlash("///path/to/file") // "path/to/file"
|
|
32
|
+
* trimLeadingSlash("path/to/file") // "path/to/file"
|
|
33
|
+
* trimLeadingSlash("") // ""
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
function trimLeadingSlash(path) {
|
|
37
|
+
if (!path || path === "/") return "/";
|
|
38
|
+
return path.replace(/^\/+/, "");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Ensures a string ends with a trailing slash
|
|
42
|
+
* @param {string} path - The string to append a trailing slash to
|
|
43
|
+
* @returns {string} The string with a trailing slash appended if not already present
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { appendTrailingSlash } from "@luxass/utils/path";
|
|
48
|
+
*
|
|
49
|
+
* appendTrailingSlash("path/to/file") // "path/to/file/"
|
|
50
|
+
* appendTrailingSlash("path/to/file/") // "path/to/file/"
|
|
51
|
+
* appendTrailingSlash("") // ""
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
function appendTrailingSlash(path) {
|
|
55
|
+
if (!path || path === "/") return "/";
|
|
56
|
+
return path.endsWith("/") ? path : `${path}/`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Ensures a string starts with a leading slash
|
|
60
|
+
* @param {string} path - The string to prepend a leading slash to
|
|
61
|
+
* @returns {string} The string with a leading slash prepended if not already present
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { prependLeadingSlash } from "@luxass/utils/path";
|
|
66
|
+
*
|
|
67
|
+
* prependLeadingSlash("path/to/file") // "/path/to/file"
|
|
68
|
+
* prependLeadingSlash("/path/to/file") // "/path/to/file"
|
|
69
|
+
* prependLeadingSlash("") // ""
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function prependLeadingSlash(path) {
|
|
73
|
+
if (!path || path === "/") return "/";
|
|
74
|
+
return path[0] === "/" ? path : `/${path}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Joins two URL paths together, handling trailing and leading slashes appropriately
|
|
78
|
+
* @param {string | undefined} base - The base URL path
|
|
79
|
+
* @param {string | undefined} path - The path to append to the base
|
|
80
|
+
* @returns {string} The joined URL path
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { joinURL } from "@luxass/utils/path";
|
|
85
|
+
*
|
|
86
|
+
* // Basic path joining
|
|
87
|
+
* joinURL("api", "users") // "api/users"
|
|
88
|
+
* joinURL("api/", "users") // "api/users"
|
|
89
|
+
* joinURL("api", "/users") // "api/users"
|
|
90
|
+
* joinURL("api/", "/users") // "api/users"
|
|
91
|
+
*
|
|
92
|
+
* // URL joining with protocol
|
|
93
|
+
* joinURL("https://api.example.com", "v1/users") // "https://api.example.com/v1/users"
|
|
94
|
+
* joinURL("https://api.example.com/", "/v1/users") // "https://api.example.com/v1/users"
|
|
95
|
+
*
|
|
96
|
+
* // Multiple slash normalization (POSIX-like)
|
|
97
|
+
* joinURL("api//v1/", "//users///") // "api/v1/users/"
|
|
98
|
+
* joinURL("base///", "///path") // "base/path"
|
|
99
|
+
*
|
|
100
|
+
* // Empty and undefined handling
|
|
101
|
+
* joinURL("", "users") // "users"
|
|
102
|
+
* joinURL("api", "") // "api"
|
|
103
|
+
* joinURL(undefined, undefined) // "/"
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function joinURL(base, path) {
|
|
107
|
+
if (!base && !path) return "/";
|
|
108
|
+
if (!base || base === "/") return path || "/";
|
|
109
|
+
if (!path || path === "/") return base;
|
|
110
|
+
const normalize = (s) => s.replace(/\/+/g, "/");
|
|
111
|
+
const joinPaths = (b, p) => {
|
|
112
|
+
b = normalize(b);
|
|
113
|
+
p = normalize(p);
|
|
114
|
+
if (b.endsWith("/") && b !== "/") b = b.slice(0, -1);
|
|
115
|
+
if (p.startsWith("/")) p = p.slice(1);
|
|
116
|
+
return p ? `${b}/${p}` : b;
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
const url = new URL(base);
|
|
120
|
+
url.pathname = trimLeadingSlash(joinPaths(url.pathname, path));
|
|
121
|
+
return url.toString();
|
|
122
|
+
} catch {
|
|
123
|
+
return joinPaths(base, path);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
export { appendTrailingSlash, joinURL, prependLeadingSlash, trimLeadingSlash, trimTrailingSlash };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//#region src/path.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Removes trailing slashes from a string
|
|
4
|
+
* @param {string} path - The string to remove trailing slashes from
|
|
5
|
+
* @returns {string} The string with trailing slashes removed
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { trimTrailingSlash } from "@luxass/utils/path";
|
|
10
|
+
*
|
|
11
|
+
* trimTrailingSlash("path/to/file/") // "path/to/file"
|
|
12
|
+
* trimTrailingSlash("path/to/file///") // "path/to/file"
|
|
13
|
+
* trimTrailingSlash("path/to/file") // "path/to/file"
|
|
14
|
+
* trimTrailingSlash("") // ""
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare function trimTrailingSlash(path: string | undefined): string;
|
|
18
|
+
/**
|
|
19
|
+
* Removes leading slashes from a string
|
|
20
|
+
* @param {string} path - The string to remove leading slashes from
|
|
21
|
+
* @returns {string} The string with leading slashes removed
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { trimLeadingSlash } from "@luxass/utils/path";
|
|
26
|
+
*
|
|
27
|
+
* trimLeadingSlash("/path/to/file") // "path/to/file"
|
|
28
|
+
* trimLeadingSlash("///path/to/file") // "path/to/file"
|
|
29
|
+
* trimLeadingSlash("path/to/file") // "path/to/file"
|
|
30
|
+
* trimLeadingSlash("") // ""
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function trimLeadingSlash(path: string | undefined): string;
|
|
34
|
+
/**
|
|
35
|
+
* Ensures a string ends with a trailing slash
|
|
36
|
+
* @param {string} path - The string to append a trailing slash to
|
|
37
|
+
* @returns {string} The string with a trailing slash appended if not already present
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { appendTrailingSlash } from "@luxass/utils/path";
|
|
42
|
+
*
|
|
43
|
+
* appendTrailingSlash("path/to/file") // "path/to/file/"
|
|
44
|
+
* appendTrailingSlash("path/to/file/") // "path/to/file/"
|
|
45
|
+
* appendTrailingSlash("") // ""
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function appendTrailingSlash(path: string | undefined): string;
|
|
49
|
+
/**
|
|
50
|
+
* Ensures a string starts with a leading slash
|
|
51
|
+
* @param {string} path - The string to prepend a leading slash to
|
|
52
|
+
* @returns {string} The string with a leading slash prepended if not already present
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { prependLeadingSlash } from "@luxass/utils/path";
|
|
57
|
+
*
|
|
58
|
+
* prependLeadingSlash("path/to/file") // "/path/to/file"
|
|
59
|
+
* prependLeadingSlash("/path/to/file") // "/path/to/file"
|
|
60
|
+
* prependLeadingSlash("") // ""
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function prependLeadingSlash(path: string | undefined): string;
|
|
64
|
+
/**
|
|
65
|
+
* Joins two URL paths together, handling trailing and leading slashes appropriately
|
|
66
|
+
* @param {string | undefined} base - The base URL path
|
|
67
|
+
* @param {string | undefined} path - The path to append to the base
|
|
68
|
+
* @returns {string} The joined URL path
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { joinURL } from "@luxass/utils/path";
|
|
73
|
+
*
|
|
74
|
+
* // Basic path joining
|
|
75
|
+
* joinURL("api", "users") // "api/users"
|
|
76
|
+
* joinURL("api/", "users") // "api/users"
|
|
77
|
+
* joinURL("api", "/users") // "api/users"
|
|
78
|
+
* joinURL("api/", "/users") // "api/users"
|
|
79
|
+
*
|
|
80
|
+
* // URL joining with protocol
|
|
81
|
+
* joinURL("https://api.example.com", "v1/users") // "https://api.example.com/v1/users"
|
|
82
|
+
* joinURL("https://api.example.com/", "/v1/users") // "https://api.example.com/v1/users"
|
|
83
|
+
*
|
|
84
|
+
* // Multiple slash normalization (POSIX-like)
|
|
85
|
+
* joinURL("api//v1/", "//users///") // "api/v1/users/"
|
|
86
|
+
* joinURL("base///", "///path") // "base/path"
|
|
87
|
+
*
|
|
88
|
+
* // Empty and undefined handling
|
|
89
|
+
* joinURL("", "users") // "users"
|
|
90
|
+
* joinURL("api", "") // "api"
|
|
91
|
+
* joinURL(undefined, undefined) // "/"
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
declare function joinURL(base: string | undefined, path: string | undefined): string;
|
|
95
|
+
//#endregion
|
|
96
|
+
export { appendTrailingSlash, joinURL, prependLeadingSlash, trimLeadingSlash, trimTrailingSlash };
|
package/dist/path.d.ts
ADDED
package/dist/path.js
ADDED
|
@@ -128,67 +128,5 @@ declare function sanitizeIdentifier(str: string): string;
|
|
|
128
128
|
* ```
|
|
129
129
|
*/
|
|
130
130
|
declare function formatStr(message: string, ...positionals: unknown[]): string;
|
|
131
|
-
/**
|
|
132
|
-
* Removes trailing slashes from a string
|
|
133
|
-
* @param {string} str - The string to remove trailing slashes from
|
|
134
|
-
* @returns {string} The string with trailing slashes removed
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```ts
|
|
138
|
-
* import { trimTrailingSlash } from "@luxass/utils/string";
|
|
139
|
-
*
|
|
140
|
-
* trimTrailingSlash("path/to/file/") // "path/to/file"
|
|
141
|
-
* trimTrailingSlash("path/to/file///") // "path/to/file"
|
|
142
|
-
* trimTrailingSlash("path/to/file") // "path/to/file"
|
|
143
|
-
* trimTrailingSlash("") // ""
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
declare function trimTrailingSlash(str: string): string;
|
|
147
|
-
/**
|
|
148
|
-
* Removes leading slashes from a string
|
|
149
|
-
* @param {string} str - The string to remove leading slashes from
|
|
150
|
-
* @returns {string} The string with leading slashes removed
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```ts
|
|
154
|
-
* import { trimLeadingSlash } from "@luxass/utils/string";
|
|
155
|
-
*
|
|
156
|
-
* trimLeadingSlash("/path/to/file") // "path/to/file"
|
|
157
|
-
* trimLeadingSlash("///path/to/file") // "path/to/file"
|
|
158
|
-
* trimLeadingSlash("path/to/file") // "path/to/file"
|
|
159
|
-
* trimLeadingSlash("") // ""
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
|
-
declare function trimLeadingSlash(str: string): string;
|
|
163
|
-
/**
|
|
164
|
-
* Ensures a string ends with a trailing slash
|
|
165
|
-
* @param {string} str - The string to append a trailing slash to
|
|
166
|
-
* @returns {string} The string with a trailing slash appended if not already present
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```ts
|
|
170
|
-
* import { appendTrailingSlash } from "@luxass/utils/string";
|
|
171
|
-
*
|
|
172
|
-
* appendTrailingSlash("path/to/file") // "path/to/file/"
|
|
173
|
-
* appendTrailingSlash("path/to/file/") // "path/to/file/"
|
|
174
|
-
* appendTrailingSlash("") // ""
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
declare function appendTrailingSlash(str: string): string;
|
|
178
|
-
/**
|
|
179
|
-
* Ensures a string starts with a leading slash
|
|
180
|
-
* @param {string} str - The string to prepend a leading slash to
|
|
181
|
-
* @returns {string} The string with a leading slash prepended if not already present
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* import { prependLeadingSlash } from "@luxass/utils/string";
|
|
186
|
-
*
|
|
187
|
-
* prependLeadingSlash("path/to/file") // "/path/to/file"
|
|
188
|
-
* prependLeadingSlash("/path/to/file") // "/path/to/file"
|
|
189
|
-
* prependLeadingSlash("") // ""
|
|
190
|
-
* ```
|
|
191
|
-
*/
|
|
192
|
-
declare function prependLeadingSlash(str: string): string;
|
|
193
131
|
//#endregion
|
|
194
|
-
export {
|
|
132
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -181,80 +181,6 @@ function formatStr(message, ...positionals) {
|
|
|
181
181
|
formattedMessage = formattedMessage.replace(/%{2}/g, "%");
|
|
182
182
|
return formattedMessage;
|
|
183
183
|
}
|
|
184
|
-
/**
|
|
185
|
-
* Removes trailing slashes from a string
|
|
186
|
-
* @param {string} str - The string to remove trailing slashes from
|
|
187
|
-
* @returns {string} The string with trailing slashes removed
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```ts
|
|
191
|
-
* import { trimTrailingSlash } from "@luxass/utils/string";
|
|
192
|
-
*
|
|
193
|
-
* trimTrailingSlash("path/to/file/") // "path/to/file"
|
|
194
|
-
* trimTrailingSlash("path/to/file///") // "path/to/file"
|
|
195
|
-
* trimTrailingSlash("path/to/file") // "path/to/file"
|
|
196
|
-
* trimTrailingSlash("") // ""
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
function trimTrailingSlash(str) {
|
|
200
|
-
if (!str) return str;
|
|
201
|
-
return str.replace(/\/+$/, "");
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Removes leading slashes from a string
|
|
205
|
-
* @param {string} str - The string to remove leading slashes from
|
|
206
|
-
* @returns {string} The string with leading slashes removed
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```ts
|
|
210
|
-
* import { trimLeadingSlash } from "@luxass/utils/string";
|
|
211
|
-
*
|
|
212
|
-
* trimLeadingSlash("/path/to/file") // "path/to/file"
|
|
213
|
-
* trimLeadingSlash("///path/to/file") // "path/to/file"
|
|
214
|
-
* trimLeadingSlash("path/to/file") // "path/to/file"
|
|
215
|
-
* trimLeadingSlash("") // ""
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
function trimLeadingSlash(str) {
|
|
219
|
-
if (!str) return str;
|
|
220
|
-
return str.replace(/^\/+/, "");
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Ensures a string ends with a trailing slash
|
|
224
|
-
* @param {string} str - The string to append a trailing slash to
|
|
225
|
-
* @returns {string} The string with a trailing slash appended if not already present
|
|
226
|
-
*
|
|
227
|
-
* @example
|
|
228
|
-
* ```ts
|
|
229
|
-
* import { appendTrailingSlash } from "@luxass/utils/string";
|
|
230
|
-
*
|
|
231
|
-
* appendTrailingSlash("path/to/file") // "path/to/file/"
|
|
232
|
-
* appendTrailingSlash("path/to/file/") // "path/to/file/"
|
|
233
|
-
* appendTrailingSlash("") // ""
|
|
234
|
-
* ```
|
|
235
|
-
*/
|
|
236
|
-
function appendTrailingSlash(str) {
|
|
237
|
-
if (!str) return str;
|
|
238
|
-
return str.endsWith("/") ? str : `${str}/`;
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Ensures a string starts with a leading slash
|
|
242
|
-
* @param {string} str - The string to prepend a leading slash to
|
|
243
|
-
* @returns {string} The string with a leading slash prepended if not already present
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```ts
|
|
247
|
-
* import { prependLeadingSlash } from "@luxass/utils/string";
|
|
248
|
-
*
|
|
249
|
-
* prependLeadingSlash("path/to/file") // "/path/to/file"
|
|
250
|
-
* prependLeadingSlash("/path/to/file") // "/path/to/file"
|
|
251
|
-
* prependLeadingSlash("") // ""
|
|
252
|
-
* ```
|
|
253
|
-
*/
|
|
254
|
-
function prependLeadingSlash(str) {
|
|
255
|
-
if (!str) return str;
|
|
256
|
-
return str.startsWith("/") ? str : `/${str}`;
|
|
257
|
-
}
|
|
258
184
|
|
|
259
185
|
//#endregion
|
|
260
|
-
export {
|
|
186
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/string.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-Cwp8CD7H.js";
|
|
2
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/string.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-pQApOGKP.js";
|
|
2
2
|
|
|
3
|
-
export {
|
|
3
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ElementOf, InferArguments, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature } from "./types-
|
|
1
|
+
import { ElementOf, InferArguments, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature } from "./types-BtiMC8Uf.js";
|
|
2
2
|
export { ElementOf, InferArguments, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luxass/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "A collection of utilities for JavaScript/TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"./guards": "./dist/guards.js",
|
|
27
27
|
"./number": "./dist/number.js",
|
|
28
28
|
"./object": "./dist/object.js",
|
|
29
|
+
"./path": "./dist/path.js",
|
|
29
30
|
"./string": "./dist/string.js",
|
|
30
31
|
"./types": "./dist/types.js",
|
|
31
32
|
"./package.json": "./package.json"
|
|
@@ -39,13 +40,15 @@
|
|
|
39
40
|
"engines": {
|
|
40
41
|
"node": ">=20"
|
|
41
42
|
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"p-retry": "^6.2.1"
|
|
45
|
+
},
|
|
42
46
|
"devDependencies": {
|
|
43
47
|
"@luxass/eslint-config": "^5.1.0",
|
|
44
48
|
"@types/node": "^22.15.2",
|
|
45
49
|
"@vitest/coverage-v8": "3.2.4",
|
|
46
50
|
"eslint": "^9.30.1",
|
|
47
51
|
"eslint-plugin-format": "^1.0.1",
|
|
48
|
-
"p-retry": "^6.2.1",
|
|
49
52
|
"publint": "^0.3.12",
|
|
50
53
|
"tsdown": "^0.12.9",
|
|
51
54
|
"typescript": "^5.8.3",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|