Skip to content

Commit

Permalink
base foundation() created, flexRange() created, basic test written
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed May 31, 2019
1 parent fa9ad38 commit 19a3f1c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
language: node_js
node_js:
- "stable"
branches:
only:
- master
11 changes: 3 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
'use strict';

const Purify = require('purify-int')
const Crypto = require('crypto')
const dep = require('./deprecated/v1') // floor(), ceil()
const libs = require('./libs') // range()

const { floor, ceil } = require('./deprecated/v1')

module.exports = {
floor,
ceil
}
module.exports = Object.assign({}, dep, libs)
21 changes: 21 additions & 0 deletions libs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const Purify = require('purify-int')
const Crypto = require('crypto')

// INTERNAL
function foundation(base) {
const pureBase = Math.abs(base)
const byteCount = Math.ceil(pureBase / 256)
const hexString = Crypto.randomBytes(byteCount).toString('hex')
return parseInt(hexString, 16) % pureBase
}

// EXTERNAL
function flexRange(v) {
const pureV = Purify.asInt(v)
const result = foundation(pureV)
return (pureV < 0) ? result * -1 : result
}

module.exports = { flexRange }
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Make Random

<!-- [![Build Status](https://travis-ci.org/johnfoderaro/make-random.svg?branch=master)](https://travis-ci.org/johnfoderaro/make-random) -->
[![Build Status](https://travis-ci.com/karnthis/make-random.svg?branch=master)](https://travis-ci.com/karnthis/make-random)
[![forthebadge](https://forthebadge.com/images/badges/gluten-free.svg)](https://forthebadge.com)

[![License: MIT](https://badgen.net/github/license/karnthis/make-random)](https://opensource.org/licenses/MIT)
Expand Down
46 changes: 46 additions & 0 deletions test/deprecated/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const chai = require('chai');
const expect = chai.expect;
const { ceil, floor } = require('../../deprecated/v1');

describe('makeRandom ceil()', () => {
it(`should have the method 'ceil'`, ()=> {
expect(ceil).to.be.a('function');
});
it(`should return a number`, () => {
expect(ceil(10)).to.be.a('number');
expect(ceil('10')).to.be.an('number');
});
it(`should not return a negative zero`, () => {
expect(ceil(-0)).to.equal(0);
});

it(`should return 0 if a number or number string is not passed to ceil()`, () => {
expect(ceil('test')).to.equal(0);
expect(ceil(true)).to.equal(0);
expect(ceil({})).to.equal(0);
expect(ceil([])).to.equal(0);
expect(ceil(() => {})).to.equal(0);
});
});

describe('makeRandom floor()', () => {
it(`should have the method 'floor'`, ()=> {
expect(floor).to.be.a('function');
});
it(`should return a number`, () => {
expect(floor(10)).to.be.a('number');
expect(floor('10')).to.be.an('number');
});
it(`should not return a negative zero`, () => {
expect(floor(-0)).to.equal(0);
});
it(`should return 0 if a number or number string is not passed to ceil()`, () => {
expect(floor('test')).to.equal(0);
expect(floor(true)).to.equal(0);
expect(floor({})).to.equal(0);
expect(floor([])).to.equal(0);
expect(floor(() => {})).to.equal(0);
});
});
61 changes: 20 additions & 41 deletions test/main/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
'use strict';

const chai = require('chai');
const expect = chai.expect;
const { ceil, floor } = require('../../index');
const expect = require('chai').expect;
const { fixedRange, flexRange } = require('../../libs');

describe('makeRandom ceil()', () => {
it(`should have the method 'ceil'`, ()=> {
expect(ceil).to.be.a('function');
});
it(`should return a number`, () => {
expect(ceil(10)).to.be.a('number');
expect(ceil('10')).to.be.an('number');
});
it(`should not return a negative zero`, () => {
expect(ceil(-0)).to.equal(0);
});

it(`should return 0 if a number or number string is not passed to ceil()`, () => {
expect(ceil('test')).to.equal(0);
expect(ceil(true)).to.equal(0);
expect(ceil({})).to.equal(0);
expect(ceil([])).to.equal(0);
expect(ceil(() => {})).to.equal(0);
});
});

describe('makeRandom floor()', () => {
it(`should have the method 'floor'`, ()=> {
expect(floor).to.be.a('function');
});
it(`should return a number`, () => {
expect(floor(10)).to.be.a('number');
expect(floor('10')).to.be.an('number');
});
it(`should not return a negative zero`, () => {
expect(floor(-0)).to.equal(0);
});
it(`should return 0 if a number or number string is not passed to ceil()`, () => {
expect(floor('test')).to.equal(0);
expect(floor(true)).to.equal(0);
expect(floor({})).to.equal(0);
expect(floor([])).to.equal(0);
expect(floor(() => {})).to.equal(0);
describe('makeRandom flexRange()', () => {
it(`should have the method 'flexRange'`, ()=> {
expect(flexRange).to.be.a('function');
});
it(`should return a positive random number`, () => {
expect(flexRange(10)).to.be.a('number').and.be.greaterThan(-1);
expect(flexRange('10')).to.be.an('number').and.be.greaterThan(-1);
});
it(`should return a negative random number`, () => {
expect(flexRange(-10)).to.be.a('number').and.be.lessThan(0);
expect(flexRange('-10')).to.be.an('number').and.be.lessThan(0);
});
// it(`should return 0 if a number or number string is not passed to range()`, () => {
// expect(flexRange('test')).to.equal(0);
// expect(flexRange(true)).to.equal(0);
// expect(flexRange({})).to.equal(0);
// expect(flexRange([])).to.equal(0);
// expect(flexRange(() => {})).to.equal(0);
// });
});

0 comments on commit 19a3f1c

Please sign in to comment.