Skip to content

Commit

Permalink
Add hexDecode function
Browse files Browse the repository at this point in the history
  • Loading branch information
dleitee committed Apr 30, 2016
1 parent 89a2d81 commit 1644ac5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/string.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,12 @@ const inequal = (stringA, stringB) => stringA !== stringB;

export {inequal};

const strToHex = (value) =>
const hexEncode = (value) =>
chars(value).map((data) => leftPad(data.charCodeAt(0).toString(16), 4, '0')).join('');

export {strToHex};
export {hexEncode};

const hexDecode = (value) =>
value.match(/.{1,4}/g).map((data)=>String.fromCharCode(parseInt(data, 16))).join('');

export {hexDecode};
22 changes: 16 additions & 6 deletions 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, strToHex}
format, compare, equal, inequal, hexEncode, hexDecode}
from '../src/strman';

describe('isString function', () => {
Expand Down Expand Up @@ -913,11 +913,21 @@ describe('inequal function', () => {
});
});

describe('strToHex function', () => {
describe('hexEncode function', () => {
it('should be hexadecimal', () => {
chai.expect(strToHex('漢')).to.equal('6f22');
chai.expect(strToHex('A')).to.equal('0041');
chai.expect(strToHex('Á')).to.equal('00c1');
chai.expect(strToHex('AA')).to.equal('00410041');
chai.expect(hexEncode('漢')).to.equal('6f22');
chai.expect(hexEncode('A')).to.equal('0041');
chai.expect(hexEncode('Á')).to.equal('00c1');
chai.expect(hexEncode('AA')).to.equal('00410041');
});
});

describe('hexDecode function', () => {
it('should be string', () => {
chai.expect(hexDecode('6f22')).to.equal('漢');
chai.expect(hexDecode('0041')).to.equal('A');
chai.expect(hexDecode('00c1')).to.equal('Á');
chai.expect(hexDecode('00410041')).to.equal('AA');
});
});

0 comments on commit 1644ac5

Please sign in to comment.