Skip to content

Commit

Permalink
fix key encoding per issue #125
Browse files Browse the repository at this point in the history
  • Loading branch information
bcripps-indeed committed Feb 23, 2017
1 parent 05b86ac commit a462afd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
16 changes: 9 additions & 7 deletions src/util/keyGenerator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
export function keyGenerator(...keywords) {
return btoa(unescape(encodeURIComponent(Array.from(keywords).join(''))));
}
export const keyGenerator = (...keywords) => encode(
Array.from(keywords).join('')
);

export function keyFromObject(obj, additionalStrings) {
export const keyFromObject = (obj, additionalStrings) => {

if (additionalStrings && Array.isArray(additionalStrings)) {
return btoa(
return encode(
additionalStrings.join('') + Object.keys(obj
).map((k) => obj[k]).join(''));
}

return btoa(Object.keys(obj).map((k) => obj[k]).join(''));
}
return encode(Object.keys(obj).map((k) => obj[k]).join(''));
};

export const encode = s => btoa(unescape(encodeURIComponent(s)));
71 changes: 49 additions & 22 deletions test/util/keyGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,58 @@ import expect from 'expect';
import { keyGenerator, keyFromObject } from './../../src/util/keyGenerator';

describe('keyGenerator utility function', () => {
const keywords1 = ['keyword', 'keywordA', 'keywordB'];
const keywords2 = ['react', 'redux', 'grid', 'test'];
it('Should work with regular utf-8 strings', () => {
const keywords1 = ['keyword', 'keywordA', 'keywordB'];
const keywords2 = ['react', 'redux', 'grid', 'test'];

expect(keyGenerator(keywords1))
.toEqual('a2V5d29yZCxrZXl3b3JkQSxrZXl3b3JkQg==');
expect(keyGenerator(keywords2))
.toEqual('cmVhY3QscmVkdXgsZ3JpZCx0ZXN0');
expect(keyGenerator(keywords1))
.toEqual('a2V5d29yZCxrZXl3b3JkQSxrZXl3b3JkQg==');
expect(keyGenerator(keywords2))
.toEqual('cmVhY3QscmVkdXgsZ3JpZCx0ZXN0');
});

it('Should work with irregular strings', () => {
expect(keyGenerator('😫', 'haha'))
.toEqual('8J+Yq2hhaGE=');
});
});

describe('keyFromObject utility function', () => {
const keywords1 = {
someKey: 'someKey',
anotherKey: 'anotherKey'
};

const keywords2 = {
react: 'react',
redux: 'redux',
grid: 'grid',
test: 'test'
};

expect(keyFromObject(keywords1))
.toEqual('c29tZUtleWFub3RoZXJLZXk=');
expect(keyFromObject(keywords2))
.toEqual('cmVhY3RyZWR1eGdyaWR0ZXN0');
it('Should work wirh utf-8 string', () => {
const keywords1 = {
someKey: 'someKey',
anotherKey: 'anotherKey'
};

const keywords2 = {
react: 'react',
redux: 'redux',
grid: 'grid',
test: 'test'
};

expect(keyFromObject(keywords1))
.toEqual('c29tZUtleWFub3RoZXJLZXk=');
expect(keyFromObject(keywords2))
.toEqual('cmVhY3RyZWR1eGdyaWR0ZXN0');
});

it('Should work with irregular strings', () => {
const irregular = {
someKey: '😫'
};

expect(keyFromObject(irregular))
.toEqual('8J+Yqw==');
});

it('Should work with irregular strings, with additional', () => {
const irregular = {
someKey: '😫'
};

expect(keyFromObject(irregular, ['banana']))
.toEqual('YmFuYW5h8J+Yqw==');
});

});

0 comments on commit a462afd

Please sign in to comment.