lib/argsert.ts

101 LOC · 101 covered · 0 uncovered · 26 ranges · 1146 concepts · 18 introducers · 803 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > import {YError} from './yerror.js'; argsert.ts ×3
2 > import {parseCommand, ParsedCommand} from './parse-command.js';
3 >
4 > const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
5 > export function argsert(callerArguments: any[], length?: number): void;
6 > export function argsert(
7 > expected: string,
8 > callerArguments: any[],
9 > length?: number
10 > ): void;
11 > export function argsert(
12 > arg1: string | any[], argsert.ts ×4
13 > arg2?: any[] | number,
14 > arg3?: number
15 > ): void {
16 > function parseArgs(): [
17 > Pick<ParsedCommand, 'demanded' | 'optional'>,
18 > any[],
19 > number?,
20 > ] {
21 > return typeof arg1 === 'object'
22 > ? [{demanded: [], optional: []}, arg1, arg2 as number | undefined]
23 > : [
24 > parseCommand(`cmd ${arg1}`), parse-command.ts ×3
25 > arg2 as any[],
26 > arg3 as number | undefined,
27 > ];
29 > // TODO: should this eventually raise an exception.
30 > try {
31 > // preface the argument description with "cmd", so
32 > // that we can run it through yargs' command parser.
33 > let position = 0;
34 > const [parsed, callerArguments, _length] = parseArgs();
35 > const args = [].slice.call(callerArguments);
36 >
37 > while (args.length && args[args.length - 1] === undefined) args.pop();
38 > const length = _length || args.length;
39 >
40 > if (length < parsed.demanded.length) {
41 > throw new YError( argsert.ts ×1
42 > `Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`
43 > );
44 > }
46 > const totalCommands = parsed.demanded.length + parsed.optional.length;
47 > if (length > totalCommands) {
48 > throw new YError( argsert.ts ×1
49 > `Too many arguments provided. Expected max ${totalCommands} but received ${length}.`
50 > );
51 > }
53 > parsed.demanded.forEach(demanded => {
54 > const arg = args.shift(); argsert.ts ×1
55 > const observedType = guessType(arg);
56 > const matchingTypes = demanded.cmd.filter(
57 > type => type === observedType || type === '*'
58 > );
59 > if (matchingTypes.length === 0)
60 > argumentTypeError(observedType, demanded.cmd, position);
61 > position += 1; argsert.ts ×1
62 > }); argsert.ts ×3
63 >
64 > parsed.optional.forEach(optional => {
65 > if (args.length === 0) return; argsert.ts ×1
66 > const arg = args.shift(); argsert.ts ×1
67 > const observedType = guessType(arg);
68 > const matchingTypes = optional.cmd.filter(
69 > type => type === observedType || type === '*'
70 > );
71 > if (matchingTypes.length === 0)
72 > argumentTypeError(observedType, optional.cmd, position);
73 > position += 1; argsert.ts ×1
74 > }); argsert.ts ×3
75 > } catch (err) { argsert.ts ×4
76 > console.warn((err as Error).stack); argsert.ts ×1
77 > }
80 > function guessType(arg: any) { argsert.ts ×2
81 > if (Array.isArray(arg)) {
82 > return 'array'; argsert.ts ×1
83 > } else if (arg === null) { argsert.ts ×2
84 > return 'null'; argsert.ts ×1
85 > }
86 > return typeof arg; argsert.ts ×1
87 > }
89 > function argumentTypeError( argsert.ts ×1
90 > observedType: string,
91 > allowedTypes: string[],
92 > position: number
93 > ) {
94 > throw new YError(
95 > `Invalid ${
96 > positionName[position] || 'manyith'
97 > } argument. Expected ${allowedTypes.join(
98 > ' or '
99 > )} but received ${observedType}.`
100 > );
101 > }