Skip to content

Commit 46d8c2e

Browse files
committed
chore: Update deps and swap lodash for native methods and @sindresorhus/is
Signed-off-by: Richie Bendall <[email protected]>
1 parent 3731477 commit 46d8c2e

File tree

4 files changed

+432
-383
lines changed

4 files changed

+432
-383
lines changed

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,25 @@
2525
"test": "yarn lint && ava"
2626
},
2727
"dependencies": {
28+
"@sindresorhus/is": "^1.2.0",
2829
"cheerio": "^1.0.0-rc.3",
2930
"content-type": "^1.0.4",
3031
"iconv-lite": "^0.5.0",
31-
"lodash": "^4.17.15",
3232
"nice-try": "^2.0.0"
3333
},
3434
"devDependencies": {
3535
"@types/cheerio": "^0.22.13",
3636
"@types/content-type": "^1.1.3",
37-
"@types/lodash": "^4.14.146",
3837
"ava": "^2.4.0",
39-
"eslint-config-richienb": "^0.2.2",
38+
"eslint-config-richienb": "^0.2.3",
4039
"node-fetch": "^2.6.0",
41-
"ts-node": "^8.4.1",
42-
"typedoc": "^0.15.0",
43-
"typescript": "^3.7.2",
40+
"ts-node": "^8.5.4",
41+
"typedoc": "^0.15.6",
42+
"typescript": "^3.7.4",
4443
"xo": "^0.25.3"
4544
},
4645
"resolutions": {
47-
"eslint": "^6.6.0"
46+
"eslint": "^6.8.0"
4847
},
4948
"xo": {
5049
"extends": "richienb/ts",

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import getCharset from "./utils/get-charset"
2828
import { decode } from "iconv-lite"
2929
import { load as $ } from "cheerio"
30-
import _ from "lodash"
30+
import is from "@sindresorhus/is"
3131

3232
/**
3333
* Detect buffer encoding and convert to target encoding
@@ -38,32 +38,32 @@ import _ from "lodash"
3838
*/
3939
export default function convertBody(content: Buffer | string, headers?: Headers): string {
4040
// Try to extract content-type header
41-
const contentType = !_.isNil(headers) ? headers.get("content-type") : null
41+
const contentType = !is.nullOrUndefined(headers) ? headers.get("content-type") : null
4242

4343
// Resulting charset
4444
let charset: string
4545

4646
// Convert to buffer
47-
if (_.isString(content)) content = Buffer.from(content)
47+
if (is.string(content)) content = Buffer.from(content)
4848

4949
// Header
5050
if (contentType) charset = getCharset(contentType)
5151

5252
// No charset in content type, peek at response body for at most 1024 bytes
53-
const res = _.toString(content.slice(0, 1024))
53+
const res = content.slice(0, 1024).toString()
5454

5555
// HTML5, HTML4 and XML
5656
if (!charset && res) {
5757
charset = getCharset(
5858
$(res)("meta[charset]").attr("charset") || // HTML5
5959
$(res)("meta[http-equiv][content]").attr("content") || // HTML4
60-
$(_.replace(res, /<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding"), // XML
60+
$(res.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding"), // XML
6161
)
6262
}
6363

6464
// Prevent decode issues when sites use incorrect encoding
6565
// ref: https://hsivonen.fi/encoding-menu/
66-
if (charset && _.includes(["gb2312", "gbk"], _.lowerCase(charset))) charset = "gb18030"
66+
if (charset && ["gb2312", "gbk"].includes(charset.toLowerCase())) charset = "gb18030"
6767

6868
// Turn raw buffers into a single utf-8 buffer
6969
return decode(

src/utils/get-charset.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse as parseContentType } from "content-type"
2-
import _ from "lodash"
2+
import is from "@sindresorhus/is"
33
import niceTry from "nice-try"
44

55
/**
@@ -8,9 +8,9 @@ import niceTry from "nice-try"
88
* @private
99
*/
1010
export default function getCharset(contentType: string): string | null {
11-
if (_.isNil(contentType)) return null
11+
if (is.nullOrUndefined(contentType)) return null
1212

1313
const parsed = niceTry(() => parseContentType(contentType))
14-
if (!_.isNil(parsed)) return parsed.parameters.charset
14+
if (!is.nullOrUndefined(parsed)) return parsed.parameters.charset
1515
else return contentType
1616
}

0 commit comments

Comments
 (0)