11'use strict' ;
22
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
37var _log = require ( 'babel-runtime/core-js/math/log2' ) ;
48
59var _log2 = _interopRequireDefault ( _log ) ;
610
11+ var _classCallCheck2 = require ( 'babel-runtime/helpers/classCallCheck' ) ;
12+
13+ var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
14+
15+ var _createClass2 = require ( 'babel-runtime/helpers/createClass' ) ;
16+
17+ var _createClass3 = _interopRequireDefault ( _createClass2 ) ;
18+
719var _lcm = require ( './lcm' ) ;
820
921var _lcm2 = _interopRequireDefault ( _lcm ) ;
@@ -14,103 +26,119 @@ var _weakMap2 = _interopRequireDefault(_weakMap);
1426
1527function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
1628
17- var CharSet = function ( ) {
18- var propMap = new _weakMap2 . default ( ) ;
29+ var propMap = new _weakMap2 . default ( ) ;
30+ var bitsPerByte = 8 ;
1931
20- function CharSet ( chars ) {
21- var bitsPerChar = Math . floor ( ( 0 , _log2 . default ) ( chars . length ) ) ;
22- if ( bitsPerChar != ( 0 , _log2 . default ) ( chars . length ) ) {
23- throw new Error ( 'EntropyString only supports CharSets with a power of 2 characters' ) ;
24- }
25- var bitsPerByte = 8 ;
32+ var _class = function ( ) {
33+ function _class ( chars ) {
34+ ( 0 , _classCallCheck3 . default ) ( this , _class ) ;
2635
36+ if ( ! ( typeof chars === 'string' || chars instanceof String ) ) {
37+ throw new Error ( 'Invalid chars: Must be string' ) ;
38+ }
39+ var length = chars . length ;
40+ if ( ! [ 2 , 4 , 8 , 16 , 32 , 64 ] . includes ( length ) ) {
41+ throw new Error ( 'Invalid char count: must be one of 2,4,8,16,32,64' ) ;
42+ }
43+ var bitsPerChar = Math . floor ( ( 0 , _log2 . default ) ( length ) ) ;
44+ // Ensure no repeated characters
45+ for ( var i = 0 ; i < length ; i ++ ) {
46+ var c = chars . charAt ( i ) ;
47+ for ( var j = i + 1 ; j < length ; j ++ ) {
48+ if ( c === chars . charAt ( j ) ) {
49+ throw new Error ( 'Characters not unique' ) ;
50+ }
51+ }
52+ }
2753 var privProps = {
2854 chars : chars ,
2955 bitsPerChar : bitsPerChar ,
30- ndxFn : ndxFn ( bitsPerChar ) ,
56+ length : length ,
57+ ndxFn : _ndxFn ( bitsPerChar ) ,
3158 charsPerChunk : ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerChar
3259 } ;
3360 propMap . set ( this , privProps ) ;
3461 }
3562
36- CharSet . prototype . getChars = function ( ) {
37- return propMap . get ( this ) . chars ;
38- } ;
39- CharSet . prototype . getBitsPerChar = function ( ) {
40- return propMap . get ( this ) . bitsPerChar ;
41- } ;
42- CharSet . prototype . getNdxFn = function ( ) {
43- return propMap . get ( this ) . ndxFn ;
44- } ;
45- CharSet . prototype . getCharsPerChunk = function ( ) {
46- return propMap . get ( this ) . charsPerChunk ;
47- } ;
48-
49- CharSet . prototype . setChars = function ( chars ) {
50- var len = chars . length ;
51- // Ensure correct number of characters
52- if ( len != propMap . get ( this ) . chars . length ) {
53- throw new Error ( 'Invalid character count' ) ;
63+ ( 0 , _createClass3 . default ) ( _class , [ {
64+ key : 'getChars' ,
65+ value : function getChars ( ) {
66+ return propMap . get ( this ) . chars ;
5467 }
55- // Ensure no repeated characters
56- for ( var i = 0 ; i < len ; i ++ ) {
57- var c = chars . charAt ( i ) ;
58- for ( var j = i + 1 ; j < len ; j ++ ) {
59- if ( c === chars . charAt ( j ) ) {
60- throw new Error ( 'Characters not unique' ) ;
61- }
62- }
68+ } , {
69+ key : 'getBitsPerChar' ,
70+ value : function getBitsPerChar ( ) {
71+ return propMap . get ( this ) . bitsPerChar ;
72+ }
73+ } , {
74+ key : 'getNdxFn' ,
75+ value : function getNdxFn ( ) {
76+ return propMap . get ( this ) . ndxFn ;
77+ }
78+ } , {
79+ key : 'getCharsPerChunk' ,
80+ value : function getCharsPerChunk ( ) {
81+ return propMap . get ( this ) . charsPerChunk ;
82+ }
83+ } , {
84+ key : 'length' ,
85+ value : function length ( ) {
86+ return propMap . get ( this ) . length ;
87+ }
88+ } , {
89+ key : 'getNdxFn' ,
90+ value : function getNdxFn ( ) {
91+ return propMap . get ( this ) . ndxFn ;
6392 }
64- propMap . get ( this ) . chars = chars ;
65- } ;
66- // Alias
67- CharSet . prototype . use = CharSet . prototype . setChars ;
6893
69- var ndxFn = function ndxFn ( bitsPerChar ) {
70- var bitsPerByte = 8 ;
94+ // Aliases
7195
72- // If bitsPerBytes is a multiple of bitsPerChar, we can slice off an integer number
73- // of chars per byte.
74- if ( ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) === bitsPerByte ) {
75- return function ( chunk , slice , bytes ) {
76- var lShift = bitsPerChar ;
77- var rShift = bitsPerByte - bitsPerChar ;
78- return ( bytes [ chunk ] << lShift * slice & 0xff ) >> rShift ;
79- } ;
96+ } , {
97+ key : 'chars' ,
98+ value : function chars ( ) {
99+ return this . getChars ( ) ;
80100 }
81- // Otherwise, while slicing off bits per char, we will possibly straddle a couple
82- // of bytes, so a bit more work is involved
83- else {
84- return function ( chunk , slice , bytes ) {
85- var slicesPerChunk = ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerByte ;
86- var bNum = chunk * slicesPerChunk ;
87-
88- var rShift = bitsPerByte - bitsPerChar ;
89- var lOffset = Math . floor ( slice * bitsPerChar / bitsPerByte ) ;
90- var lShift = slice * bitsPerChar % bitsPerByte ;
91-
92- var ndx = ( bytes [ bNum + lOffset ] << lShift & 0xff ) >> rShift ;
93-
94- var rOffset = Math . ceil ( slice * bitsPerChar / bitsPerByte ) ;
95- var rShiftIt = ( ( rOffset + 1 ) * bitsPerByte - ( slice + 1 ) * bitsPerChar ) % bitsPerByte ;
96- if ( rShift < rShiftIt ) {
97- ndx += bytes [ bNum + rOffset ] >> rShiftIt ;
98- }
99- return ndx ;
100- } ;
101- }
102- } ;
103- return CharSet ;
101+ } , {
102+ key : 'ndxFn' ,
103+ value : function ndxFn ( ) {
104+ return this . getNdxFn ( ) ;
105+ }
106+ } ] ) ;
107+ return _class ;
104108} ( ) ;
105109
106- module . exports = {
107- base64 : new CharSet ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' ) ,
108- base32 : new CharSet ( '2346789bdfghjmnpqrtBDFGHJLMNPQRT' ) ,
109- base16 : new CharSet ( '0123456789abcdef' ) ,
110- base8 : new CharSet ( '01234567' ) ,
111- base4 : new CharSet ( 'ATCG' ) ,
112- base2 : new CharSet ( '01' ) ,
113- isValid : function isValid ( charSet ) {
114- return charSet instanceof CharSet ;
110+ exports . default = _class ;
111+
112+
113+ var _ndxFn = function _ndxFn ( bitsPerChar ) {
114+ // If bitsPerBytes is a multiple of bitsPerChar, we can slice off an integer number
115+ // of chars per byte.
116+ if ( ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) === bitsPerByte ) {
117+ return function ( chunk , slice , bytes ) {
118+ var lShift = bitsPerChar ;
119+ var rShift = bitsPerByte - bitsPerChar ;
120+ return ( bytes [ chunk ] << lShift * slice & 0xff ) >> rShift ;
121+ } ;
115122 }
123+ // Otherwise, while slicing off bits per char, we will possibly straddle a couple
124+ // of bytes, so a bit more work is involved
125+ else {
126+ return function ( chunk , slice , bytes ) {
127+ var slicesPerChunk = ( 0 , _lcm2 . default ) ( bitsPerChar , bitsPerByte ) / bitsPerByte ;
128+ var bNum = chunk * slicesPerChunk ;
129+
130+ var rShift = bitsPerByte - bitsPerChar ;
131+ var lOffset = Math . floor ( slice * bitsPerChar / bitsPerByte ) ;
132+ var lShift = slice * bitsPerChar % bitsPerByte ;
133+
134+ var ndx = ( bytes [ bNum + lOffset ] << lShift & 0xff ) >> rShift ;
135+
136+ var rOffset = Math . ceil ( slice * bitsPerChar / bitsPerByte ) ;
137+ var rShiftIt = ( ( rOffset + 1 ) * bitsPerByte - ( slice + 1 ) * bitsPerChar ) % bitsPerByte ;
138+ if ( rShift < rShiftIt ) {
139+ ndx += bytes [ bNum + rOffset ] >> rShiftIt ;
140+ }
141+ return ndx ;
142+ } ;
143+ }
116144} ;
0 commit comments