UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

87 lines (86 loc) 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStateObjectStringFromComponent = exports.stringifyContextValue = exports.getMemberObjectString = void 0; const DEFAULT_OPTIONS = { format: 'object', keyPrefix: '', valueMapper: (val) => val, onlyValueMapper: false, data: true, functions: true, getters: true, withType: false, }; const convertStateMemberToString = ({ data, format, functions, getters, keyPrefix, valueMapper, withType, onlyValueMapper, }) => ([key, state]) => { const keyValueDelimiter = format === 'object' ? ':' : '='; if (!state) { return undefined; } const { code, typeParameter } = state; const type = withType && typeParameter ? `:${typeParameter}` : ''; switch (state.type) { case 'function': { if (!functions) { return undefined; } const mapper = valueMapper(code, 'function', typeParameter, key); if (onlyValueMapper) { return mapper; } return `${keyPrefix} ${key} ${keyValueDelimiter} ${mapper}`; } case 'method': { if (!functions) { return undefined; } const mapper = valueMapper(code, 'function', typeParameter, key); if (onlyValueMapper) { return mapper; } return `${keyPrefix} ${mapper}`; } case 'getter': { if (!getters) { return undefined; } const mapper = valueMapper(code, 'getter', typeParameter, key); if (onlyValueMapper) { return mapper; } return `${keyPrefix} ${mapper}`; } case 'property': { if (!data) { return undefined; } const mapper = valueMapper(code, 'data', typeParameter, key); if (onlyValueMapper) { return mapper; } return `${keyPrefix} ${key}${type}${keyValueDelimiter} ${mapper}`; } default: break; } }; const getMemberObjectString = (object, userOptions = {}) => { const options = { ...DEFAULT_OPTIONS, ...userOptions }; const lineItemDelimiter = options.format === 'object' ? ',' : '\n'; const stringifiedProperties = Object.entries(object) .map(convertStateMemberToString(options)) .filter((x) => x !== undefined) .join(lineItemDelimiter); const prefix = options.format === 'object' ? '{' : ''; const suffix = options.format === 'object' ? '}' : ''; // NOTE: we add a `lineItemDelimiter` at the very end because other functions will sometimes append more properties. // If the delimiter is a comma and the format is `object`, then we need to make sure we have an extra comma at the end, // or the object will become invalid JS. // We also have to make sure that `stringifiedProperties` isn't empty, or we will get `{,}` which is invalid const extraDelimiter = stringifiedProperties.length > 0 ? lineItemDelimiter : ''; return `${prefix}${stringifiedProperties}${extraDelimiter}${suffix}`; }; exports.getMemberObjectString = getMemberObjectString; const stringifyContextValue = (object, userOptions = {}) => (0, exports.getMemberObjectString)(object, userOptions); exports.stringifyContextValue = stringifyContextValue; const getStateObjectStringFromComponent = (component, options) => (0, exports.getMemberObjectString)(component.state, options); exports.getStateObjectStringFromComponent = getStateObjectStringFromComponent;