@httptoolkit/util 0.1.6 → 0.1.8
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/errors.d.ts +1 -1
- package/dist/errors.js +6 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/promises.js +4 -4
- package/dist/promises.js.map +1 -1
- package/dist/text.d.ts +7 -0
- package/dist/text.js +32 -0
- package/dist/text.js.map +1 -0
- package/package.json +2 -2
- package/src/errors.ts +1 -1
- package/src/index.ts +2 -1
- package/src/text.ts +33 -0
package/dist/errors.d.ts
CHANGED
package/dist/errors.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unreachableCheck = exports.UnreachableCheck = exports.StatusError = exports.CustomError =
|
|
3
|
+
exports.unreachableCheck = exports.UnreachableCheck = exports.StatusError = exports.CustomError = void 0;
|
|
4
|
+
exports.isErrorLike = isErrorLike;
|
|
5
|
+
exports.asErrorLike = asErrorLike;
|
|
4
6
|
/**
|
|
5
7
|
* An easy way to check if an unknown object (e.g. a catch(e) argument) is
|
|
6
8
|
* actually an error-like
|
|
@@ -11,7 +13,6 @@ function isErrorLike(error) {
|
|
|
11
13
|
error.code ||
|
|
12
14
|
error.stack);
|
|
13
15
|
}
|
|
14
|
-
exports.isErrorLike = isErrorLike;
|
|
15
16
|
/**
|
|
16
17
|
* A convenience method to make something error-ish into an actual Error instance.
|
|
17
18
|
*/
|
|
@@ -26,7 +27,6 @@ function asErrorLike(error) {
|
|
|
26
27
|
return new Error('[Undefined error]');
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
|
-
exports.asErrorLike = asErrorLike;
|
|
30
30
|
// Tiny wrapper to make it super easy to make custom error classes where .name behaves
|
|
31
31
|
// correctly, and useful metafields can be easily added.
|
|
32
32
|
class CustomError extends Error {
|
|
@@ -42,6 +42,9 @@ class CustomError extends Error {
|
|
|
42
42
|
if (extras.cause !== undefined)
|
|
43
43
|
this.cause = extras.cause;
|
|
44
44
|
}
|
|
45
|
+
code;
|
|
46
|
+
statusCode;
|
|
47
|
+
cause;
|
|
45
48
|
}
|
|
46
49
|
exports.CustomError = CustomError;
|
|
47
50
|
class StatusError extends CustomError {
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAkBA,kCAOC;AAKD,kCAQC;AAxBD;;;GAGG;AACH,SAAgB,WAAW,CAAC,KAAU;IAClC,OAAO,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CACzC,KAAK,YAAY,KAAK;QACtB,KAAK,CAAC,OAAO;QACb,KAAK,CAAC,IAAI;QACV,KAAK,CAAC,KAAK,CACd,CAAA;AACL,CAAC;AAED;;GAEG;AACH,SAAgB,WAAW,CAAC,KAAU;IAClC,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,KAAkB,CAAC;IAC9B,CAAC;SAAM,IAAI,KAAK,EAAE,CAAC;QACf,OAAO,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE,IAAI,iBAAiB,CAAC,CAAC;IAC7E,CAAC;SAAM,CAAC;QACJ,OAAO,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAC1C,CAAC;AACL,CAAC;AAED,sFAAsF;AACtF,wDAAwD;AACxD,MAAa,WAAY,SAAQ,KAAK;IAClC,YAAY,OAAgB,EAAE,SAI1B,EAAE;QACF,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,sCAAsC;QAEtD,wDAAwD;QACxD,IAAI,CAAC,IAAI,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CHANGED
|
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./promises"), exports);
|
|
18
18
|
__exportStar(require("./errors"), exports);
|
|
19
|
+
__exportStar(require("./text"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,2CAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,2CAAyB;AACzB,yCAAuB"}
|
package/dist/promises.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.delay = void 0;
|
|
4
|
+
exports.doWhile = doWhile;
|
|
5
|
+
exports.getDeferred = getDeferred;
|
|
6
|
+
exports.combineParallelCalls = combineParallelCalls;
|
|
4
7
|
const delay = (ms, options = {}) => new Promise((resolve) => {
|
|
5
8
|
const timer = setTimeout(resolve, ms);
|
|
6
9
|
if (options.unref && timer.unref) {
|
|
@@ -13,7 +16,6 @@ async function doWhile(doFn, whileFn) {
|
|
|
13
16
|
await doFn();
|
|
14
17
|
} while (await whileFn());
|
|
15
18
|
}
|
|
16
|
-
exports.doWhile = doWhile;
|
|
17
19
|
function getDeferred() {
|
|
18
20
|
let resolve;
|
|
19
21
|
let reject;
|
|
@@ -23,7 +25,6 @@ function getDeferred() {
|
|
|
23
25
|
});
|
|
24
26
|
return Object.assign(promise, { resolve, reject, promise });
|
|
25
27
|
}
|
|
26
|
-
exports.getDeferred = getDeferred;
|
|
27
28
|
/**
|
|
28
29
|
* Wrap a function, so that any parallel calls which happen while the async function
|
|
29
30
|
* is pending return the same value as the first call (so the function is only run
|
|
@@ -42,5 +43,4 @@ function combineParallelCalls(fn) {
|
|
|
42
43
|
return pendingPromise;
|
|
43
44
|
};
|
|
44
45
|
}
|
|
45
|
-
exports.combineParallelCalls = combineParallelCalls;
|
|
46
46
|
//# sourceMappingURL=promises.js.map
|
package/dist/promises.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promises.js","sourceRoot":"","sources":["../src/promises.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"promises.js","sourceRoot":"","sources":["../src/promises.ts"],"names":[],"mappings":";;;AAkBA,0BAOC;AAaD,kCAUC;AASD,oDAYC;AAnEM,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,UAO9B,EAAE,EAAE,EAAE,CACN,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;IAC1B,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAEtC,IAAI,OAAO,CAAC,KAAK,IAAK,KAAa,CAAC,KAAK,EAAE,CAAC;QACvC,KAAa,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC;AACL,CAAC,CAAC,CAAC;AAdM,QAAA,KAAK,SAcX;AAEA,KAAK,UAAU,OAAO,CACzB,IAAsB,EACtB,OAAyC;IAEzC,GAAG,CAAC;QACA,MAAM,IAAI,EAAE,CAAC;IACjB,CAAC,QAAQ,MAAM,OAAO,EAAE,EAAE;AAC9B,CAAC;AAaD,SAAgB,WAAW;IACvB,IAAI,OAA4B,CAAC;IACjC,IAAI,MAA6B,CAAC;IAElC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE;QACnD,OAAO,GAAG,SAAS,CAAC;QACpB,MAAM,GAAG,QAAQ,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;AAChE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAI,EAAoB;IACxD,IAAI,cAAsC,CAAC;IAE3C,OAAO,GAAG,EAAE;QACR,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YAC/B,cAAc,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE;gBAC/B,cAAc,GAAG,SAAS,CAAC;YAC/B,CAAC,CAAC,CAAC;QACP,CAAC;QAED,OAAO,cAAc,CAAC;IAC1B,CAAC,CAAC;AACN,CAAC"}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function truncateWithEllipsis(str: string, length: number): string;
|
|
2
|
+
export declare function joinAnd(val: string[], options?: {
|
|
3
|
+
separator?: string;
|
|
4
|
+
finalSeparator?: string;
|
|
5
|
+
oxfordComma?: boolean;
|
|
6
|
+
}): string;
|
|
7
|
+
export declare function aOrAn(value: string): "a" | "an";
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.truncateWithEllipsis = truncateWithEllipsis;
|
|
4
|
+
exports.joinAnd = joinAnd;
|
|
5
|
+
exports.aOrAn = aOrAn;
|
|
6
|
+
function truncateWithEllipsis(str, length) {
|
|
7
|
+
if (str.length <= length) {
|
|
8
|
+
return str;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return str.slice(0, length - 3) + "...";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function joinAnd(val, options = {}) {
|
|
15
|
+
if (val.length === 1)
|
|
16
|
+
return val[0];
|
|
17
|
+
const separator = options.separator ?? ', ';
|
|
18
|
+
const finalSeparator = options.finalSeparator ?? 'and ';
|
|
19
|
+
const oxfordComma = options.oxfordComma ?? false;
|
|
20
|
+
return val.slice(0, -1).join(separator) +
|
|
21
|
+
(oxfordComma ? separator : ' ') +
|
|
22
|
+
finalSeparator +
|
|
23
|
+
val[val.length - 1];
|
|
24
|
+
}
|
|
25
|
+
const VOWEL_ISH = ['a', 'e', 'i', 'o', 'u', 'y'];
|
|
26
|
+
function aOrAn(value) {
|
|
27
|
+
if (VOWEL_ISH.includes(value[0].toLowerCase()))
|
|
28
|
+
return 'an';
|
|
29
|
+
else
|
|
30
|
+
return 'a';
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=text.js.map
|
package/dist/text.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../src/text.ts"],"names":[],"mappings":";;AAAA,oDAMC;AAED,0BAkBC;AAGD,sBAGC;AAhCD,SAAgB,oBAAoB,CAAC,GAAW,EAAE,MAAc;IAC5D,IAAI,GAAG,CAAC,MAAM,IAAI,MAAM,EAAE,CAAC;QACvB,OAAO,GAAG,CAAC;IACf,CAAC;SAAM,CAAC;QACJ,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;IAC5C,CAAC;AACL,CAAC;AAED,SAAgB,OAAO,CAAC,GAAa,EAAE,UAOnC,EAAE;IACF,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;IAEpC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC;IAC5C,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,MAAM,CAAC;IACxD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,KAAK,CAAC;IAEjD,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC;QACnC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC;QAC/B,cAAc;QACd,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC5B,CAAC;AAED,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AACjD,SAAgB,KAAK,CAAC,KAAa;IAC/B,IAAI,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;;QACvD,OAAO,GAAG,CAAC;AACpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@httptoolkit/util",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "A tiny utility package, sharing JS code widely used across HTTP Toolkit projects",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/httptoolkit/httptoolkit-js-util#readme",
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"typescript": "5.
|
|
31
|
+
"typescript": "5.9.3"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/errors.ts
CHANGED
package/src/index.ts
CHANGED
package/src/text.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function truncateWithEllipsis(str: string, length: number) {
|
|
2
|
+
if (str.length <= length) {
|
|
3
|
+
return str;
|
|
4
|
+
} else {
|
|
5
|
+
return str.slice(0, length - 3) + "...";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function joinAnd(val: string[], options: {
|
|
10
|
+
// Separator between all but the last value
|
|
11
|
+
separator?: string,
|
|
12
|
+
// Separator before last value (preceeded with space or `separator` if oxfordComma is true)
|
|
13
|
+
finalSeparator?: string,
|
|
14
|
+
// Should there be a separator between the penultimate value & finalSeparator?
|
|
15
|
+
oxfordComma?: boolean
|
|
16
|
+
} = {}) {
|
|
17
|
+
if (val.length === 1) return val[0];
|
|
18
|
+
|
|
19
|
+
const separator = options.separator ?? ', ';
|
|
20
|
+
const finalSeparator = options.finalSeparator ?? 'and ';
|
|
21
|
+
const oxfordComma = options.oxfordComma ?? false;
|
|
22
|
+
|
|
23
|
+
return val.slice(0, -1).join(separator) +
|
|
24
|
+
(oxfordComma ? separator : ' ') +
|
|
25
|
+
finalSeparator +
|
|
26
|
+
val[val.length - 1];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const VOWEL_ISH = ['a', 'e', 'i', 'o', 'u', 'y'];
|
|
30
|
+
export function aOrAn(value: string) {
|
|
31
|
+
if (VOWEL_ISH.includes(value[0].toLowerCase())) return 'an';
|
|
32
|
+
else return 'a';
|
|
33
|
+
}
|