-
Notifications
You must be signed in to change notification settings - Fork 180
/
createSchemalize.ts
46 lines (41 loc) · 1.39 KB
/
createSchemalize.ts
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
import type { Name } from '../operations/generalTypes';
import { decamelize } from './decamelize';
import { identity } from './identity';
import { quote } from './quote';
/** @deprecated Use createSchemalize(options) instead. */
export function createSchemalize(
shouldDecamelize: boolean,
shouldQuote: boolean
): (value: Name) => string;
export function createSchemalize(options: {
shouldDecamelize: boolean;
shouldQuote: boolean;
}): (value: Name) => string;
export function createSchemalize(
options: boolean | { shouldDecamelize: boolean; shouldQuote: boolean },
_legacyShouldQuote?: boolean
): (value: Name) => string {
const { shouldDecamelize, shouldQuote } =
typeof options === 'boolean'
? {
shouldDecamelize: options,
shouldQuote: _legacyShouldQuote,
}
: options;
if (typeof options === 'boolean') {
console.warn(
'createSchemalize(shouldDecamelize, shouldQuote) is deprecated. Use createSchemalize({ shouldDecamelize, shouldQuote }) instead.'
);
}
const transform = [
shouldDecamelize ? decamelize : identity,
shouldQuote ? quote : identity,
].reduce((acc, fn) => (fn === identity ? acc : (str) => acc(fn(str))));
return (value) => {
if (typeof value === 'object') {
const { schema, name } = value;
return (schema ? `${transform(schema)}.` : '') + transform(name);
}
return transform(value);
};
}