Skip to content

Commit

Permalink
Add: eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
brick9527 committed Aug 5, 2020
1 parent 7554213 commit 1115904
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
33 changes: 33 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const { decode } = require('./lib/decode');
module.exports = {
encodeJWT: encode,
decodeJWT: decode,
}
};
2 changes: 1 addition & 1 deletion lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const decode = function (jwt, secretKey, callback = null) {
return true;
}
return { ...jwtBody };
}
};

module.exports = {
decode,
Expand Down
6 changes: 3 additions & 3 deletions lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const encode = function(options, data = {}, callback = null) {
} else if (getType(data) === 'Object') {
dataObj = JSON.parse(JSON.stringify(data));
} else {
const error = new Error(`Invalid param 'data', expect 'string' or 'object' but got ${getType(data)}.`)
const error = new Error(`Invalid param 'data', expect 'string' or 'object' but got ${getType(data)}.`);
errorHandler(error, callback);
}

Expand All @@ -45,7 +45,7 @@ const encode = function(options, data = {}, callback = null) {

// 第二部分加密
const jwtBody = {
...data,
...dataObj,
startTime: new Date().toString(),
};
const jwtBodyStr = Buffer.from(JSON.stringify(jwtBody)).toString('base64');
Expand All @@ -61,7 +61,7 @@ const encode = function(options, data = {}, callback = null) {
callback(null, jwtArr.join('.'));
}
return jwtArr.join('.');
}
};

module.exports = {
encode,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"lib": "lib"
},
"scripts": {
"test": "mocha"
"test": "mocha",
"lint": "eslint ."
},
"repository": {
"type": "git",
Expand All @@ -25,6 +26,7 @@
},
"homepage": "https://github.com/brick9527/simple-jwt#readme",
"devDependencies": {
"eslint": "^7.6.0",
"mocha": "^8.1.1"
}
}
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-undef */

const assert = require('assert');
const { getType } = require('../util/functions');

Expand Down Expand Up @@ -65,7 +67,7 @@ describe('decodeJWT Test', function() {
decodeJWT(jwt, options.secretKey, function(err, data) {
assert.equal(err, null);
assert.equal(data.id, 123);
})
});
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions util/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const errorHandler = function(error, callback) {
return false;
}
throw error;
}
};

/**
* 获取数据的真实类型
Expand All @@ -23,7 +23,7 @@ const errorHandler = function(error, callback) {
*/
const getType = function(param) {
return Object.prototype.toString.call(param).replace(']', '').replace('[object ', '');
}
};

module.exports = {
errorHandler,
Expand Down

0 comments on commit 1115904

Please sign in to comment.