Skip to content

Instantly share code, notes, and snippets.

@mediaupstream
Last active May 16, 2017 18:16
Show Gist options
  • Save mediaupstream/b6f9253890241cdf1e9a3a321c353b7b to your computer and use it in GitHub Desktop.
Save mediaupstream/b6f9253890241cdf1e9a3a321c353b7b to your computer and use it in GitHub Desktop.
node.js base64 class
/**
* Base64 encode / decode strings, objects, arrays, etc
*/
class base64 {
static encode (data) {
return new Buffer(JSON.stringify(data)).toString('base64')
}
static decode (data) {
return JSON.parse( new Buffer(data, 'base64').toString() )
}
}
module.exports = base64
let data = [
'test string',
12345,
[1,2,3],
['one', 'two', 'three'],
[1,2,'three'],
[[1,2], [2,3], [3,4]],
{foo:'bar'},
{foo:[1,2,3]},
{foo:[[1,2],[2,3],[3,4]], baz:'baz'},
[{id:1}, {id:2}, {id:3}],
{foo: {bar: 'baz'}},
'漢字',
['漢字'],
{foo:'漢字'}
]
for( item of data ){
let a = base64.encode(item)
let b = base64.decode(a)
console.log(item)
console.log(b)
console.log(a)
console.log('--------------------')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment