Skip to content

Commit 0aa2ad1

Browse files
committed
Refactor
Move ndxFn into CharSet
1 parent c2bbf9a commit 0aa2ad1

File tree

5 files changed

+115
-163
lines changed

5 files changed

+115
-163
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ Note how the number of bytes needed is dependent on the number of characters in
425425

426426
- You don't need random strings of length L.
427427
- String length is a by-product, not a goal.
428-
- You need unique strings.
428+
- You don't need truly unique strings.
429429
- Uniqueness is too onerous. You'll do fine with probabilistically unique strings.
430430
- Probabilistic uniqueness involves measured risk.
431431
- Risk is measured as *"1 in __n__ chance of generating a repeat"*

dist/lib/charSet.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ var _lcm2 = _interopRequireDefault(_lcm);
2323
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2424

2525
var CharSet = function () {
26-
function CharSet(chars) {
26+
function CharSet(chars, ndxFn) {
2727
(0, _classCallCheck3.default)(this, CharSet);
2828

2929
this.chars = chars;
30+
this.ndxFn = ndxFn;
3031
this.len = chars.length;
3132
this.entropyPerChar = Math.floor((0, _log2.default)(this.len));
3233

@@ -62,17 +63,62 @@ var CharSet = function () {
6263
return CharSet;
6364
}();
6465

65-
var charSet64 = new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
66-
var charSet32 = new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT");
67-
var charSet16 = new CharSet("0123456789abcdef");
68-
var charSet8 = new CharSet("01234567");
69-
var charSet4 = new CharSet("ATCG");
70-
var charSet2 = new CharSet("01");
66+
var charSet64 = new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", _ndx64);
67+
var charSet32 = new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT", _ndx32);
68+
var charSet16 = new CharSet("0123456789abcdef", _ndx16);
69+
var charSet8 = new CharSet("01234567", _ndx8);
70+
var charSet4 = new CharSet("ATCG", _ndx4);
71+
var charSet2 = new CharSet("01", _ndx2);
7172

7273
var isValid = function isValid(charSet) {
7374
return charSet instanceof CharSet;
7475
};
7576

77+
var _ndx64 = function _ndx64(chunk, slice, bytes) {
78+
return _ndxGen(chunk, slice, bytes, 6);
79+
};
80+
81+
var _ndx32 = function _ndx32(chunk, slice, bytes) {
82+
return _ndxGen(chunk, slice, bytes, 5);
83+
};
84+
85+
var _ndx16 = function _ndx16(chunk, slice, bytes) {
86+
return (bytes[chunk] << 4 * slice & 0xff) >> 4;
87+
};
88+
89+
var _ndx8 = function _ndx8(chunk, slice, bytes) {
90+
return _ndxGen(chunk, slice, bytes, 3);
91+
};
92+
93+
var _ndx4 = function _ndx4(chunk, slice, bytes) {
94+
return (bytes[chunk] << 2 * slice & 0xff) >> 6;
95+
};
96+
97+
var _ndx2 = function _ndx2(chunk, slice, bytes) {
98+
return (bytes[chunk] << slice & 0xff) >> 7;
99+
};
100+
101+
var _ndxGen = function _ndxGen(chunk, slice, bytes, bitsPerSlice) {
102+
var bitsPerByte = 8;
103+
var slicesPerChunk = (0, _lcm2.default)(bitsPerSlice, bitsPerByte) / bitsPerByte;
104+
105+
var bNum = chunk * slicesPerChunk;
106+
107+
var rShift = bitsPerByte - bitsPerSlice;
108+
109+
var lOffset = Math.floor(slice * bitsPerSlice / bitsPerByte);
110+
var lShift = slice * bitsPerSlice % bitsPerByte;
111+
112+
var ndx = (bytes[bNum + lOffset] << lShift & 0xff) >> rShift;
113+
114+
var rOffset = Math.ceil(slice * bitsPerSlice / bitsPerByte);
115+
var rShiftIt = ((rOffset + 1) * bitsPerByte - (slice + 1) * bitsPerSlice) % bitsPerByte;
116+
if (rShift < rShiftIt) {
117+
ndx += bytes[bNum + rOffset] >> rShiftIt;
118+
}
119+
return ndx;
120+
};
121+
76122
exports.default = {
77123
charSet64: charSet64,
78124
charSet32: charSet32,

dist/lib/entropy.js

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -106,41 +106,16 @@ var stringWithBytes = function stringWithBytes(entropy, charSet, bytes) {
106106
var chunks = Math.floor(count / charSet.charsPerChunk);
107107
var partials = count % charSet.charsPerChunk;
108108

109-
var ndxFn = void 0;
110-
switch (charSet) {
111-
case _charSet2.default.charSet64:
112-
ndxFn = _ndx64;
113-
break;
114-
case _charSet2.default.charSet32:
115-
ndxFn = _ndx32;
116-
break;
117-
case _charSet2.default.charSet16:
118-
ndxFn = _ndx16;
119-
break;
120-
case _charSet2.default.charSet8:
121-
ndxFn = _ndx8;
122-
break;
123-
case _charSet2.default.charSet4:
124-
ndxFn = _ndx4;
125-
break;
126-
case _charSet2.default.charSet2:
127-
ndxFn = _ndx2;
128-
break;
129-
default:
130-
break;
131-
}
132-
133-
var chars = charSet.chars;
134109
var string = '';
135110
for (var chunk = 0; chunk < chunks; chunk++) {
136111
for (var slice = 0; slice < charSet.charsPerChunk; slice++) {
137-
var ndx = ndxFn(chunk, slice, bytes);
138-
string += chars[ndx];
112+
var ndx = charSet.ndxFn(chunk, slice, bytes);
113+
string += charSet.chars[ndx];
139114
}
140115
}
141116
for (var _slice = 0; _slice < partials; _slice++) {
142-
var _ndx = ndxFn(chunks, _slice, bytes);
143-
string += chars[_ndx];
117+
var _ndx = charSet.ndxFn(chunks, _slice, bytes);
118+
string += charSet.chars[_ndx];
144119
}
145120
return string;
146121
};
@@ -186,51 +161,6 @@ var _bufferByte = function _bufferByte(buffer, bByte, nByte, byteCount, dataView
186161
}
187162
};
188163

189-
var _ndx64 = function _ndx64(chunk, slice, bytes) {
190-
return _ndxGen(chunk, slice, bytes, 6);
191-
};
192-
193-
var _ndx32 = function _ndx32(chunk, slice, bytes) {
194-
return _ndxGen(chunk, slice, bytes, 5);
195-
};
196-
197-
var _ndx16 = function _ndx16(chunk, slice, bytes) {
198-
return (bytes[chunk] << 4 * slice & 0xff) >> 4;
199-
};
200-
201-
var _ndx8 = function _ndx8(chunk, slice, bytes) {
202-
return _ndxGen(chunk, slice, bytes, 3);
203-
};
204-
205-
var _ndx4 = function _ndx4(chunk, slice, bytes) {
206-
return (bytes[chunk] << 2 * slice & 0xff) >> 6;
207-
};
208-
209-
var _ndx2 = function _ndx2(chunk, slice, bytes) {
210-
return (bytes[chunk] << slice & 0xff) >> 7;
211-
};
212-
213-
var _ndxGen = function _ndxGen(chunk, slice, bytes, bitsPerSlice) {
214-
var bitsPerByte = 8;
215-
var slicesPerChunk = (0, _lcm2.default)(bitsPerSlice, bitsPerByte) / bitsPerByte;
216-
217-
var bNum = chunk * slicesPerChunk;
218-
219-
var rShift = bitsPerByte - bitsPerSlice;
220-
221-
var lOffset = Math.floor(slice * bitsPerSlice / bitsPerByte);
222-
var lShift = slice * bitsPerSlice % bitsPerByte;
223-
224-
var ndx = (bytes[bNum + lOffset] << lShift & 0xff) >> rShift;
225-
226-
var rOffset = Math.ceil(slice * bitsPerSlice / bitsPerByte);
227-
var rShiftIt = ((rOffset + 1) * bitsPerByte - (slice + 1) * bitsPerSlice) % bitsPerByte;
228-
if (rShift < rShiftIt) {
229-
ndx += bytes[bNum + rOffset] >> rShiftIt;
230-
}
231-
return ndx;
232-
};
233-
234164
exports.default = {
235165
bits: bits,
236166
bitsWithRiskPower: bitsWithRiskPower,

lib/charSet.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import lcm from './lcm'
22

33
class CharSet {
4-
constructor(chars) {
4+
constructor(chars, ndxFn) {
55
this.chars = chars
6+
this.ndxFn = ndxFn
67
this.len = chars.length
78
this.entropyPerChar = Math.floor(Math.log2(this.len))
89

@@ -34,18 +35,63 @@ class CharSet {
3435
}
3536
}
3637

38+
const _ndx64 = (chunk, slice, bytes) => {
39+
return _ndxGen(chunk, slice, bytes, 6)
40+
}
41+
42+
const _ndx32 = (chunk, slice, bytes) => {
43+
return _ndxGen(chunk, slice, bytes, 5)
44+
}
45+
46+
const _ndx16 = (chunk, slice, bytes) => {
47+
return ((bytes[chunk]<<(4*slice))&0xff)>>4
48+
}
49+
50+
const _ndx8 = (chunk, slice, bytes) => {
51+
return _ndxGen(chunk, slice, bytes, 3)
52+
}
53+
54+
const _ndx4 = (chunk, slice, bytes) => {
55+
return ((bytes[chunk]<<(2*slice))&0xff)>>6
56+
}
57+
58+
const _ndx2 = (chunk, slice, bytes) => {
59+
return ((bytes[chunk]<<slice)&0xff)>>7
60+
}
61+
62+
const _ndxGen = (chunk, slice, bytes, bitsPerSlice) => {
63+
let bitsPerByte = 8
64+
let slicesPerChunk = lcm(bitsPerSlice, bitsPerByte) / bitsPerByte
65+
66+
let bNum = chunk * slicesPerChunk
67+
68+
let rShift = bitsPerByte - bitsPerSlice
69+
70+
let lOffset = Math.floor((slice*bitsPerSlice)/bitsPerByte)
71+
let lShift = (slice*bitsPerSlice) % bitsPerByte
72+
73+
let ndx = ((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
74+
75+
let rOffset = Math.ceil((slice*bitsPerSlice)/bitsPerByte)
76+
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*bitsPerSlice) % bitsPerByte
77+
if (rShift < rShiftIt) {
78+
ndx += bytes[bNum+rOffset]>>rShiftIt
79+
}
80+
return ndx
81+
}
82+
3783
const charSet64 =
38-
new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
84+
new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", _ndx64)
3985
const charSet32 =
40-
new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT")
86+
new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT", _ndx32)
4187
const charSet16 =
42-
new CharSet("0123456789abcdef")
88+
new CharSet("0123456789abcdef", _ndx16)
4389
const charSet8 =
44-
new CharSet("01234567")
90+
new CharSet("01234567", _ndx8)
4591
const charSet4 =
46-
new CharSet("ATCG")
92+
new CharSet("ATCG", _ndx4)
4793
const charSet2 =
48-
new CharSet("01")
94+
new CharSet("01", _ndx2)
4995

5096
const isValid = (charSet) => {
5197
return charSet instanceof CharSet

lib/entropy.js

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -79,41 +79,16 @@ const stringWithBytes = (entropy, charSet, bytes) => {
7979
const chunks = Math.floor(count / charSet.charsPerChunk)
8080
const partials = count % charSet.charsPerChunk
8181

82-
let ndxFn
83-
switch (charSet) {
84-
case CharSet.charSet64:
85-
ndxFn = _ndx64
86-
break
87-
case CharSet.charSet32:
88-
ndxFn = _ndx32
89-
break
90-
case CharSet.charSet16:
91-
ndxFn = _ndx16
92-
break
93-
case CharSet.charSet8:
94-
ndxFn = _ndx8
95-
break
96-
case CharSet.charSet4:
97-
ndxFn = _ndx4
98-
break
99-
case CharSet.charSet2:
100-
ndxFn = _ndx2
101-
break
102-
default:
103-
break
104-
}
105-
106-
let chars = charSet.chars
10782
let string = ''
10883
for (let chunk = 0; chunk < chunks; chunk++) {
10984
for (let slice = 0; slice < charSet.charsPerChunk; slice++) {
110-
let ndx = ndxFn(chunk, slice, bytes)
111-
string += chars[ndx]
85+
let ndx = charSet.ndxFn(chunk, slice, bytes)
86+
string += charSet.chars[ndx]
11287
}
11388
}
11489
for (let slice = 0; slice < partials; slice++) {
115-
let ndx = ndxFn(chunks, slice, bytes)
116-
string += chars[ndx]
90+
let ndx = charSet.ndxFn(chunks, slice, bytes)
91+
string += charSet.chars[ndx]
11792
}
11893
return string
11994
}
@@ -155,51 +130,6 @@ const _bufferByte = (buffer, bByte, nByte, byteCount, dataView) => {
155130
}
156131
}
157132

158-
const _ndx64 = (chunk, slice, bytes) => {
159-
return _ndxGen(chunk, slice, bytes, 6)
160-
}
161-
162-
const _ndx32 = (chunk, slice, bytes) => {
163-
return _ndxGen(chunk, slice, bytes, 5)
164-
}
165-
166-
const _ndx16 = (chunk, slice, bytes) => {
167-
return ((bytes[chunk]<<(4*slice))&0xff)>>4
168-
}
169-
170-
const _ndx8 = (chunk, slice, bytes) => {
171-
return _ndxGen(chunk, slice, bytes, 3)
172-
}
173-
174-
const _ndx4 = (chunk, slice, bytes) => {
175-
return ((bytes[chunk]<<(2*slice))&0xff)>>6
176-
}
177-
178-
const _ndx2 = (chunk, slice, bytes) => {
179-
return ((bytes[chunk]<<slice)&0xff)>>7
180-
}
181-
182-
const _ndxGen = (chunk, slice, bytes, bitsPerSlice) => {
183-
let bitsPerByte = 8
184-
let slicesPerChunk = lcm(bitsPerSlice, bitsPerByte) / bitsPerByte
185-
186-
let bNum = chunk * slicesPerChunk
187-
188-
let rShift = bitsPerByte - bitsPerSlice
189-
190-
let lOffset = Math.floor((slice*bitsPerSlice)/bitsPerByte)
191-
let lShift = (slice*bitsPerSlice) % bitsPerByte
192-
193-
let ndx = ((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
194-
195-
let rOffset = Math.ceil((slice*bitsPerSlice)/bitsPerByte)
196-
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*bitsPerSlice) % bitsPerByte
197-
if (rShift < rShiftIt) {
198-
ndx += bytes[bNum+rOffset]>>rShiftIt
199-
}
200-
return ndx
201-
}
202-
203133
export default {
204134
bits : bits,
205135
bitsWithRiskPower : bitsWithRiskPower,

0 commit comments

Comments
 (0)