normalize.js
This file
Path
lib/arguments/normalize.js
29 lines · 1019 bytes · JavaScript
Last changed by
Allow template strings with all methods (#933)ehmickybefore adoption8074d85
History of this fileTake it
123456789101112131415161718192021222324252627282930import isPlainObject from 'is-plain-obj';import {safeNormalizeFileUrl} from './cwd.js';
export const normalizeArguments = (rawFile, rawArgs = [], rawOptions = {}) => { const filePath = safeNormalizeFileUrl(rawFile, 'First argument'); const [args, options] = isPlainObject(rawArgs) ? [[], rawArgs] : [rawArgs, rawOptions];
if (!Array.isArray(args)) { throw new TypeError(`Second argument must be either an array of arguments or an options object: ${args}`); }
if (args.some(arg => typeof arg === 'object' && arg !== null)) { throw new TypeError(`Second argument must be an array of strings: ${args}`); }
const normalizedArgs = args.map(String); const nullByteArg = normalizedArgs.find(arg => arg.includes('\0')); if (nullByteArg !== undefined) { throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArg}`); }
if (!isPlainObject(options)) { throw new TypeError(`Last argument must be an options object: ${options}`); }
return [filePath, normalizedArgs, options];};