2727import getCharset from "./utils/get-charset"
2828import { decode } from "iconv-lite"
2929import { 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*/
3939export 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 (
0 commit comments