-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
138 lines (110 loc) · 3.35 KB
/
Copy pathutils.ts
File metadata and controls
138 lines (110 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// deno-lint-ignore-file no-explicit-any
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import { Msg } from "./constants.ts";
import {
filterValues,
isUndefined,
kebabCase,
mapKeys,
mapValues,
} from "./deps.ts";
/** Serialize structured directives into string.
* @throws {Error} If the directives include invalid member.
*/
export function stringifyDirectives(
directives: { readonly [k: string]: readonly string[] | string | undefined },
): string {
const normalized = normalizeDirectives(directives);
if (!Object.keys(normalized).length) {
throw Error(Msg.RequiredDirective);
}
Object.keys(normalized).forEach((input) =>
assertDirectiveName(input, `${Msg.InvalidDirectiveKey} "${input}"`)
);
const values = Object.values(normalized);
values.flat().forEach((input) =>
assertDirectiveValue(input, `${Msg.InvalidVcharWithout} "${input}"`)
);
values.forEach((values) => {
const duplicates = duplicate(values);
if (duplicates.length) {
throw Error(
`${Msg.DuplicatedDirectiveValue} ${duplicates.map(quoted).join(", ")}`,
);
}
});
const joinBySpace = joinBy.bind(null, " ");
return Object.entries(mapValues(normalized, joinBySpace))
.map(stringifyEntry)
.filter(Boolean)
.join("; ");
}
export function normalizeDirectives(
directives: { readonly [k: string]: readonly string[] | string | undefined },
): Record<string, string[]> {
const filtered = filterValues(directives, not(isUndefined)) as Record<
string,
string | string[]
>;
const normalized = mapValues(mapKeys(filtered, kebabCase), ensureArray);
return normalized;
}
function stringifyEntry(entry: readonly [key: string, value: string]): string {
return entry.filter(Boolean).join(" ");
}
function joinBy(
separator: string | undefined,
input: readonly unknown[],
): string {
return input.join(separator);
}
export function not<T extends (...args: any) => boolean>(fn: T) {
return (...args: Parameters<T>): boolean => {
return !fn.apply(null, args);
};
}
export function ensureArray<T>(input: T): T extends readonly any[] ? T : [T] {
return Array.isArray(input) ? input as never : [input] as never;
}
/**
* ```abnf
* directive-name = 1*( ALPHA / DIGIT / "-" )
* ```
*/
const reDirectiveName = /^[a-zA-Z\d-]+$/;
export function isDirectiveName(input: string): boolean {
return reDirectiveName.test(input);
}
function assertDirectiveName(input: string, message?: string): asserts input {
if (!isDirectiveName(input)) {
throw Error(message);
}
}
/**
* ```abnf
* directive-value = *( required-ascii-whitespace / ( %x21-%x2B / %x2D-%x3A / %x3C-%x7E ) )
* ```
*/
const reDirectiveValue = /^[\x21-\x2B\x2D-\x3A\x3C-\x7E]+$/;
export function isDirectiveValue(input: string): boolean {
return reDirectiveValue.test(input);
}
function assertDirectiveValue(input: string, message?: string): asserts input {
if (!isDirectiveValue(input)) {
throw Error(message);
}
}
/** Return duplicated elements. */
export function duplicate<T>(input: readonly T[]): T[] {
const store = new Set<T>();
const duplicated = new Set<T>();
for (const value of input) {
const set = store.has(value) ? duplicated : store;
set.add(value);
}
return [...duplicated];
}
function quoted(input: string): string {
return `"${input}"`;
}