UNPKG

3.57 kBTypeScriptView Raw
1/**
2 * A utility for converting snake_case to camelCase.
3 *
4 * For, for example `my_snake_string` becomes `mySnakeString`.
5 *
6 * @internal
7 */
8export type SnakeToCamel<S> = S extends `${infer FirstWord}_${infer Remainder}` ? `${FirstWord}${Capitalize<SnakeToCamel<Remainder>>}` : S;
9/**
10 * A utility for converting an type's keys from snake_case
11 * to camelCase, if the keys are strings.
12 *
13 * For example:
14 *
15 * ```ts
16 * {
17 * my_snake_string: boolean;
18 * myCamelString: string;
19 * my_snake_obj: {
20 * my_snake_obj_string: string;
21 * };
22 * }
23 * ```
24 *
25 * becomes:
26 *
27 * ```ts
28 * {
29 * mySnakeString: boolean;
30 * myCamelString: string;
31 * mySnakeObj: {
32 * mySnakeObjString: string;
33 * }
34 * }
35 * ```
36 *
37 * @remarks
38 *
39 * The generated documentation for the camelCase'd properties won't be available
40 * until {@link https://github.com/microsoft/TypeScript/issues/50715} has been
41 * resolved.
42 *
43 * @internal
44 */
45export type SnakeToCamelObject<T> = {
46 [K in keyof T as SnakeToCamel<K>]: T[K] extends {} ? SnakeToCamelObject<T[K]> : T[K];
47};
48/**
49 * A utility for adding camelCase versions of a type's snake_case keys, if the
50 * keys are strings, preserving any existing keys.
51 *
52 * For example:
53 *
54 * ```ts
55 * {
56 * my_snake_boolean: boolean;
57 * myCamelString: string;
58 * my_snake_obj: {
59 * my_snake_obj_string: string;
60 * };
61 * }
62 * ```
63 *
64 * becomes:
65 *
66 * ```ts
67 * {
68 * my_snake_boolean: boolean;
69 * mySnakeBoolean: boolean;
70 * myCamelString: string;
71 * my_snake_obj: {
72 * my_snake_obj_string: string;
73 * };
74 * mySnakeObj: {
75 * mySnakeObjString: string;
76 * }
77 * }
78 * ```
79 * @remarks
80 *
81 * The generated documentation for the camelCase'd properties won't be available
82 * until {@link https://github.com/microsoft/TypeScript/issues/50715} has been
83 * resolved.
84 *
85 * Tracking: {@link https://github.com/googleapis/google-auth-library-nodejs/issues/1686}
86 *
87 * @internal
88 */
89export type OriginalAndCamel<T> = {
90 [K in keyof T as K | SnakeToCamel<K>]: T[K] extends {} ? OriginalAndCamel<T[K]> : T[K];
91};
92/**
93 * Returns the camel case of a provided string.
94 *
95 * @remarks
96 *
97 * Match any `_` and not `_` pair, then return the uppercase of the not `_`
98 * character.
99 *
100 * @internal
101 *
102 * @param str the string to convert
103 * @returns the camelCase'd string
104 */
105export declare function snakeToCamel<T extends string>(str: T): SnakeToCamel<T>;
106/**
107 * Get the value of `obj[key]` or `obj[camelCaseKey]`, with a preference
108 * for original, non-camelCase key.
109 *
110 * @param obj object to lookup a value in
111 * @returns a `get` function for getting `obj[key || snakeKey]`, if available
112 */
113export declare function originalOrCamelOptions<T extends {}>(obj?: T): {
114 get: <K extends keyof OriginalAndCamel<T> & string>(key: K) => OriginalAndCamel<T>[K];
115};
116export interface LRUCacheOptions {
117 /**
118 * The maximum number of items to cache.
119 */
120 capacity: number;
121 /**
122 * An optional max age for items in milliseconds.
123 */
124 maxAge?: number;
125}
126/**
127 * A simple LRU cache utility.
128 * Not meant for external usage.
129 *
130 * @experimental
131 * @internal
132 */
133export declare class LRUCache<T> {
134 #private;
135 readonly capacity: number;
136 maxAge?: number;
137 constructor(options: LRUCacheOptions);
138 /**
139 * Add an item to the cache.
140 *
141 * @param key the key to upsert
142 * @param value the value of the key
143 */
144 set(key: string, value: T): void;
145 /**
146 * Get an item from the cache.
147 *
148 * @param key the key to retrieve
149 */
150 get(key: string): T | undefined;
151}