UNPKG

@sketch-hq/sketch-assistant-utils

Version:

Utility functions and types for Sketch Assistants.

124 lines (123 loc) 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.helpers = exports.buildRuleOptionSchema = void 0; const sketch_assistant_types_1 = require("@sketch-hq/sketch-assistant-types"); class ReservedRuleOptionNameError extends Error { constructor(optionName) { super(`Error creating rule option schema, option name "${optionName}" is reserved`); this.optionName = optionName; this.name = 'ReservedRuleOptionNameError'; Object.setPrototypeOf(this, new.target.prototype); } } function assertOptionNameNotReserved(optionName) { if (Object.values(sketch_assistant_types_1.ReservedRuleOptionNames).includes(optionName)) { throw new ReservedRuleOptionNameError(optionName); } } /** * Combine multiple rule option schemas into one. We treat _all_ custom options * as required. */ const buildRuleOptionSchema = (ops) => ({ type: 'object', properties: ops.reduce((acc, props) => (Object.assign(Object.assign({}, acc), props)), {}), required: ops.map((props) => Object.keys(props)).reduce((acc, val) => acc.concat(val), []), // flatten }); exports.buildRuleOptionSchema = buildRuleOptionSchema; /** * Create a floating point number option. */ const numberOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: Object.assign(Object.assign({ type: 'number', title: ops.title, description: ops.description, default: typeof ops.defaultValue === 'number' ? ops.defaultValue : 0 }, (typeof ops.minimum === 'number' ? { minimum: ops.minimum } : {})), (typeof ops.maximum === 'number' ? { maximum: ops.maximum } : {})), }; }; /** * Create an integer option. */ const integerOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: Object.assign(Object.assign({ type: 'integer', title: ops.title, description: ops.description, default: typeof ops.defaultValue === 'number' ? ops.defaultValue : 0 }, (typeof ops.minimum === 'number' ? { minimum: ops.minimum } : {})), (typeof ops.maximum === 'number' ? { maximum: ops.maximum } : {})), }; }; /** * Create a string option. */ const stringOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: Object.assign(Object.assign(Object.assign({ type: 'string', title: ops.title, description: ops.description, default: typeof ops.defaultValue === 'string' ? ops.defaultValue : '' }, (typeof ops.minLength === 'number' ? { minLength: ops.minLength } : {})), (typeof ops.maxLength === 'number' ? { maxLength: ops.maxLength } : {})), (typeof ops.pattern === 'string' ? { pattern: ops.pattern } : {})), }; }; /** * Create a boolean option. */ const booleanOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: { type: 'boolean', title: ops.title, description: ops.description, default: typeof ops.defaultValue === 'boolean' ? ops.defaultValue : false, }, }; }; /** * Create a string option limited to a set of values. */ const stringEnumOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: { type: 'string', title: ops.title, description: ops.description, default: typeof ops.defaultValue === 'string' ? ops.defaultValue : ops.values[0], enum: ops.values, enumDescriptions: ops.valueTitles, }, }; }; /** * Create a string list option. */ const stringArrayOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: { type: 'array', title: ops.title, description: ops.description, default: Array.isArray(ops.defaultValue) ? ops.defaultValue : [''], items: Object.assign(Object.assign(Object.assign({ type: 'string' }, (typeof ops.minLength === 'number' ? { minLength: ops.minLength } : {})), (typeof ops.maxLength === 'number' ? { maxLength: ops.maxLength } : {})), (typeof ops.pattern === 'string' ? { pattern: ops.pattern } : {})), }, }; }; /** * Create an object list option. */ const objectArrayOption = (ops) => { assertOptionNameNotReserved(ops.name); return { [ops.name]: { type: 'array', title: ops.title, description: ops.description, items: Object.assign(Object.assign({ type: 'object', properties: buildRuleOptionSchema(ops.props).properties }, (typeof ops.minLength === 'number' ? { minLength: ops.minLength } : {})), (typeof ops.maxLength === 'number' ? { maxLength: ops.maxLength } : {})), }, }; }; const helpers = { numberOption, integerOption, stringOption, booleanOption, stringEnumOption, stringArrayOption, objectArrayOption, }; exports.helpers = helpers;