smartstring/ts/smartstring.base64.ts

70 lines
1.4 KiB
TypeScript
Raw Normal View History

2016-10-31 23:22:38 +00:00
import * as plugins from './smartstring.plugins'
/**
* the type for base 64
*/
export type TBase64Input = 'string' | 'base64' | 'base64uri'
/**
* handle base64 strings
*/
export class Base64 {
2017-10-05 13:55:59 +00:00
private refString: string
constructor(inputStringArg, typeArg: TBase64Input) {
switch (typeArg) {
case 'string': // easiest case
this.refString = inputStringArg
break
case 'base64':
this.refString = base64.decode(inputStringArg)
break
case 'base64uri':
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)
*/
get simpleString () {
return this.refString
}
2016-10-31 23:22:38 +00:00
2017-10-05 13:55:59 +00:00
/**
* the base64 encoded version of the original string
*/
get base64String () {
return base64.encode(this.refString)
}
2016-10-31 23:22:38 +00:00
2017-10-05 13:55:59 +00:00
/**
* the base64uri encoded version of the original string
*/
get base64UriString () {
return base64.encodeUri(this.refString)
}
}
export let base64 = {
2017-10-05 13:55:59 +00:00
/**
* encodes the string
*/
encode: (stringArg: string) => {
return plugins.jsBase64.encode(stringArg)
},
2017-10-05 13:55:59 +00:00
/**
* encodes a stringArg to base64 uri style
*/
encodeUri: (stringArg: string) => {
return plugins.jsBase64.encodeURI(stringArg)
},
2017-10-05 13:55:59 +00:00
/**
* decodes a base64 encoded string
*/
decode: (stringArg: string) => {
return plugins.jsBase64.decode(stringArg)
}
2016-10-31 23:22:38 +00:00
}