@oliversalzburg/js-utils 0.6.0 → 0.7.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.
Files changed (172) hide show
  1. package/package.json +5 -5
  2. package/lib/async/async.d.ts +0 -44
  3. package/lib/async/async.js +0 -76
  4. package/lib/async/async.js.map +0 -1
  5. package/lib/async/async.test.d.ts +0 -1
  6. package/lib/async/async.test.js +0 -26
  7. package/lib/async/async.test.js.map +0 -1
  8. package/lib/core.d.ts +0 -43
  9. package/lib/core.js +0 -2
  10. package/lib/core.js.map +0 -1
  11. package/lib/data/array.d.ts +0 -45
  12. package/lib/data/array.js +0 -70
  13. package/lib/data/array.js.map +0 -1
  14. package/lib/data/nil.d.ts +0 -92
  15. package/lib/data/nil.js +0 -131
  16. package/lib/data/nil.js.map +0 -1
  17. package/lib/data/random.d.ts +0 -111
  18. package/lib/data/random.js +0 -377
  19. package/lib/data/random.js.map +0 -1
  20. package/lib/data/string.d.ts +0 -30
  21. package/lib/data/string.js +0 -52
  22. package/lib/data/string.js.map +0 -1
  23. package/lib/data/tree.d.ts +0 -28
  24. package/lib/data/tree.js +0 -45
  25. package/lib/data/tree.js.map +0 -1
  26. package/lib/device/shake.d.ts +0 -79
  27. package/lib/device/shake.js +0 -135
  28. package/lib/device/shake.js.map +0 -1
  29. package/lib/dom/core.d.ts +0 -21
  30. package/lib/dom/core.js +0 -41
  31. package/lib/dom/core.js.map +0 -1
  32. package/lib/errors/AbstractError.d.ts +0 -29
  33. package/lib/errors/AbstractError.js +0 -59
  34. package/lib/errors/AbstractError.js.map +0 -1
  35. package/lib/errors/InternalError.d.ts +0 -19
  36. package/lib/errors/InternalError.js +0 -38
  37. package/lib/errors/InternalError.js.map +0 -1
  38. package/lib/errors/InvalidArgumentError.d.ts +0 -13
  39. package/lib/errors/InvalidArgumentError.js +0 -22
  40. package/lib/errors/InvalidArgumentError.js.map +0 -1
  41. package/lib/errors/InvalidOperationError.d.ts +0 -13
  42. package/lib/errors/InvalidOperationError.js +0 -22
  43. package/lib/errors/InvalidOperationError.js.map +0 -1
  44. package/lib/errors/NotImplementedError.d.ts +0 -12
  45. package/lib/errors/NotImplementedError.js +0 -21
  46. package/lib/errors/NotImplementedError.js.map +0 -1
  47. package/lib/errors/PermissionViolationError.d.ts +0 -13
  48. package/lib/errors/PermissionViolationError.js +0 -22
  49. package/lib/errors/PermissionViolationError.js.map +0 -1
  50. package/lib/errors/ResourceConflictError.d.ts +0 -12
  51. package/lib/errors/ResourceConflictError.js +0 -21
  52. package/lib/errors/ResourceConflictError.js.map +0 -1
  53. package/lib/errors/UnknownError.d.ts +0 -16
  54. package/lib/errors/UnknownError.js +0 -25
  55. package/lib/errors/UnknownError.js.map +0 -1
  56. package/lib/errors/console.d.ts +0 -7
  57. package/lib/errors/console.js +0 -13
  58. package/lib/errors/console.js.map +0 -1
  59. package/lib/errors/error-serializer.d.ts +0 -45
  60. package/lib/errors/error-serializer.js +0 -71
  61. package/lib/errors/error-serializer.js.map +0 -1
  62. package/lib/errors/error-serializer.test.d.ts +0 -1
  63. package/lib/errors/error-serializer.test.js +0 -109
  64. package/lib/errors/error-serializer.test.js.map +0 -1
  65. package/lib/errors/stream.d.ts +0 -7
  66. package/lib/errors/stream.js +0 -15
  67. package/lib/errors/stream.js.map +0 -1
  68. package/lib/errors/tools.d.ts +0 -10
  69. package/lib/errors/tools.js +0 -13
  70. package/lib/errors/tools.js.map +0 -1
  71. package/lib/events/event.d.ts +0 -9
  72. package/lib/events/event.js +0 -10
  73. package/lib/events/event.js.map +0 -1
  74. package/lib/format/bytes.d.ts +0 -118
  75. package/lib/format/bytes.js +0 -99
  76. package/lib/format/bytes.js.map +0 -1
  77. package/lib/format/bytes.test.d.ts +0 -1
  78. package/lib/format/bytes.test.js +0 -138
  79. package/lib/format/bytes.test.js.map +0 -1
  80. package/lib/format/count.d.ts +0 -14
  81. package/lib/format/count.js +0 -50
  82. package/lib/format/count.js.map +0 -1
  83. package/lib/format/count.test.d.ts +0 -1
  84. package/lib/format/count.test.js +0 -42
  85. package/lib/format/count.test.js.map +0 -1
  86. package/lib/format/milliseconds.d.ts +0 -100
  87. package/lib/format/milliseconds.js +0 -192
  88. package/lib/format/milliseconds.js.map +0 -1
  89. package/lib/format/milliseconds.test.d.ts +0 -1
  90. package/lib/format/milliseconds.test.js +0 -343
  91. package/lib/format/milliseconds.test.js.map +0 -1
  92. package/lib/format/string.d.ts +0 -24
  93. package/lib/format/string.js +0 -30
  94. package/lib/format/string.js.map +0 -1
  95. package/lib/format/string.test.d.ts +0 -1
  96. package/lib/format/string.test.js +0 -17
  97. package/lib/format/string.test.js.map +0 -1
  98. package/lib/graphics/canvas-sandbox-mp.d.ts +0 -258
  99. package/lib/graphics/canvas-sandbox-mp.js +0 -279
  100. package/lib/graphics/canvas-sandbox-mp.js.map +0 -1
  101. package/lib/graphics/canvas-sandbox.d.ts +0 -255
  102. package/lib/graphics/canvas-sandbox.js +0 -361
  103. package/lib/graphics/canvas-sandbox.js.map +0 -1
  104. package/lib/graphics/canvas.d.ts +0 -57
  105. package/lib/graphics/canvas.js +0 -37
  106. package/lib/graphics/canvas.js.map +0 -1
  107. package/lib/graphics/canvas2d-headless.d.ts +0 -85
  108. package/lib/graphics/canvas2d-headless.js +0 -193
  109. package/lib/graphics/canvas2d-headless.js.map +0 -1
  110. package/lib/graphics/canvas2d.d.ts +0 -162
  111. package/lib/graphics/canvas2d.js +0 -328
  112. package/lib/graphics/canvas2d.js.map +0 -1
  113. package/lib/graphics/canvas3d.d.ts +0 -41
  114. package/lib/graphics/canvas3d.js +0 -67
  115. package/lib/graphics/canvas3d.js.map +0 -1
  116. package/lib/graphics/color.d.ts +0 -22
  117. package/lib/graphics/color.js +0 -42
  118. package/lib/graphics/color.js.map +0 -1
  119. package/lib/graphics/core.d.ts +0 -89
  120. package/lib/graphics/core.js +0 -146
  121. package/lib/graphics/core.js.map +0 -1
  122. package/lib/graphics/palette-sampler.d.ts +0 -7
  123. package/lib/graphics/palette-sampler.js +0 -21
  124. package/lib/graphics/palette-sampler.js.map +0 -1
  125. package/lib/graphics/palette.d.ts +0 -120
  126. package/lib/graphics/palette.js +0 -494
  127. package/lib/graphics/palette.js.map +0 -1
  128. package/lib/graphics/palette.test.d.ts +0 -1
  129. package/lib/graphics/palette.test.js +0 -43
  130. package/lib/graphics/palette.test.js.map +0 -1
  131. package/lib/graphics/render-loop.d.ts +0 -80
  132. package/lib/graphics/render-loop.js +0 -120
  133. package/lib/graphics/render-loop.js.map +0 -1
  134. package/lib/graphics/shader.d.ts +0 -56
  135. package/lib/graphics/shader.js +0 -80
  136. package/lib/graphics/shader.js.map +0 -1
  137. package/lib/log/log.d.ts +0 -11
  138. package/lib/log/log.js +0 -2
  139. package/lib/log/log.js.map +0 -1
  140. package/lib/log/report.d.ts +0 -57
  141. package/lib/log/report.js +0 -88
  142. package/lib/log/report.js.map +0 -1
  143. package/lib/math/core.d.ts +0 -85
  144. package/lib/math/core.js +0 -125
  145. package/lib/math/core.js.map +0 -1
  146. package/lib/math/easing.d.ts +0 -204
  147. package/lib/math/easing.js +0 -332
  148. package/lib/math/easing.js.map +0 -1
  149. package/lib/math/euler.d.ts +0 -18
  150. package/lib/math/euler.js +0 -22
  151. package/lib/math/euler.js.map +0 -1
  152. package/lib/math/matrix3.d.ts +0 -217
  153. package/lib/math/matrix3.js +0 -508
  154. package/lib/math/matrix3.js.map +0 -1
  155. package/lib/math/quaternion.d.ts +0 -119
  156. package/lib/math/quaternion.js +0 -270
  157. package/lib/math/quaternion.js.map +0 -1
  158. package/lib/math/transformation.d.ts +0 -26
  159. package/lib/math/transformation.js +0 -31
  160. package/lib/math/transformation.js.map +0 -1
  161. package/lib/math/vector2.d.ts +0 -202
  162. package/lib/math/vector2.js +0 -310
  163. package/lib/math/vector2.js.map +0 -1
  164. package/lib/math/vector3.d.ts +0 -293
  165. package/lib/math/vector3.js +0 -492
  166. package/lib/math/vector3.js.map +0 -1
  167. package/lib/math/vector4.d.ts +0 -182
  168. package/lib/math/vector4.js +0 -302
  169. package/lib/math/vector4.js.map +0 -1
  170. package/lib/measurement/performance.d.ts +0 -13
  171. package/lib/measurement/performance.js +0 -19
  172. package/lib/measurement/performance.js.map +0 -1
@@ -1,16 +0,0 @@
1
- import { AbstractError } from "./AbstractError.js";
2
- /**
3
- * Used when an unknown, non-{@linkcode !Error}-like object was caught and converted into a
4
- * real {@linkcode !Error} instance.
5
- * Like when you catch a `throw "boom"`, we will convert the caught `"boom"`
6
- * into an {@linkcode UnknownError}.
7
- * To enrich an {@linkcode !Error}-like object that was caught, use the {@linkcode InternalError}.
8
- */
9
- export declare class UnknownError extends AbstractError {
10
- /**
11
- * Constructs a new {@linkcode UnknownError}.
12
- * @param message - The main error message.
13
- * @param status - The HTTP status code to return.
14
- */
15
- constructor(message: string, status?: number);
16
- }
@@ -1,25 +0,0 @@
1
- import { AbstractError } from "./AbstractError.js";
2
- /**
3
- * Used when an unknown, non-{@linkcode !Error}-like object was caught and converted into a
4
- * real {@linkcode !Error} instance.
5
- * Like when you catch a `throw "boom"`, we will convert the caught `"boom"`
6
- * into an {@linkcode UnknownError}.
7
- * To enrich an {@linkcode !Error}-like object that was caught, use the {@linkcode InternalError}.
8
- */
9
- export class UnknownError extends AbstractError {
10
- /**
11
- * Constructs a new {@linkcode UnknownError}.
12
- * @param message - The main error message.
13
- * @param status - The HTTP status code to return.
14
- */
15
- constructor(message, status = 500) {
16
- super("ERR_OS_UNKNOWN", message, status);
17
- this.name = "UnknownError";
18
- if (typeof Error.captureStackTrace !== "undefined") {
19
- // Capture a new stacktrace, otherwise it will include our base-class
20
- // constructor instead of the code location we're actually interested in.
21
- Error.captureStackTrace(this, UnknownError);
22
- }
23
- }
24
- }
25
- //# sourceMappingURL=UnknownError.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"UnknownError.js","sourceRoot":"","sources":["../../source/errors/UnknownError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;;;;GAMG;AACH,MAAM,OAAO,YAAa,SAAQ,aAAa;IAC7C;;;;OAIG;IACH,YAAY,OAAe,EAAE,MAAM,GAAG,GAAG;QACvC,KAAK,CAAC,gBAAgB,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QAEzC,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAE3B,IAAI,OAAO,KAAK,CAAC,iBAAiB,KAAK,WAAW,EAAE,CAAC;YACnD,qEAAqE;YACrE,yEAAyE;YACzE,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC;CACF"}
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a function that will receive errors (or anything else that was `throw`n)
3
- * and prints it to a {@linkcode !console}.
4
- * @param console - The console to print errors to.
5
- * @returns A function that will print errors to the console.
6
- */
7
- export declare const redirectErrorsToConsole: (console: Console) => ((error: unknown) => void);
@@ -1,13 +0,0 @@
1
- /**
2
- * Returns a function that will receive errors (or anything else that was `throw`n)
3
- * and prints it to a {@linkcode !console}.
4
- * @param console - The console to print errors to.
5
- * @returns A function that will print errors to the console.
6
- */
7
- export const redirectErrorsToConsole = (console) => {
8
- const printErrorsToConsole = (error) => {
9
- console.error(error);
10
- };
11
- return printErrorsToConsole;
12
- };
13
- //# sourceMappingURL=console.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"console.js","sourceRoot":"","sources":["../../source/errors/console.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,OAAgB,EAA8B,EAAE;IACtF,MAAM,oBAAoB,GAAG,CAAC,KAAc,EAAQ,EAAE;QACpD,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC,CAAC;IACF,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC"}
@@ -1,45 +0,0 @@
1
- import { AbstractError } from "./AbstractError.js";
2
- /**
3
- * The shape of an {@linkcode !Error} instance, after it has been serialized into a simple hash.
4
- */
5
- export type SerializedError = Record<string, Record<string, string> | string | undefined>;
6
- /**
7
- * Determine if the given unknown subject is an {@linkcode !Error} instance.
8
- * @param subject - The object to inspect.
9
- * @returns `true` if the subject is an {@linkcode !Error}, `false` otherwise.
10
- */
11
- export declare const isError: (subject: unknown) => subject is Error;
12
- /**
13
- * Returns an {@linkcode AbstractError} that best represents the passed subject.
14
- * If the passed subject is already an {@linkcode AbstractError}, it is returned as-is.
15
- * Otherwise, it will be converted into an appropriate error type.
16
- * @param subject - The subject to inspect
17
- * @returns An {@linkcode AbstractError} instance.
18
- */
19
- export declare const unknownToError: (subject: unknown) => AbstractError;
20
- /**
21
- * Serializes an error into a string.
22
- * @param error - The error to stringify.
23
- * @returns A string representing the error.
24
- */
25
- export declare const errorToString: (error: Error & {
26
- toString?: () => string;
27
- }) => string;
28
- /**
29
- * Serializes an error into a JSON string.
30
- * @param error - The error to stringify.
31
- * @returns A JSON string representing the error.
32
- */
33
- export declare const errorToJSON: (error: Error) => string;
34
- /**
35
- * Converts an error into a regular hash.
36
- * @param error - The error to convert.
37
- * @returns A new object that contains all the properties of the error.
38
- */
39
- export declare const errorToRecord: (error: Error) => Record<string, unknown>;
40
- /**
41
- * Serializes an error into a simpler shape.
42
- * @param error - The error to serialize.
43
- * @returns A simple representation of the error.
44
- */
45
- export declare const errorToSimpleSerializable: (error: AbstractError) => SerializedError;
@@ -1,71 +0,0 @@
1
- import { isNil } from "../data/nil.js";
2
- import { AbstractError } from "./AbstractError.js";
3
- import { InternalError } from "./InternalError.js";
4
- import { UnknownError } from "./UnknownError.js";
5
- /**
6
- * Determine if the given unknown subject is an {@linkcode !Error} instance.
7
- * @param subject - The object to inspect.
8
- * @returns `true` if the subject is an {@linkcode !Error}, `false` otherwise.
9
- */
10
- export const isError = (subject) => {
11
- return subject instanceof Error || Object.prototype.toString.call(subject) === "[object Error]";
12
- };
13
- /**
14
- * Returns an {@linkcode AbstractError} that best represents the passed subject.
15
- * If the passed subject is already an {@linkcode AbstractError}, it is returned as-is.
16
- * Otherwise, it will be converted into an appropriate error type.
17
- * @param subject - The subject to inspect
18
- * @returns An {@linkcode AbstractError} instance.
19
- */
20
- export const unknownToError = (subject) => {
21
- if (AbstractError.isAbstractError(subject)) {
22
- return subject;
23
- }
24
- if (isError(subject)) {
25
- return InternalError.fromError(subject);
26
- }
27
- return new UnknownError(String(subject));
28
- };
29
- /**
30
- * Serializes an error into a string.
31
- * @param error - The error to stringify.
32
- * @returns A string representing the error.
33
- */
34
- export const errorToString = (error) => {
35
- return isNil(error.toString) ? Object.prototype.toString.call(error) : error.toString();
36
- };
37
- /**
38
- * Serializes an error into a JSON string.
39
- * @param error - The error to stringify.
40
- * @returns A JSON string representing the error.
41
- */
42
- export const errorToJSON = (error) => {
43
- return JSON.stringify(error, Object.getOwnPropertyNames(error));
44
- };
45
- /**
46
- * Converts an error into a regular hash.
47
- * @param error - The error to convert.
48
- * @returns A new object that contains all the properties of the error.
49
- */
50
- export const errorToRecord = (error) => {
51
- const record = {};
52
- for (const propertyName of Object.getOwnPropertyNames(error)) {
53
- record[propertyName] = error[propertyName];
54
- }
55
- return record;
56
- };
57
- /**
58
- * Serializes an error into a simpler shape.
59
- * @param error - The error to serialize.
60
- * @returns A simple representation of the error.
61
- */
62
- export const errorToSimpleSerializable = (error) => {
63
- const serialized = {
64
- code: error.code,
65
- message: error.message,
66
- name: error.name,
67
- stack: error.stack,
68
- };
69
- return serialized;
70
- };
71
- //# sourceMappingURL=error-serializer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error-serializer.js","sourceRoot":"","sources":["../../source/errors/error-serializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,Y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AAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7D,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,YAAkC,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,KAAoB,EAAmB,EAAE;IACjF,MAAM,UAAU,GAAoB;QAClC,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,KAAK,EAAE,KAAK,CAAC,KAAK;KACnB,CAAC;IAEF,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- export {};
@@ -1,109 +0,0 @@
1
- import assert from "node:assert";
2
- import { it } from "node:test";
3
- import { errorToJSON, errorToRecord, errorToSimpleSerializable, isError, unknownToError, } from "./error-serializer.js";
4
- import { InternalError } from "./InternalError.js";
5
- import { InvalidArgumentError } from "./InvalidArgumentError.js";
6
- function generateDefaultError() {
7
- try {
8
- throw new Error("oops");
9
- }
10
- catch (error) {
11
- return error;
12
- }
13
- }
14
- it("unknown to Error: AbstractError", () => {
15
- const subjectError = new InvalidArgumentError("oops");
16
- const serialized = unknownToError(subjectError);
17
- assert.strictEqual(serialized.code, "ERR_OS_INVALID_ARGUMENT");
18
- assert.strictEqual(serialized.message, "oops");
19
- assert.strictEqual(serialized.name, "InvalidArgumentError");
20
- assert.strictEqual(typeof serialized.stack, "string");
21
- });
22
- it("unknown to Error: Error", () => {
23
- const subjectError = new Error("oops");
24
- const serialized = unknownToError(subjectError);
25
- assert.strictEqual(serialized.code, "ERR_OS_INTERNAL");
26
- assert.strictEqual(serialized.message, "oops");
27
- assert.strictEqual(serialized.name, "InternalError");
28
- assert.strictEqual(typeof serialized.stack, "string");
29
- });
30
- it("unknown to Error: string", () => {
31
- const subjectError = "oops";
32
- const serialized = unknownToError(subjectError);
33
- assert.strictEqual(serialized.code, "ERR_OS_UNKNOWN");
34
- assert.strictEqual(serialized.message, "oops");
35
- assert.strictEqual(serialized.name, "UnknownError");
36
- assert.strictEqual(typeof serialized.stack, "string");
37
- });
38
- it("unknown to Error: null", () => {
39
- const subjectError = null;
40
- const serialized = unknownToError(subjectError);
41
- assert.strictEqual(serialized.code, "ERR_OS_UNKNOWN");
42
- assert.strictEqual(serialized.message, "null");
43
- assert.strictEqual(serialized.name, "UnknownError");
44
- assert.strictEqual(typeof serialized.stack, "string");
45
- });
46
- it("serializes an error to JSON", () => {
47
- const subjectError = new Error("oops");
48
- const plain = JSON.stringify(subjectError);
49
- const serialized = errorToJSON(subjectError);
50
- // We assert.strictEqual more properties to have been serialized into the string.
51
- assert.notStrictEqual(serialized, plain);
52
- assert.strictEqual(plain.length < serialized.length, true);
53
- });
54
- it("serializes an error to a record: Error", () => {
55
- const subjectError = new Error("oops");
56
- const serialized = errorToRecord(subjectError);
57
- assert.strictEqual(isError(subjectError), true);
58
- assert.strictEqual(isError(serialized), false);
59
- assert.strictEqual(serialized.message, "oops");
60
- assert.strictEqual(typeof serialized.stack, "string");
61
- });
62
- it("serializes an error to a record: Default Error", () => {
63
- const subjectError = generateDefaultError();
64
- const serialized = errorToRecord(subjectError);
65
- assert.strictEqual(isError(subjectError), true);
66
- assert.strictEqual(isError(serialized), false);
67
- assert.strictEqual(serialized.code, undefined);
68
- assert.strictEqual(serialized.message, "oops");
69
- assert.strictEqual(serialized.name, undefined);
70
- assert.strictEqual(typeof serialized.stack, "string");
71
- });
72
- it("serializes an error to a record: Node Error", async function () {
73
- let error;
74
- try {
75
- assert(false, "oops");
76
- }
77
- catch (thrownError) {
78
- error = thrownError;
79
- }
80
- const subjectError = error;
81
- const serialized = errorToRecord(subjectError);
82
- assert.strictEqual(isError(subjectError), true);
83
- assert.strictEqual(isError(serialized), false);
84
- assert.strictEqual(serialized.code, "ERR_ASSERTION");
85
- assert.strictEqual(serialized.message, "oops");
86
- assert.strictEqual(serialized.name, "AssertionError");
87
- assert.strictEqual(typeof serialized.stack, "string");
88
- });
89
- it("serializes an error to a simple object for persistence: Error", () => {
90
- const subjectError = InternalError.fromError(new Error("oops"));
91
- const serialized = errorToSimpleSerializable(subjectError);
92
- assert.strictEqual(isError(subjectError), true);
93
- assert.strictEqual(isError(serialized), false);
94
- assert.strictEqual(serialized.code, "ERR_OS_INTERNAL");
95
- assert.strictEqual(serialized.message, "oops");
96
- assert.strictEqual(serialized.name, "InternalError");
97
- assert.strictEqual(typeof serialized.stack, "string");
98
- });
99
- it("serializes an error to a simple object for persistence: Node Error", () => {
100
- const subjectError = InternalError.fromError(generateDefaultError());
101
- const serialized = errorToSimpleSerializable(subjectError);
102
- assert.strictEqual(isError(subjectError), true);
103
- assert.strictEqual(isError(serialized), false);
104
- assert.strictEqual(serialized.code, "ERR_OS_INTERNAL");
105
- assert.strictEqual(serialized.message, "oops");
106
- assert.strictEqual(serialized.name, "InternalError");
107
- assert.strictEqual(typeof serialized.stack, "string");
108
- });
109
- //# sourceMappingURL=error-serializer.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error-serializer.test.js","sourceRoot":"","sources":["../../source/errors/error-serializer.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAC/B,OAAO,EACL,WAAW,EACX,aAAa,EACb,yBAAyB,EACzB,OAAO,EACP,cAAc,GACf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,SAAS,oBAAoB;IAC3B,IAAI,CAAC;QACH,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,KAAc,CAAC;IACxB,C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,oEAAoE,EAAE,GAAG,EAAE;IAC5E,MAAM,YAAY,GAAG,aAAa,CAAC,SAAS,CAAC,oBAAoB,EAAE,CAAC,CAAC;IACrE,MAAM,UAAU,GAAG,yBAAyB,CAAC,YAAY,CAAC,CAAC;IAC3D,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,IAAI,CAAC,CAAC;IAChD,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC,CAAC;IAE/C,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;IACvD,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/C,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;IACrD,MAAM,CAAC,WAAW,CAAC,OAAO,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AACxD,CAAC,CAAC,CAAC"}
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a function that will receive errors (or anything else that was `throw`n)
3
- * and prints it to a {@linkcode NodeJS.WriteStream}.
4
- * @param stream - The stream to print errors to.
5
- * @returns A function that will print errors to the stream.
6
- */
7
- export declare const redirectErrorsToStream: (stream: NodeJS.WriteStream) => ((error: unknown) => void);
@@ -1,15 +0,0 @@
1
- import { errorToString, unknownToError } from "./error-serializer.js";
2
- /**
3
- * Returns a function that will receive errors (or anything else that was `throw`n)
4
- * and prints it to a {@linkcode NodeJS.WriteStream}.
5
- * @param stream - The stream to print errors to.
6
- * @returns A function that will print errors to the stream.
7
- */
8
- export const redirectErrorsToStream = (stream) => {
9
- const printErrorsToStream = (unknownError) => {
10
- const error = unknownToError(unknownError);
11
- stream.write(errorToString(error) + "\n");
12
- };
13
- return printErrorsToStream;
14
- };
15
- //# sourceMappingURL=stream.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../source/errors/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEtE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,MAA0B,EAA8B,EAAE;IAC/F,MAAM,mBAAmB,GAAG,CAAC,YAAqB,EAAQ,EAAE;QAC1D,MAAM,KAAK,GAAG,cAAc,CAAC,YAAY,CAAC,CAAC;QAC3C,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC,CAAC;IACF,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC"}
@@ -1,10 +0,0 @@
1
- /**
2
- * General error-related tooling that fits no other module.
3
- * @module
4
- */
5
- /**
6
- * Throws the provided error.
7
- * @param error - The error that will be thrown.
8
- * @throws The provided error.
9
- */
10
- export declare const rethrow: (error: unknown) => never;
@@ -1,13 +0,0 @@
1
- /**
2
- * General error-related tooling that fits no other module.
3
- * @module
4
- */
5
- /**
6
- * Throws the provided error.
7
- * @param error - The error that will be thrown.
8
- * @throws The provided error.
9
- */
10
- export const rethrow = (error) => {
11
- throw error;
12
- };
13
- //# sourceMappingURL=tools.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../source/errors/tools.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAc,EAAE,EAAE;IACxC,MAAM,KAAK,CAAC;AACd,CAAC,CAAC"}
@@ -1,9 +0,0 @@
1
- /**
2
- * Creates a {@linkcode !CustomEvent} instance with the given arguments.
3
- * @param type - The name of the type of the event.
4
- * @param detail - The event details.
5
- * @returns The requested event object.
6
- */
7
- export declare const createEvent: <TEventType extends string, TDetail>(type: TEventType, detail: TDetail) => CustomEvent<TDetail> & {
8
- type: TEventType;
9
- };
@@ -1,10 +0,0 @@
1
- /**
2
- * Creates a {@linkcode !CustomEvent} instance with the given arguments.
3
- * @param type - The name of the type of the event.
4
- * @param detail - The event details.
5
- * @returns The requested event object.
6
- */
7
- export const createEvent = (type, detail) => {
8
- return new CustomEvent(type, { detail });
9
- };
10
- //# sourceMappingURL=event.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"event.js","sourceRoot":"","sources":["../../source/events/event.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,IAAgB,EAChB,MAAe,EAC8B,EAAE;IAC/C,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAgD,CAAC;AAC1F,CAAC,CAAC"}
@@ -1,118 +0,0 @@
1
- /**
2
- * This code was derived from https://github.com/sindresorhus/pretty-bytes/tree/71d686a4cefb314c13b57107de5add5789753900.
3
- * It is available under the MIT license.
4
- */
5
- /**
6
- * Options for the `formatBytes` function.
7
- */
8
- export interface FormatBytesOptions {
9
- /**
10
- * Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
11
- */
12
- signed?: boolean;
13
- /**
14
- * - If `false`: Output won't be localized.
15
- * - If `true`: Localize the output using the system/browser locale.
16
- * - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
17
- * - If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
18
- */
19
- locale?: boolean | string | ReadonlyArray<string>;
20
- /**
21
- * Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
22
- * @example
23
- * ```
24
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
25
- *
26
- * formatBytes(1337, {bits: true});
27
- * //=> '1.34 kbit'
28
- * ```
29
- */
30
- bits?: boolean;
31
- /**
32
- * Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
33
- * @example
34
- * ```
35
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
36
- *
37
- * formatBytes(1000, {binary: true});
38
- * //=> '1000 bit'
39
- *
40
- * formatBytes(1024, {binary: true});
41
- * //=> '1 kiB'
42
- * ```
43
- */
44
- binary?: boolean;
45
- /**
46
- * The minimum number of fraction digits to display.
47
- *
48
- * If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
49
- * @example
50
- * ```
51
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
52
- *
53
- * // Show the number with at least 3 fractional digits
54
- * formatBytes(1900, {minimumFractionDigits: 3});
55
- * //=> '1.900 kB'
56
- *
57
- * formatBytes(1900);
58
- * //=> '1.9 kB'
59
- * ```
60
- */
61
- minimumFractionDigits?: number;
62
- /**
63
- * The maximum number of fraction digits to display.
64
- *
65
- * If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
66
- * @example
67
- * ```
68
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
69
- *
70
- * // Show the number with at most 1 fractional digit
71
- * formatBytes(1920, {maximumFractionDigits: 1});
72
- * //=> '1.9 kB'
73
- *
74
- * formatBytes(1920);
75
- * //=> '1.92 kB'
76
- * ```
77
- */
78
- maximumFractionDigits?: number;
79
- /**
80
- * Put a space between the number and unit.
81
- * @example
82
- * ```
83
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
84
- *
85
- * formatBytes(1920, {space: false});
86
- * //=> '1.9kB'
87
- *
88
- * formatBytes(1920);
89
- * //=> '1.92 kB'
90
- * ```
91
- */
92
- space?: boolean;
93
- }
94
- /**
95
- * Convert bytes to a human readable string: `1337` → `1.34 kB`.
96
- * @param number - The number to format.
97
- * @param options - The options for formatting the number.
98
- * @returns The formatted string.
99
- * @example
100
- * ```
101
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
102
- *
103
- * formatBytes(1337);
104
- * //=> '1.34 kB'
105
- *
106
- * formatBytes(100);
107
- * //=> '100 B'
108
- *
109
- * // Display file size differences
110
- * formatBytes(42, {signed: true});
111
- * //=> '+42 B'
112
- *
113
- * // Localized output using German locale
114
- * formatBytes(1337, {locale: 'de'});
115
- * //=> '1,34 kB'
116
- * ```
117
- */
118
- export declare const formatBytes: (number: number, options?: Readonly<FormatBytesOptions>) => string;
@@ -1,99 +0,0 @@
1
- /**
2
- * This code was derived from https://github.com/sindresorhus/pretty-bytes/tree/71d686a4cefb314c13b57107de5add5789753900.
3
- * It is available under the MIT license.
4
- */
5
- const BYTE_UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
6
- const BIBYTE_UNITS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
7
- const BIT_UNITS = ["b", "kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"];
8
- const BIBIT_UNITS = ["b", "kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"];
9
- /**
10
- * Formats the given number using `Number#toLocaleString`.
11
- * - If locale is a string, the value is expected to be a locale-key (for example: `de`).
12
- * - If locale is true, the system default locale is used for translation.
13
- * - If no value for locale is specified, the number is returned unmodified.
14
- * @param number - The number to be formatted.
15
- * @param locale - The locale to use for formatting.
16
- * @param options - Formatting options.
17
- * @returns The formatted number.
18
- */
19
- const toLocaleStringInternal = (number, locale, options) => {
20
- let result = number;
21
- if (typeof locale === "string" || Array.isArray(locale)) {
22
- result = number.toLocaleString(locale, options);
23
- }
24
- else if (locale === true || options !== undefined) {
25
- result = number.toLocaleString(undefined, options);
26
- }
27
- return result.toString();
28
- };
29
- /**
30
- * Convert bytes to a human readable string: `1337` → `1.34 kB`.
31
- * @param number - The number to format.
32
- * @param options - The options for formatting the number.
33
- * @returns The formatted string.
34
- * @example
35
- * ```
36
- * import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';
37
- *
38
- * formatBytes(1337);
39
- * //=> '1.34 kB'
40
- *
41
- * formatBytes(100);
42
- * //=> '100 B'
43
- *
44
- * // Display file size differences
45
- * formatBytes(42, {signed: true});
46
- * //=> '+42 B'
47
- *
48
- * // Localized output using German locale
49
- * formatBytes(1337, {locale: 'de'});
50
- * //=> '1,34 kB'
51
- * ```
52
- */
53
- export const formatBytes = (number, options = { binary: false, bits: false, space: true }) => {
54
- if (!Number.isFinite(number)) {
55
- throw new TypeError(`Expected a finite number, got ${typeof number}: ${number.toString()}`);
56
- }
57
- options = {
58
- binary: false,
59
- bits: false,
60
- space: true,
61
- ...options,
62
- };
63
- const UNITS = options.bits
64
- ? options.binary
65
- ? BIBIT_UNITS
66
- : BIT_UNITS
67
- : options.binary
68
- ? BIBYTE_UNITS
69
- : BYTE_UNITS;
70
- const separator = options.space ? " " : "";
71
- if (options.signed && number === 0) {
72
- return ` 0${separator}${UNITS[0]}`;
73
- }
74
- const isNegative = number < 0;
75
- const prefix = isNegative ? "-" : options.signed ? "+" : "";
76
- if (isNegative) {
77
- number = -number;
78
- }
79
- let localeOptions;
80
- if (options.minimumFractionDigits !== undefined) {
81
- localeOptions = { minimumFractionDigits: options.minimumFractionDigits };
82
- }
83
- if (options.maximumFractionDigits !== undefined) {
84
- localeOptions = { maximumFractionDigits: options.maximumFractionDigits, ...localeOptions };
85
- }
86
- if (number < 1) {
87
- const numberString = toLocaleStringInternal(number, options.locale, localeOptions);
88
- return prefix + numberString + separator + UNITS[0];
89
- }
90
- const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
91
- number /= (options.binary ? 1024 : 1000) ** exponent;
92
- if (!localeOptions) {
93
- number = Number(number.toPrecision(3));
94
- }
95
- const numberString = toLocaleStringInternal(number, options.locale, localeOptions);
96
- const unit = UNITS[exponent];
97
- return prefix + numberString + separator + unit;
98
- };
99
- //# sourceMappingURL=bytes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../source/format/bytes.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkGH,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAEzE,MAAM,YAAY,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAEnF,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAExF,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAElG;;;;;;;;;GASG;AACH,MAAM,sBAAsB,GAAG,CAC7B,MAAuB,EACvB,MAA4D,EAC5D,OAA6C,EAC7C,EAAE;IACF,IAAI,MAAM,GAAG,MAAM,CAAC;IACpB,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACxD,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;SAAM,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACpD,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,OAAO,CAAC,CA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