Skip to content

Commit 6f0f110

Browse files
committed
Merge branch 'dev'
2 parents 7c60a49 + cccf555 commit 6f0f110

File tree

3 files changed

+96
-30
lines changed

3 files changed

+96
-30
lines changed

changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- Initial Release
2929

3030
## Minor Releases:
31+
### [v1.3.0] - 2022 01 29
32+
#### Added
33+
- `flipCoin()` Generates a random boolean value
34+
- `randomCaseString()` Generates a true mixed-case random string of specified length
35+
#### Changed
36+
- `random()` Fixed bug where defaulting to randomly returning 0 or 1 always returned 0
37+
3138
## [v1.2.2] - 2021 10 24
3239
#### Changed
3340
- Changelog tweaks
@@ -74,7 +81,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7481

7582
<!-- LINKS -->
7683
<!-- RELEASES -->
77-
[Unreleased]: https://github.com/karnthis/make-random/compare/v1.2.2...dev
84+
[Unreleased]: https://github.com/karnthis/make-random/compare/v1.3.0...dev
85+
[v1.3.0]: https://github.com/karnthis/make-random/compare/v1.2.2...v1.3.0
7886
[v1.2.2]: https://github.com/karnthis/make-random/compare/v1.2.0...v1.2.2
7987
[v1.2.0]: https://github.com/karnthis/make-random/compare/v1.1.0...v1.2.0
8088
[v1.1.0]: https://github.com/karnthis/make-random/compare/v1.0.0...v1.1.0

libs/index.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ const { asUpper, asLower } = require('../data/letters.js')
1111
function foundation(base) {
1212
const byteCount = makeByteCount(base)
1313
return promiseCryptoRBytes(byteCount)
14-
.then(buf => buf.toString('hex'))
14+
.then(buf => buf.toString('hex'))
1515
}
1616

1717
function numberFoundation(base) {
1818
const trueBase = Math.abs(base)
1919
return foundation(trueBase)
20-
.then(hex => parseInt(hex, 16) % trueBase)
20+
.then(hex => {
21+
const val = parseInt(hex, 16)
22+
if (trueBase === 1) {
23+
return val % 2
24+
} else {
25+
return val % trueBase
26+
}
27+
})
2128
}
2229

2330
function stringFoundation(base) {
2431
const trueBase = Math.abs(base)
2532
return promiseCryptoRBytes(trueBase)
26-
.then(buf => buf.toString('hex'))
33+
.then(buf => buf.toString('hex'))
2734
}
2835

2936
function promiseCryptoRBytes(byteCount = 1) {
@@ -59,7 +66,11 @@ async function randomLetter(count = 3, source) {
5966
function random(v = 1) {
6067
const pureV = cleanInteger(v, 1)
6168
return numberFoundation(pureV)
62-
.then(res => (pureV < 0 && res !== 0) ? res * -1 : res)
69+
.then(res => (pureV < 0 && res !== 0) ? res * -1 : res)
70+
}
71+
function coinFlip() {
72+
return random()
73+
.then(num => num === 0)
6374
}
6475
function randomInRange(v1 = -100, v2 = 100) {
6576
v1 = cleanInteger(v1, 1)
@@ -76,7 +87,7 @@ function randomInRange(v1 = -100, v2 = 100) {
7687
}
7788
const baseValue = high - low
7889
return numberFoundation(baseValue)
79-
.then(res => (baseValue < 0) ? (res * -1) + low : res + low)
90+
.then(res => (baseValue < 0) ? (res * -1) + low : res + low)
8091
}
8192

8293
function randomAZString(len = 10, upper = true) {
@@ -88,7 +99,18 @@ function randomAZString(len = 10, upper = true) {
8899
}
89100
function randomString(len = 10) {
90101
return stringFoundation(len)
91-
.then(hex => hex.slice(0, len))
102+
.then(hex => hex.slice(0, len))
103+
}
104+
function randomCaseString(len = 10) {
105+
return stringFoundation(len)
106+
.then(hex => hex.slice(0, len))
107+
.then(async (str) => {
108+
return Promise.all(str
109+
.split('')
110+
.map(async (char) => (await coinFlip()) ? char : char.toUpperCase())
111+
)
112+
})
113+
.then(strArr => strArr.join(''))
92114
}
93115

94116
async function randomLatin(len = 5) {
@@ -102,29 +124,29 @@ async function randomLatin(len = 5) {
102124

103125
async function randomUUID() {
104126
return stringFoundation(16)
105-
.then(res => {
106-
const byteTo4 = res.slice(12,14)
107-
const maskOr = parseInt('01000000', 2)
108-
const maskAnd = parseInt('01001111', 2)
109-
const byteSet4 = (maskOr | (maskAnd & parseInt(byteTo4, 16)))
110-
return res.slice(0,12) + byteSet4.toString(16) + res.slice(14)
111-
})
112-
.then(res => {
113-
const bytePart2 = res.slice(16,18)
114-
const maskOr = parseInt('10000000', 2)
115-
const maskAnd = parseInt('10111111', 2)
116-
const byteSetPart2 = (maskOr | (maskAnd & parseInt(bytePart2, 16)))
117-
return res.slice(0,16) + byteSetPart2.toString(16) + res.slice(18)
118-
})
119-
.then(res => {
120-
return [
121-
res.slice(0,8),
122-
res.slice(8, 12),
123-
res.slice(12,16),
124-
res.slice(16,20),
125-
res.slice(20),
126-
].join("-").toUpperCase()
127-
})
127+
.then(res => {
128+
const byteTo4 = res.slice(12,14)
129+
const maskOr = parseInt('01000000', 2)
130+
const maskAnd = parseInt('01001111', 2)
131+
const byteSet4 = (maskOr | (maskAnd & parseInt(byteTo4, 16)))
132+
return res.slice(0,12) + byteSet4.toString(16) + res.slice(14)
133+
})
134+
.then(res => {
135+
const bytePart2 = res.slice(16,18)
136+
const maskOr = parseInt('10000000', 2)
137+
const maskAnd = parseInt('10111111', 2)
138+
const byteSetPart2 = (maskOr | (maskAnd & parseInt(bytePart2, 16)))
139+
return res.slice(0,16) + byteSetPart2.toString(16) + res.slice(18)
140+
})
141+
.then(res => {
142+
return [
143+
res.slice(0,8),
144+
res.slice(8, 12),
145+
res.slice(12,16),
146+
res.slice(16,20),
147+
res.slice(20),
148+
].join("-").toUpperCase()
149+
})
128150
}
129151

130152
// WRAPPERS
@@ -146,6 +168,7 @@ function randomWords(len) {
146168

147169
module.exports = {
148170
// WRAPPERS
171+
coinFlip,
149172
flexRange,
150173
setRange,
151174
azString,
@@ -155,6 +178,7 @@ module.exports = {
155178
randomInRange,
156179
randomString,
157180
randomAZString,
181+
randomCaseString,
158182
randomLatin,
159183
randomUUID
160184
}

readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ npm install make-random
1919
## Examples
2020
### Jump-to:
2121
- [random()](#random)
22+
- [coinFlip()](#coinFlip)
2223
- [randomInRange()](#randomInRange)
2324
- [randomAZString()](#randomAZString)
2425
- [randomString()](#randomString)
26+
- [randomCaseString()](#randomCaseString)
2527
- [randomLatin()](#randomLatin)
2628
- [randomUUID()](#randomUUID)
2729

@@ -54,6 +56,18 @@ random()
5456
.then(resp => console.log(resp))
5557
```
5658

59+
### coinFlip()
60+
61+
The `coinFlip()` method accepts no arguments. The method returns a random boolean value.
62+
63+
```javascript
64+
const { coinFlip } = require('make-random')
65+
66+
// Return a random boolean value
67+
coinFlip()
68+
.then(resp => console.log(resp))
69+
```
70+
5771
### randomInRange()
5872

5973
The `randomInRange()` method accepts any integers or integer-like strings as optional single or pair arguments. The method returns a true integer between the 2 arguments given. If only 1 argument is given, the random number will be between the argument value and 100. If no arguments are given, the method defaults to returning a random integer between -100 and 100. If the arguements are the same value (-10, "-10"), the method will return an integer between 0 and the passed arguments as if `random()` was used.
@@ -140,6 +154,26 @@ randomString('20')
140154
.then(resp => console.log(resp))
141155
```
142156

157+
### randomCaseString()
158+
159+
The `randomCaseString()` method accepts an optional integer or integer-like argument. It returns an alpha-numeric randomly mixed-case string of the specified length. If no argument is given, method defaults to a 10 character string.
160+
161+
```javascript
162+
const { randomCaseString } = require('make-random')
163+
164+
// Return 10 character mixed-case random string
165+
randomCaseString()
166+
.then(resp => console.log(resp))
167+
168+
// Return 15 character mixed-case random string
169+
randomCaseString(15)
170+
.then(resp => console.log(resp))
171+
172+
// Accept integer or integer-like string
173+
randomCaseString('20')
174+
.then(resp => console.log(resp))
175+
```
176+
143177
### randomLatin()
144178

145179
The `randomLatin()` method accepts an optional integer or integer-like argument. It returns a string with the specified number of random latin words. If no argument is given, method defaults to 5 words. All words start with E or R.

0 commit comments

Comments
 (0)