@@ -24,7 +24,7 @@ SOFTWARE.
2424
2525*/
2626
27- // Encoder.js v1.0.1
27+ // Encoder.js v1.0.1'
2828
2929/*
3030
@@ -171,9 +171,14 @@ const characterMap = [
171171] ;
172172
173173const errorprefix = 'Encoder.js Error: ' ;
174+ const encddecdpfx = ( insertmode ) => { return `The text cannot be ${ insertmode } for the following reason: ` } ;
175+ const docsurllink = 'https://encoder.js.is-a.dev/en/docs' ;
174176const errors = [
175177 `${ errorprefix } The string to be decoded is not correctly encoded.` ,
176- `${ errorprefix } Something went wrong.`
178+ `${ errorprefix } Something went wrong.` ,
179+ ( mode_ , param , paramtype ) => { return `${ errorprefix } ${ encddecdpfx ( mode_ ) } ${ param } must be a ${ paramtype } . ${ docsurllink } ` } ,
180+ ( mode_ , param ) => { return `${ errorprefix } ${ encddecdpfx ( mode_ ) } ${ param } is required. ${ docsurllink } ` } ,
181+ ( mode_ , param , info ) => { return `${ errorprefix } ${ encddecdpfx ( mode_ ) } ${ param } cannot be ${ info } . ${ docsurllink } ` }
177182]
178183const knownErrors = [
179184 "InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded." ,
@@ -596,7 +601,7 @@ function throwNewError(catchedError, id_) {
596601 }
597602}
598603
599- export const encode = ( text , key , compress ) => {
604+ function encode4 ( text , key , compress ) {
600605 let encd ;
601606 let inputText = text ;
602607 if ( key && typeof key == 'string' && key != '' ) {
@@ -624,7 +629,8 @@ export const encode = (text, key, compress) => {
624629 } else encde ( ) ;
625630 return encode3 ( encd ) ;
626631} ;
627- export const decode = ( text , key ) => {
632+
633+ function decode4 ( text , key ) {
628634 let inputText = decode3 ( text ) ;
629635 let datachar = inputText . slice ( 0 , 1 ) ;
630636 let decd ;
@@ -664,3 +670,49 @@ export const decode = (text, key) => {
664670 }
665671 return decd ;
666672} ;
673+
674+ function check ( text , key ) {
675+ if ( ! / ^ [ A - Z a - z 0 - 9 _ ( ) . ~ - ] + $ / . test ( text ) ) throw new Error ( errors [ 0 ] ) ;
676+ if ( text . length > 2 && text !== 'V' && ( key === undefined || key === null ) ) throw new Error ( errors [ 0 ] ) ;
677+ }
678+ function even_or_odd ( number ) {
679+ return number % 2 === 0 ? false : true ;
680+ }
681+
682+ export const encode = ( text , key , compress ) => {
683+ if ( text === undefined || text === null ) throw new Error ( errors [ 3 ] ( 'encoded' , 'Text' ) ) ;
684+ if ( typeof text !== 'string' ) throw new Error ( errors [ 2 ] ( 'encoded' , 'Text' , 'string' ) ) ;
685+ if ( key !== undefined && key !== null && typeof key !== 'string' ) throw new Error ( errors [ 2 ] ( 'encoded' , 'Key' , 'string' ) ) ;
686+ if ( compress !== undefined && compress !== null && typeof compress !== 'boolean' ) throw new Error ( errors [ 2 ] ( 'encoded' , 'Compress' , 'boolean' ) ) ;
687+ if ( text === '' ) throw new Error ( errors [ 4 ] ( 'encoded' , 'Text' , text ) ) ;
688+ const encoded_ = encode4 ( text , key , compress ) ;
689+ let output_ = encoded_ ;
690+ if ( compress ) {
691+ const doubleEncoded = encode4 ( encoded_ , key , compress ) ;
692+ if ( doubleEncoded . length < encoded_ . length ) {
693+ output_ = `J${ doubleEncoded } ` ;
694+ }
695+ }
696+ if ( even_or_odd ( output_ . length ) ) output_ = output_ . split ( '' ) . reverse ( ) . join ( '' ) ;
697+ return output_ ;
698+ } ;
699+ export const decode = ( text , key ) => {
700+ let input_ = text ;
701+ if ( even_or_odd ( input_ . length ) ) input_ = input_ . split ( '' ) . reverse ( ) . join ( '' ) ;
702+ if ( input_ === undefined || input_ === null ) throw new Error ( errors [ 3 ] ( 'decoded' , 'Text' ) ) ;
703+ if ( typeof input_ !== 'string' ) throw new Error ( errors [ 2 ] ( 'decoded' , 'Text' , 'string' ) ) ;
704+ if ( key !== undefined && key !== null && typeof key !== 'string' ) throw new Error ( errors [ 2 ] ( 'decoded' , 'Key' , 'string' ) ) ;
705+ check ( input_ , key ) ;
706+ const datachar = input_ . slice ( 0 , 1 ) ;
707+ const nodatachar = input_ . slice ( 1 ) ;
708+ let output_ ;
709+ if ( datachar == 'J' ) {
710+ const decoded_ = decode4 ( decode4 ( nodatachar , key ) , key ) ;
711+ output_ = decoded_ ;
712+ } else {
713+ const decoded_ = decode4 ( input_ , key ) ;
714+ output_ = decoded_ ;
715+ }
716+ return output_ ;
717+ } ;
718+
0 commit comments