@jsopen/objects 2.0.2 → 2.1.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/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## Changelog
2
2
 
3
- ### [v2.0.2](https://github.com/panates/jsopen-objects/compare/v2.0.1...v2.0.2) -
3
+ ### [v2.1.0](https://github.com/panates/jsopen-objects/compare/v2.0.2...v2.1.0) -
4
4
 
5
- #### 🛠 Refactoring and Updates
5
+ #### 🚀 New Features
6
6
 
7
- - refactor: Minor typing changes @Eray Hanoğlu
7
+ - feat: Added `updateErrorMessage` helper method @Eray Hanoğlu
package/cjs/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateErrorMessage = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  tslib_1.__exportStar(require("./clone.js"), exports);
5
6
  tslib_1.__exportStar(require("./is-object.js"), exports);
6
7
  tslib_1.__exportStar(require("./merge.js"), exports);
7
8
  tslib_1.__exportStar(require("./omit.js"), exports);
8
9
  tslib_1.__exportStar(require("./type-guards.js"), exports);
10
+ var update_error_message_js_1 = require("./update-error-message.js");
11
+ Object.defineProperty(exports, "updateErrorMessage", { enumerable: true, get: function () { return update_error_message_js_1.updateErrorMessage; } });
@@ -1 +1 @@
1
- {"root":["../../src/clone.ts","../../src/index.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts","../../src/type-guards.ts"],"version":"5.9.2"}
1
+ {"root":["../../src/clone.ts","../../src/index.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts","../../src/type-guards.ts","../../src/update-error-message.ts"],"version":"5.9.3"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateErrorMessage = updateErrorMessage;
4
+ exports.updateErrorMessageFallback = updateErrorMessageFallback;
5
+ /**
6
+ * Updates the error message and stack trace at sametime.
7
+ * @param err
8
+ * @param newMessage
9
+ */
10
+ function updateErrorMessage(err, newMessage) {
11
+ err.message = String(newMessage);
12
+ /** V8 */
13
+ if (typeof Error.captureStackTrace === 'function') {
14
+ Error.captureStackTrace(err);
15
+ return;
16
+ }
17
+ /** Other engines */
18
+ return updateErrorMessageFallback(err, newMessage);
19
+ }
20
+ /**
21
+ * Updates the error message and stack trace at sametime.
22
+ * @param err
23
+ * @param newMessage
24
+ */
25
+ function updateErrorMessageFallback(err, newMessage) {
26
+ err.message = String(newMessage);
27
+ /** Other engines */
28
+ const stack = typeof err.stack === 'string' ? err.stack : null;
29
+ if (!stack)
30
+ return err;
31
+ const name = err.name || 'Error';
32
+ const lines = stack.split('\n');
33
+ const firstFrameIdx = lines.findIndex(l => /^\s*at\s+/.test(l));
34
+ if (firstFrameIdx === -1) {
35
+ const msgLines = String(newMessage).split(/\r?\n/);
36
+ lines[0] = `${name}: ${msgLines[0] ?? ''}`;
37
+ lines.splice(1, 0, ...msgLines.slice(1));
38
+ err.stack = lines.join('\n');
39
+ return err;
40
+ }
41
+ const frameLines = lines.slice(firstFrameIdx);
42
+ const msgLines = String(newMessage).split(/\r?\n/);
43
+ const newHead = [`${name}: ${msgLines[0] ?? ''}`, ...msgLines.slice(1)];
44
+ err.stack = [...newHead, ...frameLines].join('\n');
45
+ return err;
46
+ }
package/esm/index.js CHANGED
@@ -3,3 +3,4 @@ export * from './is-object.js';
3
3
  export * from './merge.js';
4
4
  export * from './omit.js';
5
5
  export * from './type-guards.js';
6
+ export { updateErrorMessage } from './update-error-message.js';
@@ -1 +1 @@
1
- {"root":["../../src/clone.ts","../../src/index.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts","../../src/type-guards.ts"],"version":"5.9.2"}
1
+ {"root":["../../src/clone.ts","../../src/index.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts","../../src/type-guards.ts","../../src/update-error-message.ts"],"version":"5.9.3"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Updates the error message and stack trace at sametime.
3
+ * @param err
4
+ * @param newMessage
5
+ */
6
+ export function updateErrorMessage(err, newMessage) {
7
+ err.message = String(newMessage);
8
+ /** V8 */
9
+ if (typeof Error.captureStackTrace === 'function') {
10
+ Error.captureStackTrace(err);
11
+ return;
12
+ }
13
+ /** Other engines */
14
+ return updateErrorMessageFallback(err, newMessage);
15
+ }
16
+ /**
17
+ * Updates the error message and stack trace at sametime.
18
+ * @param err
19
+ * @param newMessage
20
+ */
21
+ export function updateErrorMessageFallback(err, newMessage) {
22
+ err.message = String(newMessage);
23
+ /** Other engines */
24
+ const stack = typeof err.stack === 'string' ? err.stack : null;
25
+ if (!stack)
26
+ return err;
27
+ const name = err.name || 'Error';
28
+ const lines = stack.split('\n');
29
+ const firstFrameIdx = lines.findIndex(l => /^\s*at\s+/.test(l));
30
+ if (firstFrameIdx === -1) {
31
+ const msgLines = String(newMessage).split(/\r?\n/);
32
+ lines[0] = `${name}: ${msgLines[0] ?? ''}`;
33
+ lines.splice(1, 0, ...msgLines.slice(1));
34
+ err.stack = lines.join('\n');
35
+ return err;
36
+ }
37
+ const frameLines = lines.slice(firstFrameIdx);
38
+ const msgLines = String(newMessage).split(/\r?\n/);
39
+ const newHead = [`${name}: ${msgLines[0] ?? ''}`, ...msgLines.slice(1)];
40
+ err.stack = [...newHead, ...frameLines].join('\n');
41
+ return err;
42
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jsopen/objects",
3
3
  "description": "Helper utilities for working with JavaScript objects and arrays",
4
- "version": "2.0.2",
4
+ "version": "2.1.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/types/index.d.cts CHANGED
@@ -3,3 +3,4 @@ export * from './is-object.js';
3
3
  export * from './merge.js';
4
4
  export * from './omit.js';
5
5
  export * from './type-guards.js';
6
+ export { updateErrorMessage } from './update-error-message.js';
package/types/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './is-object.js';
3
3
  export * from './merge.js';
4
4
  export * from './omit.js';
5
5
  export * from './type-guards.js';
6
+ export { updateErrorMessage } from './update-error-message.js';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Updates the error message and stack trace at sametime.
3
+ * @param err
4
+ * @param newMessage
5
+ */
6
+ export declare function updateErrorMessage(err: Error, newMessage: string): Error | undefined;
7
+ /**
8
+ * Updates the error message and stack trace at sametime.
9
+ * @param err
10
+ * @param newMessage
11
+ */
12
+ export declare function updateErrorMessageFallback(err: Error, newMessage: string): Error;