Skip to content

Commit

Permalink
validateType to validateString
Browse files Browse the repository at this point in the history
  • Loading branch information
timotew committed Feb 16, 2018
1 parent 617be72 commit 57b6148
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,23 @@ Script.prototype.runInNewContext = function(sandbox, options) {
return this.runInContext(context, options);
};

function validateType(prop, propName, type = 'string') {
if (prop !== undefined &&
typeof prop !== type) {
function validateString(prop, propName) {
if (prop !== undefined && typeof prop !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', propName,
type, prop);
}
'string', prop);
}

function getContextOptions(options) {
const contextOptions = options ? {
name: options.contextName,
origin: options.contextOrigin
} : {};
validateType(contextOptions.name, 'options.contextName');
validateType(contextOptions.origin, 'options.contextOrigin');
return contextOptions;
if (options) {
const contextOptions = {
name: options.contextName,
origin: options.contextOrigin
};
validateString(contextOptions.name, 'options.contextName');
validateString(contextOptions.origin, 'options.contextOrigin');
return contextOptions;
}
return {};
}

let defaultContextNameIndex = 1;
Expand All @@ -116,11 +117,11 @@ function createContext(sandbox, options) {
};
if (options.name === undefined) {
options.name = `VM Context ${defaultContextNameIndex++}`;
} else {
validateType(options.name, 'options.name');
} else if (typeof options.name !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.name',
'string', options.name);
}

validateType(options.origin, 'options.origin');
validateString(options.origin, 'options.origin');
} else {
options = {
name: `VM Context ${defaultContextNameIndex++}`
Expand Down

0 comments on commit 57b6148

Please sign in to comment.