Skip to content

Commit 2e1e344

Browse files
committed
Move default charSets into CharSet
1 parent 4ef7250 commit 2e1e344

File tree

8 files changed

+58
-60
lines changed

8 files changed

+58
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Example usage:
4545
48-bit string using base32 characters:
4646

4747
```js
48-
import {Random, Entropy, CharSet} from './entropy-string'
48+
import {Random} from './entropy-string'
4949

5050
let random = new Random()
5151
let string = random.string(48)

dist/lib/charSet.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.charSet2 = exports.charSet4 = exports.charSet8 = exports.charSet16 = exports.charSet32 = exports.charSet64 = undefined;
67

78
var _log = require('babel-runtime/core-js/math/log2');
89

@@ -29,9 +30,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
2930
var propMap = new _weakMap2.default();
3031
var bitsPerByte = 8;
3132

32-
var _class = function () {
33-
function _class(chars) {
34-
(0, _classCallCheck3.default)(this, _class);
33+
var CharSet = function () {
34+
function CharSet(chars) {
35+
(0, _classCallCheck3.default)(this, CharSet);
3536

3637
if (!(typeof chars === 'string' || chars instanceof String)) {
3738
throw new Error('Invalid chars: Must be string');
@@ -60,7 +61,7 @@ var _class = function () {
6061
propMap.set(this, privProps);
6162
}
6263

63-
(0, _createClass3.default)(_class, [{
64+
(0, _createClass3.default)(CharSet, [{
6465
key: 'getChars',
6566
value: function getChars() {
6667
return propMap.get(this).chars;
@@ -104,10 +105,10 @@ var _class = function () {
104105
return this.getNdxFn();
105106
}
106107
}]);
107-
return _class;
108+
return CharSet;
108109
}();
109110

110-
exports.default = _class;
111+
exports.default = CharSet;
111112

112113

113114
var _ndxFn = function _ndxFn(bitsPerChar) {
@@ -141,4 +142,11 @@ var _ndxFn = function _ndxFn(bitsPerChar) {
141142
return ndx;
142143
};
143144
}
144-
};
145+
};
146+
147+
var charSet64 = exports.charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
148+
var charSet32 = exports.charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT');
149+
var charSet16 = exports.charSet16 = new CharSet('0123456789abcdef');
150+
var charSet8 = exports.charSet8 = new CharSet('01234567');
151+
var charSet4 = exports.charSet4 = new CharSet('ATCG');
152+
var charSet2 = exports.charSet2 = new CharSet('01');

dist/lib/random.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.charSet2 = exports.charSet4 = exports.charSet8 = exports.charSet16 = exports.charSet32 = exports.charSet64 = undefined;
76

87
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
98

@@ -27,13 +26,6 @@ var _weakMap2 = _interopRequireDefault(_weakMap);
2726

2827
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2928

30-
var charSet64 = exports.charSet64 = new _charSet2.default('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
31-
var charSet32 = exports.charSet32 = new _charSet2.default('2346789bdfghjmnpqrtBDFGHJLMNPQRT');
32-
var charSet16 = exports.charSet16 = new _charSet2.default('0123456789abcdef');
33-
var charSet8 = exports.charSet8 = new _charSet2.default('01234567');
34-
var charSet4 = exports.charSet4 = new _charSet2.default('ATCG');
35-
var charSet2 = exports.charSet2 = new _charSet2.default('01');
36-
3729
var propMap = new _weakMap2.default();
3830

3931
var _class = function () {
@@ -42,7 +34,7 @@ var _class = function () {
4234

4335
var charSet = void 0;
4436
if (arg === undefined) {
45-
charSet = charSet32;
37+
charSet = _charSet.charSet32;
4638
} else if (arg instanceof _charSet2.default) {
4739
charSet = arg;
4840
} else if (typeof arg === 'string' || arg instanceof String) {

entropy-string.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
const Random = require('./lib/random').default
2-
const Entropy = require('./lib/entropy').default
3-
const CharSet = require('./lib/charSet').default
1+
const Random = require('./dist/lib/random').default
2+
const Entropy = require('./dist/lib/entropy').default
3+
const CharSet = require('./dist/lib/charSet').default
4+
const {charSet2, charSet4, charSet8, charSet16, charSet32, charSet64} = require('./dist/lib/random')
45

56
module.exports = {
67
Random,
78
Entropy,
8-
CharSet
9+
CharSet,
10+
charSet2,
11+
charSet4,
12+
charSet8,
13+
charSet16,
14+
charSet32,
15+
charSet64
916
}

lib/charset.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import WeakMap from 'weak-map'
44
const propMap = new WeakMap()
55
const bitsPerByte = 8
66

7-
export default class {
7+
export default class CharSet {
88
constructor(chars) {
99
if (!(typeof chars === 'string' || chars instanceof String)) {
1010
throw new Error('Invalid chars: Must be string')
@@ -94,3 +94,10 @@ const _ndxFn = (bitsPerChar) => {
9494
}
9595
}
9696
}
97+
98+
export let charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
99+
export let charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT')
100+
export let charSet16 = new CharSet('0123456789abcdef')
101+
export let charSet8 = new CharSet('01234567')
102+
export let charSet4 = new CharSet('ATCG')
103+
export let charSet2 = new CharSet('01')

lib/random.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import CharSet from './charSet'
1+
import CharSet, {charSet32} from './charSet'
22
import Entropy from './entropy'
33

44
import WeakMap from 'weak-map'
55

6-
export let charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
7-
export let charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT')
8-
export let charSet16 = new CharSet('0123456789abcdef')
9-
export let charSet8 = new CharSet('01234567')
10-
export let charSet4 = new CharSet('ATCG')
11-
export let charSet2 = new CharSet('01')
12-
136
const propMap = new WeakMap()
147

158
export default class {

test/entropy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Entropy from '../lib/entropy'
2-
import Random, {charSet64, charSet32, charSet16, charSet8, charSet4, charSet2} from '../lib/random'
2+
import Random from '../lib/random'
3+
import {charSet64, charSet32, charSet16, charSet8, charSet4, charSet2} from '../lib/charSet'
34

45
import test from 'ava'
56

test/random.js

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import Random from '../lib/random'
2-
import CharSet from '../lib/charSet'
2+
import CharSet, {charSet64, charSet32, charSet16, charSet8, charSet4, charSet2} from '../lib/charSet'
33

44
import test from 'ava'
55

6-
test.beforeEach('Create CharSets', t => {
7-
t.context.charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
8-
t.context.charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT')
9-
t.context.charSet16 = new CharSet('0123456789abcdef')
10-
t.context.charSet8 = new CharSet('01234567')
11-
t.context.charSet4 = new CharSet('ATCG')
12-
t.context.charSet2 = new CharSet('01')
13-
})
14-
15-
166
test('Char Set Base 64 Strings', t => {
17-
let random = new Random(t.context.charSet64)
7+
let random = new Random(charSet64)
188
t.is(random.stringWithBytes( 6, Buffer.from([0xdd])), '3')
199
t.is(random.stringWithBytes(12, Buffer.from([0x78, 0xfc])), 'eP')
2010
t.is(random.stringWithBytes(18, Buffer.from([0xc5, 0x6f, 0x21])), 'xW8')
@@ -30,7 +20,7 @@ test('Char Set Base 64 Strings', t => {
3020
})
3121

3222
test('Char Set Base 32 Strings', t => {
33-
let random = new Random(t.context.charSet32)
23+
let random = new Random(charSet32)
3424
t.is(random.stringWithBytes( 5, Buffer.from([0xdd])), 'N')
3525
t.is(random.stringWithBytes(10, Buffer.from([0x78, 0xfc])), 'p6')
3626
t.is(random.stringWithBytes(15, Buffer.from([0x78, 0xfc])), 'p6R')
@@ -45,7 +35,7 @@ test('Char Set Base 32 Strings', t => {
4535
})
4636

4737
test('Char Set Base 16 Strings', t => {
48-
let random = new Random(t.context.charSet16)
38+
let random = new Random(charSet16)
4939
t.is(random.stringWithBytes( 4, Buffer.from([0x9d])), '9')
5040
t.is(random.stringWithBytes( 8, Buffer.from([0xae])), 'ae')
5141
t.is(random.stringWithBytes(12, Buffer.from([0x01, 0xf2])), '01f')
@@ -54,7 +44,7 @@ test('Char Set Base 16 Strings', t => {
5444
})
5545

5646
test('Char Set Base 8 Strings', t => {
57-
let random = new Random(t.context.charSet8)
47+
let random = new Random(charSet8)
5848
t.is(random.stringWithBytes( 3, Buffer.from([0x5a])), '2')
5949
t.is(random.stringWithBytes( 6, Buffer.from([0x5a])), '26')
6050
t.is(random.stringWithBytes( 9, Buffer.from([0x21, 0xa4])), '103')
@@ -68,7 +58,7 @@ test('Char Set Base 8 Strings', t => {
6858
})
6959

7060
test('Char Set Base 4 Strings', t => {
71-
let random = new Random(t.context.charSet4)
61+
let random = new Random(charSet4)
7262
t.is(random.stringWithBytes( 2, Buffer.from([0x5a])), 'T')
7363
t.is(random.stringWithBytes( 4, Buffer.from([0x5a])), 'TT')
7464
t.is(random.stringWithBytes( 6, Buffer.from([0x93])), 'CTA')
@@ -80,7 +70,7 @@ test('Char Set Base 4 Strings', t => {
8070
})
8171

8272
test('Char Set Base 2 Strings', t => {
83-
let random = new Random(t.context.charSet2)
73+
let random = new Random(charSet2)
8474
t.is(random.stringWithBytes( 1, Buffer.from([0x27])), '0')
8575
t.is(random.stringWithBytes( 2, Buffer.from([0x27])), '00')
8676
t.is(random.stringWithBytes( 3, Buffer.from([0x27])), '001')
@@ -95,25 +85,25 @@ test('Char Set Base 2 Strings', t => {
9585

9686
test('Char Set Strings', t => {
9787
let random = new Random()
98-
t.is(random.stringWithBytes(30, [0xa5, 0x62, 0x20, 0x87], t.context.charSet64), 'pWIgh')
99-
t.is(random.stringWithBytes(25, [0xa5, 0x62, 0x20, 0x87], t.context.charSet32), 'DFr43')
100-
t.is(random.stringWithBytes(16, [0xc7, 0xc9], t.context.charSet16), 'c7c9')
101-
t.is(random.stringWithBytes(24, [0xfd, 0x93, 0xd1], t.context.charSet8), '77311721')
102-
t.is(random.stringWithBytes(12, [0x20, 0xf1], t.context.charSet4), 'ACAAGG')
103-
t.is(random.stringWithBytes( 6, [0x27], t.context.charSet2), '001001')
88+
t.is(random.stringWithBytes(30, [0xa5, 0x62, 0x20, 0x87], charSet64), 'pWIgh')
89+
t.is(random.stringWithBytes(25, [0xa5, 0x62, 0x20, 0x87], charSet32), 'DFr43')
90+
t.is(random.stringWithBytes(16, [0xc7, 0xc9], charSet16), 'c7c9')
91+
t.is(random.stringWithBytes(24, [0xfd, 0x93, 0xd1], charSet8), '77311721')
92+
t.is(random.stringWithBytes(12, [0x20, 0xf1], charSet4), 'ACAAGG')
93+
t.is(random.stringWithBytes( 6, [0x27], charSet2), '001001')
10494
})
10595

10696
test('Invalid bytes', t => {
10797
let random
10898
let regex = /Insufficient/
10999

110-
random = new Random(t.context.charSet64)
100+
random = new Random(charSet64)
111101
t.regex(invalidBytes(random, 7, [1]), regex)
112102
t.regex(invalidBytes(random, 13, [1,2]), regex)
113103
t.regex(invalidBytes(random, 25, [1,2,3]), regex)
114104
t.regex(invalidBytes(random, 31, [1,2,3,4]), regex)
115105

116-
random = new Random(t.context.charSet32)
106+
random = new Random(charSet32)
117107
t.regex(invalidBytes(random, 6, [1]), regex)
118108
t.regex(invalidBytes(random, 16, [1,2]), regex)
119109
t.regex(invalidBytes(random, 21, [1,2,3]), regex)
@@ -122,21 +112,21 @@ test('Invalid bytes', t => {
122112
t.regex(invalidBytes(random, 46, [1,2,3,4,5,6]), regex)
123113
t.regex(invalidBytes(random, 32, [250, 200, 150, 100]), regex)
124114

125-
random = new Random(t.context.charSet16)
115+
random = new Random(charSet16)
126116
t.regex(invalidBytes(random, 9, [1]), regex)
127117
t.regex(invalidBytes(random, 17, [1,2]), regex)
128118

129-
random = new Random(t.context.charSet8)
119+
random = new Random(charSet8)
130120
t.regex(invalidBytes(random, 7, [1]), regex)
131121
t.regex(invalidBytes(random, 16, [1,2]), regex)
132122
t.regex(invalidBytes(random, 25, [1,2,3]), regex)
133123
t.regex(invalidBytes(random, 31, [1,2,3,4]), regex)
134124

135-
random = new Random(t.context.charSet4)
125+
random = new Random(charSet4)
136126
t.regex(invalidBytes(random, 9, [1]), regex)
137127
t.regex(invalidBytes(random, 17, [1,2]), regex)
138128

139-
random = new Random(t.context.charSet2)
129+
random = new Random(charSet2)
140130
t.regex(invalidBytes(random, 9, [1]), regex)
141131
t.regex(invalidBytes(random, 17, [1,2]), regex)
142132
})

0 commit comments

Comments
 (0)