Skip to content

Commit

Permalink
Add binEncode function
Browse files Browse the repository at this point in the history
  • Loading branch information
dleitee committed Apr 30, 2016
1 parent 1644ac5 commit 3331ce6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/string.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,8 @@ const hexDecode = (value) =>
value.match(/.{1,4}/g).map((data)=>String.fromCharCode(parseInt(data, 16))).join('');

export {hexDecode};

const binEncode = (value) =>
chars(value).map((data) => leftPad(data.charCodeAt(0).toString(2), 16, '0')).join('');

export {binEncode};
11 changes: 10 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {isString, trim, removeSpaces, replace, removeNonChars, removeNonWords, a
endsWith, startsWith, ensureLeft, ensureRight, first, last, indexOf, lastIndexOf, insert,
length, leftPad, rightPad, prepend, removeLeft, appendArray, prependArray, removeRight,
repeat, reverse, shuffle, surround, safeTruncate, transliterate, truncate, removeEmptyStrings,
format, compare, equal, inequal, hexEncode, hexDecode}
format, compare, equal, inequal, hexEncode, hexDecode, binEncode}
from '../src/strman';

describe('isString function', () => {
Expand Down Expand Up @@ -931,3 +931,12 @@ describe('hexDecode function', () => {
});
});

describe('binEncode function', () => {
it('should be binary', () => {
chai.expect(binEncode('漢')).to.equal('0110111100100010');
chai.expect(binEncode('A')).to.equal('0000000001000001');
chai.expect(binEncode('Á')).to.equal('0000000011000001');
chai.expect(binEncode('AA')).to.equal('00000000010000010000000001000001');
});
});

0 comments on commit 3331ce6

Please sign in to comment.