Skip to content

Commit effc6ed

Browse files
authored
Fix and Update πŸš€ ! (#4)
* update .gitignore file 🐞 .. * disable package-lock.json 🚫 .. * add test cases ☒️ .. * add eslint to pkg.json --lint πŸ’„ .. * run lint --eslint πŸ’…πŸ» .. * cleanup 🎯 .. * add koa contributors to license 🌍 .. * add test coverage πŸ“¦ .. * update README.md πŸ“‹ .. * add CI pipeline πŸ“¦ .. * better stuctrue πŸŽ— .. * add method name --'isJSON' πŸš€ .. * add missing condition πŸš€ .. * add test case ☒️ ..
1 parent e8e2633 commit effc6ed

File tree

10 files changed

+143
-62
lines changed

10 files changed

+143
-62
lines changed

β€Ž.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
extends: standard

β€Ž.gitignore

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,23 @@
1-
# Compiled source #
1+
# OS #
22
###################
3-
*.com
4-
*.class
5-
*.dll
6-
*.exe
7-
*.o
8-
*.so
9-
10-
# Packages #
11-
############
12-
# it's better to unpack these files and commit the raw source
13-
# git has its own built in compression methods
14-
*.7z
15-
*.dmg
16-
*.gz
17-
*.iso
18-
*.jar
19-
*.rar
20-
*.tar
21-
*.zip
22-
23-
# Logs and databases #
24-
######################
25-
*.log
26-
*.sql
27-
*.sqlite
28-
29-
# OS generated files #
30-
######################
31-
.DS_Store*
32-
ehthumbs.db
33-
Icon?
3+
.DS_Store
4+
.idea
345
Thumbs.db
6+
tmp/
7+
temp/
358

36-
# Node.js #
37-
###########
38-
lib-cov
39-
*.seed
40-
*.log
41-
*.csv
42-
*.dat
43-
*.out
44-
*.pid
45-
*.gz
46-
47-
pids
48-
logs
49-
results
509

10+
# Node.js #
11+
###################
5112
node_modules
13+
package-lock.json
5214
npm-debug.log
15+
yarn-debug.log
16+
yarn-error.log
5317

54-
# Components #
55-
##############
5618

57-
/build
58-
/components
19+
# NYC #
20+
###################
21+
coverage
22+
*.lcov
23+
.nyc_output

β€Ž.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

β€Ž.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: node_js
2+
node_js:
3+
- 8
4+
- 10
5+
- 12
6+
- 'lts/*'
7+
- 'node'
8+
script:
9+
- npm run test-cov
10+
after_script:
11+
- npm i coveralls
12+
- cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls

β€ŽLICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
The MIT License (MIT)
33

44
Copyright (c) 2014 Jonathan Ong me@jongleberry.com
5+
Copyright (c) 2020 Koa contributors
56

67
Permission is hereby granted, free of charge, to any person obtaining a copy
78
of this software and associated documentation files (the "Software"), to deal

β€ŽREADME.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
# Koa Is JSON
1+
# [**koa-is-json**](https://github.com/koajs/is-json)
22

33
[![Greenkeeper badge](https://badges.greenkeeper.io/koajs/is-json.svg)](https://greenkeeper.io/)
44

5-
Check if a body is JSON
5+
> Check if a body is JSON.
6+
7+
8+
## License
9+
10+
[MIT](/LICENSE)

β€Žindex.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

2-
module.exports = isJSON;
3-
42
/**
53
* Check if `body` should be interpreted as json.
4+
*
65
*/
76

8-
function isJSON(body) {
9-
if (!body) return false;
10-
if ('string' == typeof body) return false;
11-
if ('function' == typeof body.pipe) return false;
12-
if (Buffer.isBuffer(body)) return false;
13-
return true;
7+
module.exports = function isJSON (body) {
8+
return !(
9+
!body ||
10+
typeof body === 'string' ||
11+
typeof body.pipe === 'function' ||
12+
Buffer.isBuffer(body)
13+
)
1414
}

β€Žpackage.json

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
11
{
22
"name": "koa-is-json",
3-
"description": "check if a koa body should be interpreted as JSON",
3+
"description": "Check if a koa body should be interpreted as JSON",
44
"version": "1.0.0",
5+
"scripts": {
6+
"lint": "eslint --fix .",
7+
"test-only": "mocha",
8+
"test": "npm run lint & npm run test-only",
9+
"test-cov": "nyc npm run test"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/koajs/is-json.git"
14+
},
15+
"keywords": [
16+
"json",
17+
"is-json",
18+
"koa"
19+
],
520
"author": {
621
"name": "Jonathan Ong",
722
"email": "me@jongleberry.com",
823
"url": "http://jongleberry.com",
924
"twitter": "https://twitter.com/jongleberry"
1025
},
26+
"contributors": [
27+
{
28+
"name": "Haoxin",
29+
"email": "haoxinst@gmail.com",
30+
"url": "https://medium.com/@haoxin",
31+
"twitter": "https://twitter.com/haoxins"
32+
},
33+
{
34+
"name": "Imed Jaberi",
35+
"email": "imed_jebari@hotmail.fr",
36+
"url": "https://github.com/3imed-jaberi",
37+
"twitter": "https://twitter.com/3imed_jaberi"
38+
}
39+
],
1140
"license": "MIT",
12-
"repository": "koajs/is-json"
41+
"nyc": {
42+
"reporter": [
43+
"lcov",
44+
"text-summary"
45+
],
46+
"report-dir": "./coverage"
47+
},
48+
"devDependencies": {
49+
"eslint": "^4.15.0",
50+
"eslint-config-standard": "^11.0.0-beta.0",
51+
"eslint-plugin-import": "^2.8.0",
52+
"eslint-plugin-node": "^5.2.1",
53+
"eslint-plugin-promise": "^4.2.1",
54+
"eslint-plugin-standard": "^3.0.1",
55+
"mocha": "^7.0.1",
56+
"nyc": "^15.0.0"
57+
},
58+
"engines": {
59+
"node": ">= 8"
60+
},
61+
"bugs": {
62+
"url": "https://github.com/koajs/is-json/issues"
63+
},
64+
"homepage": "https://github.com/koajs/is-json#readme"
1365
}

β€Žtest/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
env:
3+
mocha: true

β€Žtest/index.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
const assert = require('assert')
3+
const isJSON = require('..')
4+
5+
describe('koa-is-json test', () => {
6+
let body
7+
8+
it('Condition 1: !body', () => {
9+
assert.ok(!isJSON())
10+
})
11+
12+
it('Condition 2: \'string\' == typeof body', () => {
13+
body = 'hello world !'
14+
assert.ok(!isJSON(body))
15+
})
16+
17+
it('Condition 3: \'function\' == typeof body.pipe', () => {
18+
body = {
19+
pipe: () => {
20+
return 'hello world !'
21+
}
22+
}
23+
24+
assert.ok(!isJSON(body))
25+
})
26+
27+
it('Condition 4: Buffer.isBuffer(body)', () => {
28+
body = Buffer.alloc(5)
29+
assert.ok(!isJSON(body))
30+
})
31+
32+
it('Condition 5: check null', () => {
33+
assert.ok(!isJSON(null))
34+
})
35+
36+
it('Condition 6: check correct body', () => {
37+
body = JSON.parse(JSON.stringify({ msg: 'hello world !' }))
38+
assert.ok(isJSON(body))
39+
})
40+
})

0 commit comments

Comments
 (0)