2018-07-21 12:37:39 +00:00
|
|
|
import * as plugins from './smartstring.plugins';
|
2016-10-31 23:22:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the type for base 64
|
|
|
|
*/
|
2018-07-21 12:37:39 +00:00
|
|
|
export type TBase64Input = 'string' | 'base64' | 'base64uri';
|
2016-10-31 23:22:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* handle base64 strings
|
|
|
|
*/
|
|
|
|
export class Base64 {
|
2018-07-21 12:37:39 +00:00
|
|
|
private refString: string;
|
2017-10-05 13:55:59 +00:00
|
|
|
constructor(inputStringArg, typeArg: TBase64Input) {
|
|
|
|
switch (typeArg) {
|
|
|
|
case 'string': // easiest case
|
2018-07-21 12:37:39 +00:00
|
|
|
this.refString = inputStringArg;
|
|
|
|
break;
|
2017-10-05 13:55:59 +00:00
|
|
|
case 'base64':
|
2018-07-21 12:37:39 +00:00
|
|
|
this.refString = base64.decode(inputStringArg);
|
|
|
|
break;
|
2017-10-05 13:55:59 +00:00
|
|
|
case 'base64uri':
|
2018-07-21 12:37:39 +00:00
|
|
|
this.refString = base64.decode(inputStringArg);
|
2016-10-31 23:22:38 +00:00
|
|
|
}
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2016-10-31 23:22:38 +00:00
|
|
|
|
2017-10-05 13:55:59 +00:00
|
|
|
/**
|
|
|
|
* the simple string (unencoded)
|
|
|
|
*/
|
2018-07-21 12:37:39 +00:00
|
|
|
get simpleString() {
|
|
|
|
return this.refString;
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2016-10-31 23:22:38 +00:00
|
|
|
|
2017-10-05 13:55:59 +00:00
|
|
|
/**
|
|
|
|
* the base64 encoded version of the original string
|
|
|
|
*/
|
2018-07-21 12:37:39 +00:00
|
|
|
get base64String() {
|
|
|
|
return base64.encode(this.refString);
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2016-10-31 23:22:38 +00:00
|
|
|
|
2017-10-05 13:55:59 +00:00
|
|
|
/**
|
|
|
|
* the base64uri encoded version of the original string
|
|
|
|
*/
|
2018-07-21 12:37:39 +00:00
|
|
|
get base64UriString() {
|
|
|
|
return base64.encodeUri(this.refString);
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2016-11-01 17:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export let base64 = {
|
2017-10-05 13:55:59 +00:00
|
|
|
/**
|
|
|
|
* encodes the string
|
|
|
|
*/
|
|
|
|
encode: (stringArg: string) => {
|
2018-07-21 12:37:39 +00:00
|
|
|
return plugins.jsBase64.encode(stringArg);
|
2017-10-05 13:55:59 +00:00
|
|
|
},
|
2016-11-01 17:59:43 +00:00
|
|
|
|
2017-10-05 13:55:59 +00:00
|
|
|
/**
|
|
|
|
* encodes a stringArg to base64 uri style
|
|
|
|
*/
|
|
|
|
encodeUri: (stringArg: string) => {
|
2018-07-21 12:37:39 +00:00
|
|
|
return plugins.jsBase64.encodeURI(stringArg);
|
2017-10-05 13:55:59 +00:00
|
|
|
},
|
2016-11-01 17:59:43 +00:00
|
|
|
|
2017-10-05 13:55:59 +00:00
|
|
|
/**
|
|
|
|
* decodes a base64 encoded string
|
|
|
|
*/
|
|
|
|
decode: (stringArg: string) => {
|
2018-07-21 12:37:39 +00:00
|
|
|
return plugins.jsBase64.decode(stringArg);
|
2020-12-31 03:56:40 +00:00
|
|
|
},
|
2018-07-21 12:37:39 +00:00
|
|
|
};
|