Skip to content

Commit

Permalink
Convert to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Kennedy committed Apr 4, 2020
1 parent 8b7404c commit b741f88
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 80 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage/
node_modules/
npm-debug.log
npm-debug.log
index.js
index.d.ts
50 changes: 0 additions & 50 deletions index.d.ts

This file was deleted.

58 changes: 46 additions & 12 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Module variables.
* @private
*/

var decode = decodeURIComponent;
Expand All @@ -26,9 +27,10 @@ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
* @param {string} str
* @param {object} [options]
* @return {object}
* @private
*/

function parse(str, options) {
function parse(str, options: any = {}) {
if (typeof str !== 'string') {
throw new TypeError('argument str must be a string');
}
Expand Down Expand Up @@ -77,9 +79,10 @@ function parse(str, options) {
* @param {string} val
* @param {object} [options]
* @return {string}
* @private
*/

function serialize(name, val, options) {
function serialize(name, val, options: any = {}) {
var opt = options || {};
var enc = opt.encode || encode;

Expand Down Expand Up @@ -169,6 +172,7 @@ function serialize(name, val, options) {
*
* @param {string} str
* @param {function} decode
* @private
*/

function tryDecode(str, decode) {
Expand All @@ -179,16 +183,46 @@ function tryDecode(str, decode) {
}
}

function get() {}

function set() {}

function getAll() {}
const CookieStore = {
/**
* Get a cookie.
*
* @param {string} name
* @return {Promise}
*/
get(name) {
return Promise.resolve(parse(document.cookie)[name]);
},

/**
* Set a cookie.
*
* @param {string} name
* @param {string} value
* @return {Promise}
*/
set(name, value) {
return new Promise(function (resolve, reject) {
const cookieString = serialize(name, value);
document.cookie = cookieString;
resolve();
});
},

/**
* Get multiple cookies.
*
* @return {Promise}
*/
getAll() {
throw Error('getAll not implemented, coming soon though.');
},
};

if (!window.cookieStore) {
window.cookieStore = {
get: get,
set: set,
getAll: getAll,
};
window.cookieStore = CookieStore;
}

interface Window {
cookieStore: typeof CookieStore;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"banner": "banner-cli index.js",
"lint": "npm run lint:js && npm run lint:types",
"lint:js": "eslint '**/*.{js,ts}'",
"lint:types": "tsc --noEmit",
"build": "tsc",
"coveralls": "cat .coverage/lcov.info | coveralls"
}
}
}
15 changes: 5 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["es2017", "dom"],
"allowJs": true,
"checkJs": true,
"target": "es5",
"module": "commonjs",
"strict": false,
"noImplicitThis": true,
"alwaysStrict": true,
"esModuleInterop": true,
"noEmit": false,
"declaration": true
"declaration": true,
"lib": ["ES5", "dom", "ES2015"]
},
"include": ["index.js"],
"files": ["types.ts"]
"files": ["index.ts"]
}
5 changes: 0 additions & 5 deletions types.ts

This file was deleted.

0 comments on commit b741f88

Please sign in to comment.