UNPKG

polen

Version:

A framework for delightful GraphQL developer portals

86 lines 2.68 kB
import { objPolicyFilter } from '#lib/kit-temp'; import { Obj } from '@wollybeard/kit'; import { never } from '@wollybeard/kit/language'; /** * Apply mask to data with standard covariance. * * Data must be assignable to the mask's expected type (may have excess properties). * * @param data - The data to mask * @param mask - The mask to apply * @returns The masked data * * @example * ```ts * const user = { name: 'John', email: 'john@example.com', password: 'secret' } * const mask = Mask.pick<User>(['name', 'email']) * const safeUser = apply(user, mask) // { name: 'John', email: 'john@example.com' } * ``` */ export const apply = (data, mask) => { return applyInternal(data, mask); }; /** * Apply mask to partial data. * * Data may have only a subset of the mask's expected properties. * Useful when working with incomplete data or optional fields. * * @param data - The partial data to mask * @param mask - The mask to apply * @returns The masked data * * @example * ```ts * const partialUser = { name: 'John' } // missing email * const mask = Mask.pick<User>(['name', 'email']) * const result = applyPartial(partialUser, mask) // { name: 'John' } * ``` */ export const applyPartial = (data, mask) => { return applyInternal(data, mask); }; /** * Apply mask to data with exact type matching. * * Data must exactly match the mask's expected type - no missing or excess properties. * Provides the strictest type checking. * * @param data - The data to mask (must exactly match expected type) * @param mask - The mask to apply * @returns The masked data * * @example * ```ts * type User = { name: string; email: string } * const mask = Mask.pick<User>(['name']) * * // This works - exact match * const user: User = { name: 'John', email: 'john@example.com' } * const result = applyExact(user, mask) * * // This fails - has extra property * const userWithExtra = { name: 'John', email: 'john@example.com', age: 30 } * const result2 = applyExact(userWithExtra, mask) // Type error! * ``` */ export const applyExact = (data, mask) => { return applyInternal(data, mask); }; // Internal implementation const applyInternal = (data, mask) => { // ━ Handle binary mask if (mask.type === `binary`) { return mask.show ? data : undefined; } // ━ Handle properties mask if (mask.type === `properties`) { // Properties mask requires object data if (!Obj.is(data)) { throw new Error(`Cannot apply properties mask to non-object data`); } return objPolicyFilter(mask.mode, data, mask.properties); } never(); }; //# sourceMappingURL=apply.js.map