@@ -169,11 +169,17 @@ const characterMap = [
169169 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
170170 '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '-' , '_' , '(' , ')' , '.' , '~'
171171] ;
172+ const cc = characterMap . length ;
172173
173174const errorprefix = 'Encoder.js Error: ' ;
175+ const encddecdpfx = ( insertmode ) => { return `The text cannot be ${ insertmode } for the following reason: ` } ;
176+ const docsurllink = 'https://encoder.js.is-a.dev/en/docs' ;
174177const errors = [
175178 `${ errorprefix } The string to be decoded is not correctly encoded.` ,
176- `${ errorprefix } Something went wrong.`
179+ `${ errorprefix } Something went wrong.` ,
180+ ( mode_ , param , paramtype ) => { return `${ errorprefix } ${ encddecdpfx ( mode_ ) } ${ param } must be a ${ paramtype } . ${ docsurllink } ` } ,
181+ ( mode_ , param ) => { return `${ errorprefix } ${ encddecdpfx ( mode_ ) } ${ param } is required. ${ docsurllink } ` } ,
182+ ( mode_ , param , info ) => { return `${ errorprefix } ${ encddecdpfx ( mode_ ) } ${ param } cannot be ${ info } . ${ docsurllink } ` }
177183]
178184const knownErrors = [
179185 "InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded." ,
@@ -596,7 +602,8 @@ function throwNewError(catchedError, id_) {
596602 }
597603}
598604
599- export const encode = ( text , key , compress ) => {
605+ const useShit = false ;
606+ function encode4 ( text , key , compress ) {
600607 let encd ;
601608 let inputText = text ;
602609 if ( key && typeof key == 'string' && key != '' ) {
@@ -614,7 +621,7 @@ export const encode = (text, key, compress) => {
614621 encd = `${ datachar } ${ enc2 . slice ( 1 ) } ` ;
615622 }
616623 }
617- if ( compress && / ^ \d + $ / . test ( inputText ) && inputText . length < 11 ) {
624+ if ( useShit && compress && / ^ \d + $ / . test ( inputText ) && inputText . length < 11 ) {
618625 try {
619626 const base62num = intToBase62 ( parseInt ( inputText ) ) ;
620627 if ( base62num === base62ToInt ( base62num ) ) {
@@ -624,12 +631,12 @@ export const encode = (text, key, compress) => {
624631 } else encde ( ) ;
625632 return encode3 ( encd ) ;
626633} ;
627- export const decode = ( text , key ) => {
634+ function decode4 ( text , key ) {
628635 let inputText = decode3 ( text ) ;
629636 let datachar = inputText . slice ( 0 , 1 ) ;
630637 let decd ;
631638 let encd = inputText . slice ( 1 ) ;
632- if ( datachar == 'U' ) {
639+ if ( datachar == 'U' && useShit ) {
633640 decd = base62ToInt ( encd ) ;
634641 } else {
635642 if ( ! used . test ( datachar ) ) {
@@ -664,3 +671,130 @@ export const decode = (text, key) => {
664671 }
665672 return decd ;
666673} ;
674+
675+ function check ( text , key ) {
676+ if ( ! / ^ [ A - Z a - z 0 - 9 _ ( ) . ~ - ] + $ / . test ( text ) ) throw new Error ( errors [ 0 ] ) ;
677+ if ( text . length > 2 && text !== 'V' && ( key === undefined || key === null ) ) throw new Error ( errors [ 0 ] ) ;
678+ }
679+ function even_or_odd ( number ) {
680+ return number % 2 === 0 ? false : true ;
681+ }
682+
683+ const numEncode = ( text , key ) => {
684+ let num = '' ;
685+ for ( let i = 0 ; i < text . length ; i ++ ) {
686+ num += key ? `${ ( parseInt ( text . charAt ( i ) , 10 ) + key . charCodeAt ( i % key . length ) ) % 10 } ` : text . charAt ( i ) ;
687+ }
688+ let number = parseInt ( num , 10 ) ;
689+ number = key ? number % parseInt ( text , 10 ) : number ;
690+ if ( `${ number } ` . replaceAll ( 'e' , 'found' ) !== `${ number } ` ) return false ;
691+ let output_ = false ;
692+ if ( number < cc ) {
693+ output_ = 'U' + characterMap [ number ] ;
694+ } else if ( number < 4624 ) {
695+ number -= cc ;
696+ output_ = 'u' + characterMap [ Math . floor ( number / cc ) ] + characterMap [ number % cc ] ;
697+ } else if ( number < 314432 ) {
698+ number -= 4624 ;
699+ output_ = 'Q' + characterMap [ Math . floor ( number / ( cc * cc ) ) ] + characterMap [ Math . floor ( ( number % ( cc * cc ) ) / cc ) ] + characterMap [ number % cc ] ;
700+ } else if ( number < 21381376 ) {
701+ number -= 314432 ;
702+ output_ = 'q' + characterMap [ Math . floor ( number / ( cc * cc * cc ) ) ] + characterMap [ Math . floor ( ( number % ( cc * cc * cc ) ) / ( cc * cc ) ) ] + characterMap [ Math . floor ( ( number % ( cc * cc ) ) / cc ) ] + characterMap [ number % cc ] ;
703+ } else if ( useShit ) {
704+ let result = '' ;
705+ while ( number >= 0 ) {
706+ result = characterMap [ number % cc ] + result ;
707+ number = Math . floor ( number / cc ) - 1 ;
708+ }
709+ output_ = result ;
710+ } else {
711+ return false ;
712+ }
713+ return encode3 ( output_ ) ;
714+ }
715+ const numDecode = ( text , key ) => {
716+ const encoded = decode3 ( text ) ;
717+ const prefix = encoded . charAt ( 0 ) ;
718+ let number = 0 ;
719+ let output_ ;
720+ if ( prefix === 'U' ) {
721+ output_ = ( characterMap . indexOf ( encoded . charAt ( 1 ) ) ) . toString ( ) ;
722+ } else if ( prefix === 'u' ) {
723+ number += cc ;
724+ number += characterMap . indexOf ( encoded . charAt ( 1 ) ) * cc + characterMap . indexOf ( encoded . charAt ( 2 ) ) ;
725+ output_ = number . toString ( ) ;
726+ } else if ( prefix === 'Q' ) {
727+ number += 4624 ;
728+ number += characterMap . indexOf ( encoded . charAt ( 1 ) ) * ( cc * cc ) + characterMap . indexOf ( encoded . charAt ( 2 ) ) * cc + characterMap . indexOf ( encoded . charAt ( 3 ) ) ;
729+ output_ = number . toString ( ) ;
730+ } else if ( prefix === 'q' ) {
731+ number += 314432 ;
732+ number += characterMap . indexOf ( encoded . charAt ( 1 ) ) * ( cc * cc * cc ) + characterMap . indexOf ( encoded . charAt ( 2 ) ) * ( cc * cc ) + characterMap . indexOf ( encoded . charAt ( 3 ) ) * cc + characterMap . indexOf ( encoded . charAt ( 4 ) ) ;
733+ output_ = number . toString ( ) ;
734+ } else {
735+ let base = 0 ;
736+ for ( let i = 0 ; i < encoded . length ; i ++ ) {
737+ base = base * cc + characterMap . indexOf ( encoded . charAt ( i ) ) ;
738+ }
739+ output_ = base . toString ( ) ;
740+ }
741+ let num = '' ;
742+ for ( let i = 0 ; i < output_ . length ; i ++ ) {
743+ num += key ? `${ ( parseInt ( output_ . charAt ( i ) , 10 ) - key . charCodeAt ( i % key . length ) + 10 ) % 10 } ` : output_ . charAt ( i ) ;
744+ }
745+ return num ;
746+ }
747+
748+ function encode5 ( text , key , compress ) {
749+ if ( text === undefined || text === null ) throw new Error ( errors [ 3 ] ( 'encoded' , 'Text' ) ) ;
750+ if ( typeof text !== 'string' ) throw new Error ( errors [ 2 ] ( 'encoded' , 'Text' , 'string' ) ) ;
751+ if ( key !== undefined && key !== null && typeof key !== 'string' ) throw new Error ( errors [ 2 ] ( 'encoded' , 'Key' , 'string' ) ) ;
752+ if ( compress !== undefined && compress !== null && typeof compress !== 'boolean' ) throw new Error ( errors [ 2 ] ( 'encoded' , 'Compress' , 'boolean' ) ) ;
753+ if ( text === '' ) throw new Error ( errors [ 4 ] ( 'encoded' , 'Text' , text ) ) ;
754+ const encoded_ = encode4 ( text , key , compress ) ;
755+ let output_ = encoded_ ;
756+ if ( compress ) {
757+ const doubleEncoded = encode4 ( encoded_ , key , compress ) ;
758+ const numEncoded = numEncode ( text ) ;
759+ const customDecode5 = ( text ) => {
760+ try {
761+ return decode5 ( text ) ;
762+ } catch {
763+ return false ;
764+ }
765+ }
766+ if ( doubleEncoded . length < encoded_ . length ) {
767+ output_ = `J${ doubleEncoded } ` ;
768+ }
769+ if ( / ^ [ 0 - 9 ] + $ / . test ( text ) && ( key === undefined || key === null || key === '' ) && customDecode5 ( numEncoded ) === text ) { output_ = numEncoded }
770+ }
771+ if ( even_or_odd ( output_ . length ) ) output_ = output_ . split ( '' ) . reverse ( ) . join ( '' ) ;
772+ return output_ ;
773+ }
774+ function decode5 ( text , key ) {
775+ let input_ = text ;
776+ if ( even_or_odd ( input_ . length ) ) input_ = input_ . split ( '' ) . reverse ( ) . join ( '' ) ;
777+ if ( input_ === undefined || input_ === null ) throw new Error ( errors [ 3 ] ( 'decoded' , 'Text' ) ) ;
778+ if ( typeof input_ !== 'string' ) throw new Error ( errors [ 2 ] ( 'decoded' , 'Text' , 'string' ) ) ;
779+ if ( key !== undefined && key !== null && typeof key !== 'string' ) throw new Error ( errors [ 2 ] ( 'decoded' , 'Key' , 'string' ) ) ;
780+ check ( input_ , key ) ;
781+ const datachar = input_ . slice ( 0 , 1 ) ;
782+ const nodatachar = input_ . slice ( 1 ) ;
783+ let output_ ;
784+ if ( datachar == 'J' ) {
785+ output_ = decode4 ( decode4 ( nodatachar , key ) , key ) ;
786+ } else if ( datachar == 'U' || datachar == 'u' || datachar == 'Q' || datachar == 'q' ) {
787+ output_ = numDecode ( text ) ;
788+ } else {
789+ output_ = decode4 ( input_ , key ) ;
790+ }
791+ return output_ ;
792+ }
793+
794+ export const encode = ( text , key , compress ) => {
795+ return encode5 ( text , key , compress ) ;
796+ } ;
797+ export const decode = ( text , key ) => {
798+ return decode5 ( text , key ) ;
799+ } ;
800+
0 commit comments