@tombell/4ad 0.0.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/LICENSE +28 -0
- package/README.md +1 -0
- package/dist/foo.d.ts +2 -0
- package/dist/foo.js +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +23 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Tom Bell
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# 4ad.js
|
package/dist/foo.d.ts
ADDED
package/dist/foo.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default 200;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const SYNTAX = /^(\d*)d(\d+)(?:\s*([+-])\s*(\d+))?$/;
|
|
2
|
+
export function parse(notation) {
|
|
3
|
+
const match = SYNTAX.exec(notation);
|
|
4
|
+
if (!match) {
|
|
5
|
+
throw new Error("invalid dice notation");
|
|
6
|
+
}
|
|
7
|
+
const count = parseInt(match[1], 10) || 1;
|
|
8
|
+
const sides = parseInt(match[2], 10);
|
|
9
|
+
const modSign = match[3];
|
|
10
|
+
const modValue = match[4] ? parseInt(match[4], 10) : 0;
|
|
11
|
+
return { count, sides, modSign, modValue };
|
|
12
|
+
}
|
|
13
|
+
export default function roll(notation) {
|
|
14
|
+
const { count, sides, modSign, modValue } = parse(notation);
|
|
15
|
+
const rolls = [];
|
|
16
|
+
for (let i = 0; i < count; i++) {
|
|
17
|
+
rolls.push(Math.floor(Math.random() * sides) + 1);
|
|
18
|
+
}
|
|
19
|
+
const sum = rolls.reduce((a, b) => a + b, 0);
|
|
20
|
+
const modifier = modSign === "-" ? -modValue : modValue;
|
|
21
|
+
const total = sum + modifier;
|
|
22
|
+
return { rolls, modifier, total };
|
|
23
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tombell/4ad",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A simple library to parse dice syntax for Four Against Darkness games.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dice",
|
|
7
|
+
"4ad",
|
|
8
|
+
"four against darkness"
|
|
9
|
+
],
|
|
10
|
+
"author": "Tom Bell <tomb@tomb.io>",
|
|
11
|
+
"license": "BSD-3-Clause",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"module": "./dist/esm/index.js",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "bunx tsc -p tsconfig.build.json",
|
|
19
|
+
"lint": "bunx eslint",
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"coverage": "bun test --coverage",
|
|
22
|
+
"prepublishOnly": "bun run build"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@eslint/js": "^9.27.0",
|
|
26
|
+
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
|
|
27
|
+
"@types/bun": "latest",
|
|
28
|
+
"eslint": "^9.27.0",
|
|
29
|
+
"eslint-config-prettier": "^10.1.5",
|
|
30
|
+
"eslint-plugin-prettier": "^5.4.0",
|
|
31
|
+
"jiti": "^2.4.2",
|
|
32
|
+
"prettier": "^3.5.3",
|
|
33
|
+
"typescript": "^5.8.3",
|
|
34
|
+
"typescript-eslint": "^8.33.0"
|
|
35
|
+
}
|
|
36
|
+
}
|