Skip to content

Commit e30137c

Browse files
committed
Use type functions.
1 parent 76ea31f commit e30137c

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
7575
}
7676

7777
// Validate input and shift parameters if necessary
78-
if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
78+
if (!isString(data) && !isBuffer(data)) {
7979
throw new TypeError("data should be a string, Buffer or Uint8Array");
8080
}
81-
if (typeof encoding === "function") {
81+
if (isFunction(encoding)) {
8282
callback = encoding;
8383
encoding = null;
8484
}
@@ -107,11 +107,11 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
107107
// Ends the current native request
108108
RedirectableRequest.prototype.end = function (data, encoding, callback) {
109109
// Shift parameters if necessary
110-
if (typeof data === "function") {
110+
if (isFunction(data)) {
111111
callback = data;
112112
data = encoding = null;
113113
}
114-
else if (typeof encoding === "function") {
114+
else if (isFunction(encoding)) {
115115
callback = encoding;
116116
encoding = null;
117117
}
@@ -429,7 +429,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
429429
}
430430

431431
// Evaluate the beforeRedirect callback
432-
if (typeof beforeRedirect === "function") {
432+
if (isFunction(beforeRedirect)) {
433433
var responseDetails = {
434434
headers: response.headers,
435435
statusCode: statusCode,
@@ -476,7 +476,7 @@ function wrap(protocols) {
476476
// Executes a request, following redirects
477477
function request(input, options, callback) {
478478
// Parse parameters
479-
if (typeof input === "string") {
479+
if (isString(input)) {
480480
var urlStr = input;
481481
try {
482482
input = urlToOptions(new URL(urlStr));
@@ -494,7 +494,7 @@ function wrap(protocols) {
494494
options = input;
495495
input = { protocol: protocol };
496496
}
497-
if (typeof options === "function") {
497+
if (isFunction(options)) {
498498
callback = options;
499499
options = null;
500500
}
@@ -593,6 +593,18 @@ function isSubdomain(subdomain, domain) {
593593
return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
594594
}
595595

596+
function isString(value) {
597+
return typeof value === "string" || value instanceof String;
598+
}
599+
600+
function isFunction(value) {
601+
return typeof value === "function";
602+
}
603+
604+
function isBuffer(value) {
605+
return typeof value === "object" && ("length" in value);
606+
}
607+
596608
// Exports
597609
module.exports = wrap({ http: http, https: https });
598610
module.exports.wrap = wrap;

0 commit comments

Comments
 (0)