Skip to content

Commit

Permalink
Add base64Encode and base64Decode functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dleitee committed May 1, 2016
1 parent bc9474c commit 81bc11e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/string.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,11 @@ const urlDecode = (value) => decodeURI(value);

export {urlDecode};

const base64Encode = (value) => new Buffer(value).toString('base64');

export {base64Encode};

const base64Decode = (value) => new Buffer(value, 'base64').toString();

export {base64Decode};

22 changes: 21 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {isString, trim, removeSpaces, replace, removeNonChars, removeNonWords, a
length, leftPad, rightPad, prepend, removeLeft, appendArray, prependArray, removeRight,
repeat, reverse, shuffle, surround, safeTruncate, transliterate, truncate, removeEmptyStrings,
format, compare, equal, inequal, hexEncode, hexDecode, binEncode, binDecode, decEncode,
decDecode}
decDecode, base64Encode, base64Decode}
from '../src/strman';

describe('isString function', () => {
Expand Down Expand Up @@ -967,3 +967,23 @@ describe('decDecode function', () => {
chai.expect(decDecode('0006500065')).to.equal('AA');
});
});

describe('base64Encode function', () => {
it('should be string', () => {
chai.expect(base64Encode('Daniel')).to.equal('RGFuaWVs');
chai.expect(base64Encode('foo')).to.equal('Zm9v');
chai.expect(base64Encode('bar')).to.equal('YmFy');
chai.expect(base64Encode('bár!')).to.equal('YsOhciE=');
chai.expect(base64Encode('漢')).to.equal('5ryi');
});
});

describe('base64Decode function', () => {
it('should be string', () => {
chai.expect(base64Decode('RGFuaWVs')).to.equal('Daniel');
chai.expect(base64Decode('Zm9v')).to.equal('foo');
chai.expect(base64Decode('YmFy')).to.equal('bar');
chai.expect(base64Decode('YsOhciE=')).to.equal('bár!');
chai.expect(base64Decode('5ryi')).to.equal('漢');
});
});

0 comments on commit 81bc11e

Please sign in to comment.