Skip to content

Commit 810188d

Browse files
committed
Merge branch 'dev'
2 parents 89cd917 + d12628f commit 810188d

File tree

5 files changed

+76
-42
lines changed

5 files changed

+76
-42
lines changed

changelog.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
<!-- #### Removed -->
1111

1212
## Major Releases:
13-
### v1.0.0 - 2019 03 09
13+
### v1.0.0 - 2016 09 23
1414
- Initial Release
1515

1616
## Minor Releases:
17-
### [v0.1.5] - 2019 03 11
17+
### [v0.1.7] - 2019 06 08
18+
#### Changed
19+
- Updated existing supported methods to promises
20+
21+
### [v0.1.6] - 2019 06 08
22+
#### Added
23+
- `setRange()`
24+
- `flexRange()`
25+
26+
#### Changed
27+
- Deprecated:
28+
- `makeRandom.ceil()`
29+
- `makeRandom.floor()`
30+
31+
### [v0.1.5] - 2019 05 29
1832
#### Added
1933
- changelog.md
2034
#### Changed
@@ -23,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2337

2438
<!-- LINKS -->
2539
<!-- RELEASES -->
26-
[Unreleased]: https://github.com/karnthis/make-random/compare/v0.1.5...dev
40+
[Unreleased]: https://github.com/karnthis/make-random/compare/v0.1.7...dev
41+
[v0.1.6]: https://github.com/karnthis/make-random/compare/v0.1.6...v0.1.7
42+
[v0.1.6]: https://github.com/karnthis/make-random/compare/v0.1.5...v0.1.6
2743
[v0.1.5]: https://github.com/karnthis/make-random/compare/v0.1.4...v0.1.5
2844
<!-- ISSUES -->

libs/index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
const Purify = require('purify-int')
44
const Crypto = require('crypto')
55

6-
const debug = true
7-
86
// INTERNAL
97
function foundation(base) {
108
const trueBase = Math.abs(base)
119
const byteCount = makeByteCount(trueBase)
12-
// if (debug) console.log(byteCount)
13-
const hexString = Crypto.randomBytes(byteCount).toString('hex')
14-
// if (debug) console.log(parseInt(hexString, 16))
15-
return parseInt(hexString, 16) % trueBase
10+
return promiseCryptoRBytes(byteCount)
11+
.then(buf => buf.toString('hex'))
12+
.then(hex => parseInt(hex, 16) % trueBase)
13+
}
14+
15+
function promiseCryptoRBytes(byteCount = 1) {
16+
return new Promise((resolve, reject) => {
17+
Crypto.randomBytes(byteCount, (err, buf) => {
18+
if (err) throw err
19+
resolve(buf)
20+
})
21+
})
1622
}
1723

1824
function makeByteCount(max) {
@@ -32,16 +38,15 @@ function offsetNum(v, invert = false) {
3238
} else {
3339
return (v < 0) ? ++v : --v
3440
}
35-
41+
3642
}
3743

3844
// EXTERNAL
3945
function flexRange(v = 1) {
4046
v = (v > 0) ? ++v : --v
4147
const pureV = Purify.asInt(v, 1)
42-
const result = foundation(pureV)
43-
console.log(result)
44-
return (pureV < 0 && result !== 0 ) ? result * -1 : result
48+
return foundation(pureV)
49+
.then(res => (pureV < 0 && res !== 0) ? res * -1 : res)
4550
}
4651

4752
function setRange(v1 = -100, v2 = 100) {
@@ -55,11 +60,11 @@ function setRange(v1 = -100, v2 = 100) {
5560
high = v1
5661
low = v2
5762
} else {
58-
return flexRange(offsetNum(v2,true))
63+
return flexRange(offsetNum(v2, true))
5964
}
6065
const baseValue = high - low
61-
const result = foundation(baseValue)
62-
return (baseValue < 0) ? (result * -1) + low : result + low
66+
return foundation(baseValue)
67+
.then(res => (baseValue < 0) ? (res * -1) + low : res + low)
6368
}
6469

6570
module.exports = { flexRange, setRange }

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "make-random",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "Make a random number, powered by Crypto",
55
"main": "index.js",
66
"scripts": {

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![npm: version](https://badgen.net/npm/v/make-random)](https://www.npmjs.com/package/make-random)
1010
[![npm: downloads](https://badgen.net/npm/dt/make-random)](https://www.npmjs.com/package/make-random)
1111

12-
## For Make Random (Legacy) [Please Click Here](https://github.com/karnthis/make-random-legacy)
12+
## For Make Random (Legacy) [Please Click Here](https://www.npmjs.com/package/make-random-legacy)
1313

1414
## About Make Random
1515

test/main/index.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,58 @@ describe('flexRange()', () => {
77
it(`should be a function`, ()=> {
88
expect(flexRange).to.be.a('function');
99
});
10-
it(`should return a positive random number`, () => {
11-
expect(flexRange(10)).to.be.a('number').and.be.at.least(0);
12-
expect(flexRange('10')).to.be.an('number').and.be.at.least(0);
10+
it(`should return a positive random number`, async () => {
11+
const result1 = await flexRange(10)
12+
expect(result1).to.be.a('number').and.be.at.least(0);
13+
const result2 = await flexRange('10')
14+
expect(result2).to.be.an('number').and.be.at.least(0);
1315
});
14-
it(`should return a negative random number`, () => {
15-
expect(flexRange(-10)).to.be.a('number').and.to.be.at.most(0);
16-
expect(flexRange('-10')).to.be.an('number').and.to.be.at.most(0);
16+
it(`should return a negative random number`, async () => {
17+
const result1 = await flexRange(-10)
18+
expect(result1).to.be.a('number').and.to.be.at.most(0);
19+
const result2 = await flexRange('-10')
20+
expect(result2).to.be.an('number').and.to.be.at.most(0);
1721
});
18-
it(`should return randomly 0 or 1 if passed no arguments`, () => {
19-
expect(flexRange()).to.be.within(0,1);
22+
it(`should return randomly 0 or 1 if passed no arguments`, async () => {
23+
const result = await flexRange()
24+
expect(result).to.be.within(0,1);
2025
});
2126
});
2227

2328
describe('setRange()', () => {
2429
it(`should be a function`, ()=> {
2530
expect(setRange).to.be.a('function');
2631
});
27-
it(`should return randomly -100 to 100 if passed no arguments`, () => {
28-
expect(setRange()).to.be.an('number').to.be.within(-100,100);
32+
it(`should return randomly -100 to 100 if passed no arguments`, async () => {
33+
const result = await setRange()
34+
expect(result).to.be.an('number').to.be.within(-100,100);
2935
});
30-
it(`should return randomly 10 to 50 if passed those values`, () => {
31-
expect(setRange(10,50)).to.be.an('number').to.be.within(10,50);
36+
it(`should return randomly 10 to 50 if passed those values`, async () => {
37+
const result = await setRange(10,50)
38+
expect(result).to.be.an('number').to.be.within(10,50);
3239
});
33-
it(`should handle if order is flipped`, () => {
34-
expect(setRange(50,10)).to.be.an('number').to.be.within(10,50);
40+
it(`should handle if order is flipped`, async () => {
41+
const result = await setRange(50,10)
42+
expect(result).to.be.an('number').to.be.within(10,50);
3543
});
36-
it(`should handle mixed positive/negative numbers (-10 to 10)`, () => {
37-
expect(setRange(-10,10)).to.be.an('number').to.be.within(-10,10);
44+
it(`should handle mixed positive/negative numbers (-10 to 10)`, async () => {
45+
const result = await setRange(-10,10)
46+
expect(result).to.be.an('number').to.be.within(-10,10);
3847
});
39-
it(`should handle negative numbers (-10 to -50)`, () => {
40-
expect(setRange(-10,-50)).to.be.an('number').to.be.within(-50,-10);
48+
it(`should handle negative numbers (-10 to -50)`, async () => {
49+
const result = await setRange(-10,-50)
50+
expect(result).to.be.an('number').to.be.within(-50,-10);
4151
});
42-
it(`should handle numbers as strings ('10' to '-50')`, () => {
43-
expect(setRange('10','-50')).to.be.an('number').to.be.within(-50,10);
52+
it(`should handle numbers as strings ('10' to '-50')`, async () => {
53+
const result = await setRange('10','-50')
54+
expect(result).to.be.an('number').to.be.within(-50,10);
4455
});
45-
it(`should handle mixed numbers as strings ('10' to -50)`, () => {
46-
expect(setRange('10',-50)).to.be.an('number').to.be.within(-50,10);
56+
it(`should handle mixed numbers as strings ('10' to -50)`, async () => {
57+
const result = await setRange('10',-50)
58+
expect(result).to.be.an('number').to.be.within(-50,10);
4759
});
48-
it(`should default to flexRange() behavior if input is the same`, () => {
49-
expect(setRange(10,10)).to.be.an('number').and.be.greaterThan(-1);
60+
it(`should default to flexRange() behavior if input is the same`, async () => {
61+
const result = await setRange(10,10)
62+
expect(result).to.be.an('number').and.be.greaterThan(-1);
5063
});
5164
});

0 commit comments

Comments
 (0)