@esportsplus/random 0.0.8 → 0.0.10
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/.editorconfig +9 -9
- package/.gitattributes +2 -2
- package/.github/dependabot.yml +23 -0
- package/.github/workflows/bump.yml +7 -0
- package/.github/workflows/publish.yml +14 -0
- package/build/item.js +9 -11
- package/package.json +20 -21
- package/src/coinflip.ts +5 -5
- package/src/index.ts +8 -8
- package/src/item.ts +32 -35
- package/src/range.ts +12 -12
- package/src/roll.ts +5 -5
- package/src/shuffle.ts +19 -19
- package/tsconfig.json +10 -9
package/.editorconfig
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
root = true
|
|
2
|
-
|
|
3
|
-
[*]
|
|
4
|
-
indent_style = space
|
|
5
|
-
indent_size = 4
|
|
6
|
-
charset = utf-8
|
|
7
|
-
trim_trailing_whitespace = true
|
|
8
|
-
insert_final_newline = true
|
|
9
|
-
end_of_line = lf
|
|
1
|
+
root = true
|
|
2
|
+
|
|
3
|
+
[*]
|
|
4
|
+
indent_style = space
|
|
5
|
+
indent_size = 4
|
|
6
|
+
charset = utf-8
|
|
7
|
+
trim_trailing_whitespace = true
|
|
8
|
+
insert_final_newline = true
|
|
9
|
+
end_of_line = lf
|
package/.gitattributes
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
# Auto detect text files and perform LF normalization
|
|
2
|
-
* text=auto
|
|
1
|
+
# Auto detect text files and perform LF normalization
|
|
2
|
+
* text=auto
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
registries:
|
|
8
|
+
npm-npmjs:
|
|
9
|
+
token: ${{secrets.NPM_TOKEN}}
|
|
10
|
+
type: npm-registry
|
|
11
|
+
url: https://registry.npmjs.org
|
|
12
|
+
updates:
|
|
13
|
+
- package-ecosystem: "npm"
|
|
14
|
+
directory: "/"
|
|
15
|
+
groups:
|
|
16
|
+
production-dependencies:
|
|
17
|
+
dependency-type: "production"
|
|
18
|
+
development-dependencies:
|
|
19
|
+
dependency-type: "development"
|
|
20
|
+
registries:
|
|
21
|
+
- npm-npmjs
|
|
22
|
+
schedule:
|
|
23
|
+
interval: "daily"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
name: publish to npm
|
|
2
|
+
on:
|
|
3
|
+
release:
|
|
4
|
+
types: [published]
|
|
5
|
+
workflow_dispatch:
|
|
6
|
+
workflow_run:
|
|
7
|
+
workflows: [bump]
|
|
8
|
+
types:
|
|
9
|
+
- completed
|
|
10
|
+
jobs:
|
|
11
|
+
publish:
|
|
12
|
+
secrets:
|
|
13
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_PUBLISHING }}
|
|
14
|
+
uses: esportsplus/workflows/.github/workflows/publish.yml@main
|
package/build/item.js
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import { rng } from '@esportsplus/crypto';
|
|
2
|
-
const BASIS_POINTS = 10000;
|
|
3
2
|
export default (items, weights) => {
|
|
4
3
|
if (weights === undefined) {
|
|
5
4
|
return items[Math.floor(rng() * items.length)];
|
|
6
5
|
}
|
|
7
|
-
|
|
6
|
+
let n = items.length, random = 0;
|
|
7
|
+
if (n !== weights.length) {
|
|
8
8
|
throw new Error('Random: each item requires a weight');
|
|
9
9
|
}
|
|
10
|
-
let
|
|
11
|
-
|
|
12
|
-
total -= weights[i];
|
|
10
|
+
for (let i = 0; i < n; i++) {
|
|
11
|
+
random += weights[i];
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (random <= total) {
|
|
13
|
+
let current = 0;
|
|
14
|
+
random *= rng();
|
|
15
|
+
for (let i = 0; i < n; i++) {
|
|
16
|
+
current += weights[i];
|
|
17
|
+
if (random <= current) {
|
|
20
18
|
return items[i];
|
|
21
19
|
}
|
|
22
20
|
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
{
|
|
2
|
-
"author": "ICJR",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"author": "ICJR",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"@esportsplus/crypto": "^0.0.4"
|
|
5
|
+
},
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@esportsplus/typescript": "^0.0.21"
|
|
8
|
+
},
|
|
9
|
+
"main": "build/index.js",
|
|
10
|
+
"name": "@esportsplus/random",
|
|
11
|
+
"private": false,
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc && tsc-alias",
|
|
14
|
+
"-": "-",
|
|
15
|
+
"prepare": "npm run build",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"types": "build/index.d.ts",
|
|
19
|
+
"version": "0.0.10"
|
|
20
|
+
}
|
package/src/coinflip.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { rng } from '@esportsplus/crypto';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export default () => {
|
|
5
|
-
return rng() < 0.5;
|
|
1
|
+
import { rng } from '@esportsplus/crypto';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default () => {
|
|
5
|
+
return rng() < 0.5;
|
|
6
6
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import coinflip from './coinflip';
|
|
2
|
-
import item from './item';
|
|
3
|
-
import range from './range';
|
|
4
|
-
import roll from './roll';
|
|
5
|
-
import shuffle from './shuffle';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export default { coinflip, item, range, roll, shuffle };
|
|
1
|
+
import coinflip from './coinflip';
|
|
2
|
+
import item from './item';
|
|
3
|
+
import range from './range';
|
|
4
|
+
import roll from './roll';
|
|
5
|
+
import shuffle from './shuffle';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export default { coinflip, item, range, roll, shuffle };
|
|
9
9
|
export { coinflip, item, range, roll, shuffle };
|
package/src/item.ts
CHANGED
|
@@ -1,36 +1,33 @@
|
|
|
1
|
-
import { rng } from '@esportsplus/crypto';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (
|
|
13
|
-
throw new Error('Random: each item requires a weight');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
throw new Error('Random: weighted item pick failed');
|
|
1
|
+
import { rng } from '@esportsplus/crypto';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default <T>(items: T[], weights?: number[]): T => {
|
|
5
|
+
if (weights === undefined) {
|
|
6
|
+
return items[ Math.floor(rng() * items.length) ];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let n = items.length,
|
|
10
|
+
random = 0;
|
|
11
|
+
|
|
12
|
+
if (n !== weights.length) {
|
|
13
|
+
throw new Error('Random: each item requires a weight');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < n; i++) {
|
|
17
|
+
random += weights[i];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let current = 0;
|
|
21
|
+
|
|
22
|
+
random *= rng();
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < n; i++) {
|
|
25
|
+
current += weights[i];
|
|
26
|
+
|
|
27
|
+
if (random <= current) {
|
|
28
|
+
return items[i];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
throw new Error('Random: weighted item pick failed');
|
|
36
33
|
};
|
package/src/range.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { rng } from '@esportsplus/crypto';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export default (min: number, max: number, integer = false) => {
|
|
5
|
-
if (!integer) {
|
|
6
|
-
return rng() * (max - min) + min;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
min = Math.ceil(min);
|
|
10
|
-
max = Math.floor(max) + 1;
|
|
11
|
-
|
|
12
|
-
return Math.floor(rng() * (max - min) + min);
|
|
1
|
+
import { rng } from '@esportsplus/crypto';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default (min: number, max: number, integer = false) => {
|
|
5
|
+
if (!integer) {
|
|
6
|
+
return rng() * (max - min) + min;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
min = Math.ceil(min);
|
|
10
|
+
max = Math.floor(max) + 1;
|
|
11
|
+
|
|
12
|
+
return Math.floor(rng() * (max - min) + min);
|
|
13
13
|
};
|
package/src/roll.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { rng } from '@esportsplus/crypto';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export default (percentage: number) => {
|
|
5
|
-
return rng() <= percentage;
|
|
1
|
+
import { rng } from '@esportsplus/crypto';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default (percentage: number) => {
|
|
5
|
+
return rng() <= percentage;
|
|
6
6
|
};
|
package/src/shuffle.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { rng } from '@esportsplus/crypto';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// Fisher-Yates shuffle
|
|
5
|
-
// - https://wikipedia.org/wiki/Fisher-Yates_shuffle
|
|
6
|
-
export default (values: any[]) => {
|
|
7
|
-
let n = values.length,
|
|
8
|
-
random: number,
|
|
9
|
-
value;
|
|
10
|
-
|
|
11
|
-
while (--n > 0) {
|
|
12
|
-
random = Math.floor(rng() * (n + 1));
|
|
13
|
-
value = values[random];
|
|
14
|
-
|
|
15
|
-
values[random] = values[n];
|
|
16
|
-
values[n] = value;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return values;
|
|
1
|
+
import { rng } from '@esportsplus/crypto';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
// Fisher-Yates shuffle
|
|
5
|
+
// - https://wikipedia.org/wiki/Fisher-Yates_shuffle
|
|
6
|
+
export default (values: any[]) => {
|
|
7
|
+
let n = values.length,
|
|
8
|
+
random: number,
|
|
9
|
+
value;
|
|
10
|
+
|
|
11
|
+
while (--n > 0) {
|
|
12
|
+
random = Math.floor(rng() * (n + 1));
|
|
13
|
+
value = values[random];
|
|
14
|
+
|
|
15
|
+
values[random] = values[n];
|
|
16
|
+
values[n] = value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return values;
|
|
20
20
|
};
|
package/tsconfig.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"baseUrl": ".",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".",
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"declarationDir": "build",
|
|
6
|
+
"outDir": "build"
|
|
7
|
+
},
|
|
8
|
+
"exclude": ["node_modules"],
|
|
9
|
+
"extends": "./node_modules/@esportsplus/typescript/tsconfig.base.json",
|
|
10
|
+
"include": ["src"]
|
|
10
11
|
}
|