Skip to content

Commit

Permalink
path: replace assertPath() with validator
Browse files Browse the repository at this point in the history
The path module's assertPath() does exactly what the
validateString() validator does, so this commit updates
path to use validateString() instead. A couple drive by
updates to validateString() outside of assertPath() are
also included.

PR-URL: #24840
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
cjihrig authored and Trott committed Dec 7, 2018
1 parent 4ebb3f3 commit f962f97
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ const {
CHAR_COLON,
CHAR_QUESTION_MARK,
} = require('internal/constants');

function assertPath(path) {
if (typeof path !== 'string') {
throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
}
}
const { validateString } = require('internal/validators');

function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
Expand Down Expand Up @@ -163,7 +158,7 @@ const win32 = {
}
}

assertPath(path);
validateString(path, 'path');

// Skip empty entries
if (path.length === 0) {
Expand Down Expand Up @@ -282,7 +277,7 @@ const win32 = {
},

normalize: function normalize(path) {
assertPath(path);
validateString(path, 'path');
const len = path.length;
if (len === 0)
return '.';
Expand Down Expand Up @@ -401,7 +396,7 @@ const win32 = {


isAbsolute: function isAbsolute(path) {
assertPath(path);
validateString(path, 'path');
const len = path.length;
if (len === 0)
return false;
Expand Down Expand Up @@ -429,7 +424,7 @@ const win32 = {
var firstPart;
for (var i = 0; i < arguments.length; ++i) {
var arg = arguments[i];
assertPath(arg);
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = firstPart = arg;
Expand Down Expand Up @@ -494,8 +489,8 @@ const win32 = {
// to = 'C:\\orandea\\impl\\bbb'
// The output of the function should be: '..\\..\\impl\\bbb'
relative: function relative(from, to) {
assertPath(from);
assertPath(to);
validateString(from, 'from');
validateString(to, 'to');

if (from === to)
return '';
Expand Down Expand Up @@ -648,7 +643,7 @@ const win32 = {
},

dirname: function dirname(path) {
assertPath(path);
validateString(path, 'path');
const len = path.length;
if (len === 0)
return '.';
Expand Down Expand Up @@ -744,9 +739,9 @@ const win32 = {


basename: function basename(path, ext) {
if (ext !== undefined && typeof ext !== 'string')
throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
assertPath(path);
if (ext !== undefined)
validateString(ext, 'ext');
validateString(path, 'path');
var start = 0;
var end = -1;
var matchedSlash = true;
Expand Down Expand Up @@ -832,7 +827,7 @@ const win32 = {


extname: function extname(path) {
assertPath(path);
validateString(path, 'path');
var start = 0;
var startDot = -1;
var startPart = 0;
Expand Down Expand Up @@ -905,7 +900,7 @@ const win32 = {


parse: function parse(path) {
assertPath(path);
validateString(path, 'path');

var ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0)
Expand Down Expand Up @@ -1082,7 +1077,7 @@ const posix = {
path = process.cwd();
}

assertPath(path);
validateString(path, 'path');

// Skip empty entries
if (path.length === 0) {
Expand Down Expand Up @@ -1114,7 +1109,7 @@ const posix = {


normalize: function normalize(path) {
assertPath(path);
validateString(path, 'path');

if (path.length === 0)
return '.';
Expand All @@ -1138,7 +1133,7 @@ const posix = {


isAbsolute: function isAbsolute(path) {
assertPath(path);
validateString(path, 'path');
return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
},

Expand All @@ -1149,7 +1144,7 @@ const posix = {
var joined;
for (var i = 0; i < arguments.length; ++i) {
var arg = arguments[i];
assertPath(arg);
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = arg;
Expand All @@ -1164,8 +1159,8 @@ const posix = {


relative: function relative(from, to) {
assertPath(from);
assertPath(to);
validateString(from, 'from');
validateString(to, 'to');

if (from === to)
return '';
Expand Down Expand Up @@ -1262,7 +1257,7 @@ const posix = {
},

dirname: function dirname(path) {
assertPath(path);
validateString(path, 'path');
if (path.length === 0)
return '.';
const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
Expand All @@ -1289,9 +1284,9 @@ const posix = {


basename: function basename(path, ext) {
if (ext !== undefined && typeof ext !== 'string')
throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
assertPath(path);
if (ext !== undefined)
validateString(ext, 'ext');
validateString(path, 'path');

var start = 0;
var end = -1;
Expand Down Expand Up @@ -1367,7 +1362,7 @@ const posix = {


extname: function extname(path) {
assertPath(path);
validateString(path, 'path');
var startDot = -1;
var startPart = 0;
var end = -1;
Expand Down Expand Up @@ -1428,7 +1423,7 @@ const posix = {


parse: function parse(path) {
assertPath(path);
validateString(path, 'path');

var ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0)
Expand Down

0 comments on commit f962f97

Please sign in to comment.