@luxass/utils 2.4.1 → 2.5.1
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/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-x9QZDUSt.js";
|
|
2
2
|
import { clamp } from "./number-DzUC7V5F.js";
|
|
3
3
|
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-BT3S70pM.js";
|
|
4
|
-
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-
|
|
4
|
+
import { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash } from "./string-DDSo2EBf.js";
|
|
5
5
|
import { ElementOf, InferArguments, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature } from "./types-DCrM3M9i.js";
|
|
6
6
|
import pRetry from "p-retry";
|
|
7
7
|
|
|
@@ -21,4 +21,4 @@ declare class InvariantError extends Error {
|
|
|
21
21
|
*/
|
|
22
22
|
declare function invariant(predicate: unknown, message: string, ...positionals: unknown[]): asserts predicate;
|
|
23
23
|
//#endregion
|
|
24
|
-
export { ElementOf, InferArguments, InvariantError, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature, capitalize, clamp, dedent, dedentRaw, formatStr, getChangedKeys, getOwnProperty, hasOwnProperty, invariant, isNotNull, isNotNullish, isNotUndefined, isTruthy, omit, pRetry as promiseRetry, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
24
|
+
export { ElementOf, InferArguments, InvariantError, MaybeArray, MaybePromise, Nullable, Nullish, Prettify, RemoveIndexSignature, 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/index.js
CHANGED
|
@@ -1,34 +1,9 @@
|
|
|
1
1
|
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-O1HGJraI.js";
|
|
2
|
-
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-
|
|
2
|
+
import { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash } from "./string-DCBui1sH.js";
|
|
3
3
|
import { clamp } from "./number-BS9T5WGO.js";
|
|
4
4
|
import { getChangedKeys, getOwnProperty, hasOwnProperty, omit } from "./object-CyGLe77G.js";
|
|
5
|
+
import pRetry from "p-retry";
|
|
5
6
|
|
|
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
7
|
//#region src/common.ts
|
|
33
8
|
var InvariantError = class extends Error {
|
|
34
9
|
constructor(message, ...positionals) {
|
|
@@ -51,270 +26,4 @@ function invariant(predicate, message, ...positionals) {
|
|
|
51
26
|
}
|
|
52
27
|
|
|
53
28
|
//#endregion
|
|
54
|
-
|
|
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
|
-
//#endregion
|
|
320
|
-
export { InvariantError, capitalize, clamp, dedent, dedentRaw, formatStr, getChangedKeys, getOwnProperty, hasOwnProperty, invariant, isNotNull, isNotNullish, isNotUndefined, isTruthy, omit, pRetry as promiseRetry, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
29
|
+
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 };
|
|
@@ -181,6 +181,80 @@ 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
|
+
}
|
|
184
258
|
|
|
185
259
|
//#endregion
|
|
186
|
-
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
260
|
+
export { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash };
|
|
@@ -128,5 +128,67 @@ 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;
|
|
131
193
|
//#endregion
|
|
132
|
-
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
194
|
+
export { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash };
|
package/dist/string.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-
|
|
2
|
-
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
1
|
+
import { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash } from "./string-DDSo2EBf.js";
|
|
2
|
+
export { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash };
|
package/dist/string.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-
|
|
1
|
+
import { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash } from "./string-DCBui1sH.js";
|
|
2
2
|
|
|
3
|
-
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
3
|
+
export { appendTrailingSlash, capitalize, dedent, dedentRaw, formatStr, prependLeadingSlash, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, trimLeadingSlash, trimTrailingSlash };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luxass/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "A collection of utilities for JavaScript/TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -39,13 +39,15 @@
|
|
|
39
39
|
"engines": {
|
|
40
40
|
"node": ">=20"
|
|
41
41
|
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"p-retry": "^6.2.1"
|
|
44
|
+
},
|
|
42
45
|
"devDependencies": {
|
|
43
|
-
"@luxass/eslint-config": "^5.
|
|
46
|
+
"@luxass/eslint-config": "^5.1.0",
|
|
44
47
|
"@types/node": "^22.15.2",
|
|
45
48
|
"@vitest/coverage-v8": "3.2.4",
|
|
46
|
-
"eslint": "^9.30.
|
|
49
|
+
"eslint": "^9.30.1",
|
|
47
50
|
"eslint-plugin-format": "^1.0.1",
|
|
48
|
-
"p-retry": "^6.2.1",
|
|
49
51
|
"publint": "^0.3.12",
|
|
50
52
|
"tsdown": "^0.12.9",
|
|
51
53
|
"typescript": "^5.8.3",
|