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
Allow template strings with all methods (#933)ehmickybefore adoption8074d85
lines 1–29why, one step back1import isPlainObject from 'is-plain-obj';2import {safeNormalizeFileUrl} from './cwd.js';34export const normalizeArguments = (rawFile, rawArgs = [], rawOptions = {}) => {5 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');6 const [args, options] = isPlainObject(rawArgs)7 ? [[], rawArgs]8 : [rawArgs, rawOptions];910 if (!Array.isArray(args)) {11 throw new TypeError(`Second argument must be either an array of arguments or an options object: ${args}`);12 }1314 if (args.some(arg => typeof arg === 'object' && arg !== null)) {15 throw new TypeError(`Second argument must be an array of strings: ${args}`);16 }1718 const normalizedArgs = args.map(String);19 const nullByteArg = normalizedArgs.find(arg => arg.includes('\0'));20 if (nullByteArg !== undefined) {21 throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArg}`);22 }2324 if (!isPlainObject(options)) {25 throw new TypeError(`Last argument must be an options object: ${options}`);26 }2728 return [filePath, normalizedArgs, options];29};