ransyr 1.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/LICENSE +21 -0
- package/README.md +26 -0
- package/main.js +52 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kyusora
|
|
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,26 @@
|
|
|
1
|
+
# ransyr
|
|
2
|
+
|
|
3
|
+
  
|
|
4
|
+
|
|
5
|
+
Python-like random utilities for JavaScript, designed to make your life easier 😎
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Description
|
|
10
|
+
|
|
11
|
+
`ransyr` dibuat untuk mempermudah penggunaan random di JavaScript, **meniru gaya Python**.
|
|
12
|
+
Daripada harus menulis:
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
const choice = arr[Math.floor(Math.random() * arr.length)];
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Contoh Penggunaan
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
import random from 'ransyr';
|
|
22
|
+
|
|
23
|
+
const arr = ["apple", "banana", "cherry"];
|
|
24
|
+
console.log(random.choice(arr));
|
|
25
|
+
console.log(random.random());
|
|
26
|
+
```
|
package/main.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
|
|
2
|
+
export default class ransyr {
|
|
3
|
+
|
|
4
|
+
static random() {
|
|
5
|
+
return Math.random();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static sample(arr, k) {
|
|
9
|
+
if (k > arr.length) {
|
|
10
|
+
throw new RangeError("sample size larger than population");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const copy = [...arr];
|
|
14
|
+
const result = [];
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < k; i++) {
|
|
17
|
+
const idx = Math.floor(Math.random() * copy.length);
|
|
18
|
+
result.push(copy[idx]);
|
|
19
|
+
copy.splice(idx, 1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static randint(min, max) {
|
|
26
|
+
if (min > max) [min,
|
|
27
|
+
max] = [max,
|
|
28
|
+
min];
|
|
29
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static choice(arr) {
|
|
33
|
+
if (!arr.length) return undefined;
|
|
34
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static shuffle(arr) {
|
|
38
|
+
const copy = [...arr];
|
|
39
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
40
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
41
|
+
[copy[i],
|
|
42
|
+
copy[j]] = [copy[j],
|
|
43
|
+
copy[i]];
|
|
44
|
+
}
|
|
45
|
+
return copy;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static chance(percent) {
|
|
49
|
+
return Math.random() * 100 < percent;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ransyr",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "python like random utilities for make things easier",
|
|
5
|
+
"main": "main.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/kyusora/ransyr.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"random",
|
|
15
|
+
"ransyr",
|
|
16
|
+
"tools",
|
|
17
|
+
"utilities",
|
|
18
|
+
"python",
|
|
19
|
+
"js"
|
|
20
|
+
],
|
|
21
|
+
"author": "kyusora",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/kyusora/ransyr/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/kyusora/ransyr#readme"
|
|
27
|
+
}
|