Skip to content

Commit a01c26d

Browse files
committed
feat(ipcInfo): utility to get and set ipcInfo cookie
1 parent 898c722 commit a01c26d

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed
9.78 KB
Binary file not shown.

packages/utilities/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"jsdoc": "rimraf docs && jsdoc -c ./jsdoc.json ./README.md"
3535
},
3636
"dependencies": {
37+
"js-cookie": "^2.2.1",
3738
"window-or-global": "^1.0.1"
3839
},
3940
"devDependencies": {

packages/utilities/src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
export * from './escaperegexp';
99
export * from './featureflag';
10+
export * from './ipcinfoCookie';
1011
export * from './serialize';
1112
export * from './settings';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ipcinfoCookie } from '../';
2+
3+
describe('ipcinfo cookie utility', () => {
4+
beforeEach(() => {
5+
const document = {
6+
cookie: 'ipcInfo=cc%253DUS%253Blc%253Den',
7+
};
8+
global.document = document;
9+
});
10+
11+
// it('should return undefined if cookie is not there', () => {
12+
// const ipcinfo = ipcinfoCookie.get();
13+
// expect(ipcinfo).toBe(undefined);
14+
// });
15+
16+
it('should fetch the ipcInfo cookie and return a neat object', () => {
17+
const info = {
18+
cc: 'US',
19+
lc: 'en',
20+
};
21+
22+
const ipcinfo = ipcinfoCookie.get();
23+
expect(ipcinfo).toBe(info);
24+
});
25+
26+
// it('should set the ipcInfo cookie', () => {
27+
// const output = ipcinfoCookie.set('US', 'en');
28+
// });
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as ipcinfoCookie } from './ipcinfoCookie';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Cookies from 'js-cookie';
2+
3+
/**
4+
* Utility to set and get the ipcInfo cookie needed to determine country and language code
5+
*
6+
*/
7+
class ipcinfoCookie {
8+
/**
9+
* retreive the ipcInfo cookie that contains the cc and lc
10+
* decodes and converts to object
11+
*
12+
* @returns {object} objects ipc info object
13+
*/
14+
static get() {
15+
const ipcinfo = Cookies.get('ipcInfo');
16+
if (ipcinfo) {
17+
let cc;
18+
let lc;
19+
const info = decodeURIComponent(ipcinfo).split(';');
20+
info.map(code => {
21+
const itemParts = code.split('=');
22+
if (itemParts[0] === 'cc') cc = itemParts[1];
23+
if (itemParts[0] === 'lc') lc = itemParts[1];
24+
});
25+
26+
return { cc, lc };
27+
}
28+
}
29+
30+
/**
31+
* set the ipcInfo cookie
32+
* takes care of converting to string and encoding
33+
*
34+
* @param {string} cc country coude
35+
* @param {string} lc language code
36+
*
37+
*/
38+
static set(cc, lc) {
39+
const info = `cc=${cc};lc=${lc}`;
40+
41+
Cookies.set('ipcInfo', encodeURIComponent(info));
42+
}
43+
}
44+
45+
export default ipcinfoCookie;

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -11131,6 +11131,11 @@ js-base64@^2.1.8:
1113111131
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.5.1.tgz#1efa39ef2c5f7980bb1784ade4a8af2de3291121"
1113211132
integrity sha512-M7kLczedRMYX4L8Mdh4MzyAMM9O5osx+4FcOQuTvr3A9F2D9S5JXheN0ewNbrvK2UatkTRhL5ejGmGSjNMiZuw==
1113311133

11134+
js-cookie@^2.2.1:
11135+
version "2.2.1"
11136+
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.2.1.tgz#69e106dc5d5806894562902aa5baec3744e9b2b8"
11137+
integrity sha512-HvdH2LzI/EAZcUwA8+0nKNtWHqS+ZmijLA30RwZA0bo7ToCckjK5MkGhjED9KoRcXO6BaGI3I9UIzSA1FKFPOQ==
11138+
1113411139
js-levenshtein@^1.1.3:
1113511140
version "1.1.6"
1113611141
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"

0 commit comments

Comments
 (0)