polen
Version:
A framework for delightful GraphQL developer portals
137 lines • 3.61 kB
JavaScript
//
//
//
//
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • Mask
//
//
/**
* Create a mask based on the provided options.
*
* @param options - Mask configuration:
* - `boolean`: Creates a binary mask (true = show, false = hide)
* - `string[]`: Creates a properties mask that allows only the specified keys
* - `object`: Creates a properties mask based on true/false values per key
*
* @returns A mask that can be applied to data
*
* @example
* ```ts
* // Binary mask
* const showAll = create(true)
* const hideAll = create(false)
*
* // Properties mask with array
* const allowMask = create<User>(['name', 'email'])
*
* // Properties mask with object
* const objectMask = create<User>({
* name: true,
* email: true,
* password: false
* })
* ```
*/
export const create = (options) => {
if (typeof options === `boolean`) {
return createBinary(options);
}
// Array input -> PropertiesMask with 'allow' mode
if (Array.isArray(options)) {
return createProperties(`allow`, options);
}
// Object input -> PropertiesMask based on true/false values
const entries = Object.entries(options);
const allowedKeys = entries
.filter(([_, include]) => include === true)
.map(([key]) => key);
const deniedKeys = entries
.filter(([_, include]) => include === false)
.map(([key]) => key);
// If we have denied keys, use deny mode
if (deniedKeys.length > 0 && allowedKeys.length === 0) {
return createProperties(`deny`, deniedKeys);
}
// Default to allow mode with allowed keys
return createProperties(`allow`, allowedKeys);
};
/**
* Create a properties mask.
*
* @param mode - 'allow' to show only specified properties, 'deny' to hide them
* @param properties - Array of property keys to allow or deny
* @returns A PropertiesMask
*/
export const createProperties = (mode, properties) => ({
type: `properties`,
mode,
properties,
});
/**
* Create a binary mask.
*
* @param show - Whether to show (true) or hide (false) the data
* @returns A BinaryMask
*/
export const createBinary = (show) => ({
type: `binary`,
show,
});
//
//
//
//
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • Convenience Constructors with Semantic Names
//
//
/**
* Create a mask that shows all data.
* @returns A BinaryMask with show=true
*/
export const show = () => ({
type: `binary`,
show: true,
});
/**
* Create a mask that hides all data.
* @returns A BinaryMask with show=false
*/
export const hide = () => ({
type: `binary`,
show: false,
});
/**
* Create a mask that shows only the specified properties.
*
* @param properties - Array of property keys to show
* @returns A PropertiesMask in 'allow' mode
*
* @example
* ```ts
* const userMask = pick<User>(['name', 'email'])
* // Only 'name' and 'email' will be shown
* ```
*/
export const pick = (properties) => ({
type: `properties`,
mode: `allow`,
properties,
});
/**
* Create a mask that hides the specified properties.
*
* @param properties - Array of property keys to hide
* @returns A PropertiesMask in 'deny' mode
*
* @example
* ```ts
* const userMask = omit<User>(['password', 'ssn'])
* // Everything except 'password' and 'ssn' will be shown
* ```
*/
export const omit = (properties) => ({
type: `properties`,
mode: `deny`,
properties,
});
//# sourceMappingURL=mask.js.map