Skip to content
Myr

parameters.js

This file

Path

lib/methods/parameters.js

31 lines · 1.3 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–3why, one step back
1import isPlainObject from 'is-plain-obj';
2import {safeNormalizeFileUrl} from '../arguments/file-url.js';3

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 4–5why, one step back
4// The command `arguments` and `options` are both optional.
5// This also does basic validation on them and on the command file.

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 6why, one step back
6export const normalizeParameters = (rawFile, rawArguments = [], rawOptions = {}) => {

Rename files (#977)ehmickybefore adoption4b2e316

line 7why, one step back
7 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 8–10why, one step back
8 const [commandArguments, options] = isPlainObject(rawArguments)
9 ? [[], rawArguments]10 : [rawArguments, rawOptions];

Rename files (#977)ehmickybefore adoption4b2e316

line 11why, one step back
11

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 12–13why, one step back
12 if (!Array.isArray(commandArguments)) {
13 throw new TypeError(`Second argument must be either an array of arguments or an options object: ${commandArguments}`);

Rename files (#977)ehmickybefore adoption4b2e316

lines 14–15why, one step back
14 }
15

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 16–17why, one step back
16 if (commandArguments.some(commandArgument => typeof commandArgument === 'object' && commandArgument !== null)) {
17 throw new TypeError(`Second argument must be an array of strings: ${commandArguments}`);

Rename files (#977)ehmickybefore adoption4b2e316

lines 18–19why, one step back
18 }
19

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 20–23why, one step back
20 const normalizedArguments = commandArguments.map(String);
21 const nullByteArgument = normalizedArguments.find(normalizedArgument => normalizedArgument.includes('\0'));22 if (nullByteArgument !== undefined) {23 throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArgument}`);

Rename files (#977)ehmickybefore adoption4b2e316

lines 24–29why, one step back
24 }
2526 if (!isPlainObject(options)) {27 throw new TypeError(`Last argument must be an options object: ${options}`);28 }29

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 30why, one step back
30 return [filePath, normalizedArguments, options];

Rename files (#977)ehmickybefore adoption4b2e316

line 31why, one step back
31};