die-statement 1.0.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/LICENSE +21 -0
- package/README.md +90 -0
- package/index.d.ts +1 -0
- package/index.js +13 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Gustavo Luiz Gregorio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# die() - Because Sometimes Code Needs to Just... Stop
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/entropy-particles)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
A simple and easy way to kill any application whenever you want, be it on a new line or as an inline statement!
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```javascript
|
|
11
|
+
npm i die-statement
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or just drop the function in your codebase. No npm bloat, no build step, no regrets. Here it is, i'll make it easy for ya!
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
function die(message = "Fatal Error") {
|
|
18
|
+
return (() => {
|
|
19
|
+
const error = new Error();
|
|
20
|
+
const match = error.stack?.match(/^(?!die).*@(.*:\d+:\d+)$/m);
|
|
21
|
+
|
|
22
|
+
if (match) {
|
|
23
|
+
console.log(message, "\n\nDied at:", match[1]);
|
|
24
|
+
} else {
|
|
25
|
+
console.log(message, "Died at (location unknown)");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
throw error;
|
|
29
|
+
})();
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
die(message?)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Parameters
|
|
40
|
+
|
|
41
|
+
- `message` (string, optional): Your last words before everything crashes. Defaults to `"Fatal Error"` if you're feeling uncreative.
|
|
42
|
+
|
|
43
|
+
### Returns
|
|
44
|
+
|
|
45
|
+
Nothing. It throws an Error. Your code stops here. Game over.
|
|
46
|
+
|
|
47
|
+
## Examples
|
|
48
|
+
|
|
49
|
+
**As a fallback (the fancy way):**
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
const necessaryElement = document.getElementById("#important") || die();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**When you've had enough (the direct way):**
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
if (userDidSomethingDumb) {
|
|
59
|
+
die("User did something unforgivable");
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**The dramatic exit:**
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
const config = loadConfig() || die("No config, no service. I quit.");
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## What It Does
|
|
70
|
+
|
|
71
|
+
When called, `die()` will:
|
|
72
|
+
|
|
73
|
+
1. Log your message (or the default one) to the console
|
|
74
|
+
2. Show you exactly where your code gave up on life (`Died at: file.js:42:10`)
|
|
75
|
+
3. Throw an Error to stop execution immediately
|
|
76
|
+
|
|
77
|
+
## Why Use This?
|
|
78
|
+
|
|
79
|
+
Sometimes `throw new Error()` is too many characters. Sometimes you want your failures to feel more... deliberate. Sometimes you just want to watch the world burn, but in a traceable way.
|
|
80
|
+
|
|
81
|
+
Also, if you've ever missed PHP's `die()` function and tried to use `throw new Error()` in a statement only to have JavaScript laugh at you—well, now you don't have to miss it anymore.
|
|
82
|
+
|
|
83
|
+
## Notes
|
|
84
|
+
|
|
85
|
+
- The stack trace parsing works in modern browsers. If it fails, you'll get `"(location unknown)"` instead. Even `die()` has its limits.
|
|
86
|
+
- This is blocking and synchronous. If you call it, everything stops. That's the point.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
_"To die, to sleep—no more—and by a sleep to say we end the heartache..."_ - Shakespeare, probably talking about JavaScript
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function die(message?: string): never;
|
package/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function die(message = "Fatal Error") {
|
|
2
|
+
return (() => {
|
|
3
|
+
const error = new Error();
|
|
4
|
+
const match = error.stack?.match(/^(?!die).*@(.*:\d+:\d+)$/m);
|
|
5
|
+
if (match) {
|
|
6
|
+
console.error(message, "\n\nDied at:", match[1]);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
console.error(message, "Died at (location unknown)");
|
|
10
|
+
}
|
|
11
|
+
throw error;
|
|
12
|
+
})();
|
|
13
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "die-statement",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple and easy way to kill any application whenever you want, be it on a new line or as an inline statement!",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"die",
|
|
7
|
+
"error",
|
|
8
|
+
"throw",
|
|
9
|
+
"fatal",
|
|
10
|
+
"exit",
|
|
11
|
+
"assert",
|
|
12
|
+
"inline",
|
|
13
|
+
"statement",
|
|
14
|
+
"fail",
|
|
15
|
+
"crash",
|
|
16
|
+
"stop",
|
|
17
|
+
"php-die",
|
|
18
|
+
"error-handling",
|
|
19
|
+
"guard",
|
|
20
|
+
"fallback",
|
|
21
|
+
"one-liner",
|
|
22
|
+
"utility",
|
|
23
|
+
"debugging",
|
|
24
|
+
"stack-trace",
|
|
25
|
+
"fatal-guard"
|
|
26
|
+
],
|
|
27
|
+
"author": "Gustavo Luiz Gregorio",
|
|
28
|
+
"homepage": "https://github.com/GustavoLGregorio/die-statement#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/GustavoLGregorio/die-statement/issues"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/GustavoLGregorio/die-statement.git"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"type": "commonjs",
|
|
38
|
+
"main": "index.js",
|
|
39
|
+
"files": [
|
|
40
|
+
"index.js",
|
|
41
|
+
"index.d.ts",
|
|
42
|
+
"LICENSE",
|
|
43
|
+
"README.md",
|
|
44
|
+
"package.json"
|
|
45
|
+
]
|
|
46
|
+
}
|