UNPKG

@oada/oadaify

Version:

Make OADA data nicer to work with in JS/TS

84 lines 2.69 kB
/** * @license * Copyright 2022 Alex Layton * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. */ import type { Except } from 'type-fest'; /** * Symbol to access OADA `_id` key */ export declare const _id: unique symbol; /** * Symbol to access OADA `_rev` key */ export declare const _rev: unique symbol; /** * Symbol to access OADA `_type` key */ export declare const _type: unique symbol; /** * Symbol to access OADA `_meta` key */ export declare const _meta: unique symbol; /** * @todo just declare symbols in here if TS stops being dumb about symbol keys */ declare const Symbols: { readonly _id: typeof _id; readonly _rev: typeof _rev; readonly _type: typeof _type; readonly _meta: typeof _meta; }; export declare type JsonObject = { [Key in string]?: JsonValue; }; export declare type JsonArray = JsonValue[] | readonly JsonValue[]; export declare type JsonValue = string | number | boolean | null | JsonObject | JsonArray; export declare type OADAified<T> = T extends JsonValue ? OADAifiedJsonValue<T> : T; declare type OADAifyKey<T, K> = K extends keyof T ? T[K] : never; /** * @todo Better name */ export declare type OADAifiedJsonObject<T extends JsonObject = JsonObject> = { [_id]: OADAifyKey<T, '_id'>; [_rev]: OADAifyKey<T, '_rev'>; [_type]: OADAifyKey<T, '_type'>; /** * @todo OADAify under _meta or not? */ [_meta]: OADAified<T['_meta']>; } & { [K in keyof Except<T, keyof typeof Symbols>]: OADAified<T[K]>; }; /** * @todo Better name */ export declare type OADAifiedJsonArray<T extends JsonArray = JsonArray> = Array<OADAifiedJsonValue<T extends Array<infer R> ? R : T extends ReadonlyArray<infer R> ? R : never>>; /** * @todo Better name */ export declare type OADAifiedJsonValue<T extends JsonValue = JsonValue> = T extends JsonArray ? OADAifiedJsonArray<T> : T extends JsonObject ? OADAifiedJsonObject<T> : T; /** * @todo why doesn't TS figure this correctly with for ... in? */ export declare type StringKey<T extends JsonObject> = keyof T & string; /** * Converts OADA keys (i.e., ones starting with `_`) to Symbols * * This way when looping etc. you only get actual data keys. * _Should_ turn itself back to original JSON for stringify, ajv, etc. */ export declare function oadaify<T extends JsonValue>(value: T, deep?: boolean): OADAified<T>; /** * Inverse of oadaify * * Makes OADA keys normal object properties again. * * @see oadaify */ export declare function deoadaify<T extends JsonValue>(value: OADAified<T>): T; export default oadaify; //# sourceMappingURL=index.d.ts.map