How to use the @teleporthq/teleport-shared.UIDLUtils.cloneObject function in @teleporthq/teleport-shared

To help you get started, we’ve selected a few @teleporthq/teleport-shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github teleporthq / teleport-code-generators / packages / teleport-uidl-validator / src / parser / index.ts View on Github external
export const parseComponentJSON = (
  input: Record,
  params: ParseComponentJSONParams = {}
): ComponentUIDL => {
  const safeInput = params.noClone ? input : UIDLUtils.cloneObject(input)

  const node = safeInput.node as Record
  const result: ComponentUIDL = {
    ...((safeInput as unknown) as ComponentUIDL),
  }

  // other parsers for other sections of the component here
  result.node = parseComponentNode(node) as UIDLElementNode

  return result
}
github teleporthq / teleport-code-generators / packages / teleport-project-generator / src / index.ts View on Github external
throw new Error(contentValidationResult.errorMsg)
    }

    const { components = {}, root } = uidl

    // Based on the routing roles, separate pages into distict UIDLs with their own file names and paths
    const pageUIDLs = createPageUIDLs(uidl, this.strategy)

    // Set the filename and folder path for each component based on the strategy
    prepareComponentOutputOptions(components, this.strategy)

    // Set the local dependency paths based on the relative paths between files
    resolveLocalDependencies(pageUIDLs, components, this.strategy)

    // Initialize output folder and other reusable structures
    const rootFolder = UIDLUtils.cloneObject(template || DEFAULT_TEMPLATE)
    let collectedDependencies: Record = {}

    // If static prefix is not specified, compute it from the path, but if the string is empty it should work
    const assetsPrefix =
      typeof this.strategy.static.prefix === 'string'
        ? this.strategy.static.prefix
        : '/' + this.getAssetsPath().join('/')
    const options: GeneratorOptions = {
      assetsPrefix,
      projectRouteDefinition: root.stateDefinitions.route,
      mapping,
      skipValidation: true,
    }

    // Handling pages
    for (const pageUIDL of pageUIDLs) {
github teleporthq / teleport-code-generators / packages / teleport-uidl-resolver / src / utils.ts View on Github external
export const resolveChildren = (mappedChildren: UIDLNode[], originalChildren: UIDLNode[] = []) => {
  let newChildren = UIDLUtils.cloneObject(mappedChildren)

  let placeholderFound = false
  newChildren.forEach((childNode) => {
    UIDLUtils.traverseNodes(childNode, (node, parentNode) => {
      if (!isPlaceholderNode(node)) {
        return // we're only interested in placeholder nodes
      }

      if (parentNode !== null) {
        if (parentNode.type === 'element') {
          // children nodes can only be added to type 'element'
          // filter out the placeholder node and add the original children instead
          parentNode.content.children = replacePlaceholderNode(
            parentNode.content.children,
            originalChildren
          )
github teleporthq / teleport-code-generators / packages / teleport-uidl-resolver / src / utils.ts View on Github external
})
  }

  if (mappedElement.children) {
    originalElement.children = resolveChildren(mappedElement.children, originalElement.children)

    // Solves an edge case for next.js by passing the styles from the  tag to the <a> tag
    const anchorChild = originalElement.children.find(
      (child) =&gt; child.type === 'element' &amp;&amp; child.content.elementType === 'a'
    ) as UIDLElementNode

    // only do it if there's a child </a><a> tag and the original element is a navlink
    const shouldPassStylesToAnchor =
      originalElement.style &amp;&amp; originalElementType === 'navlink' &amp;&amp; anchorChild
    if (shouldPassStylesToAnchor) {
      anchorChild.content.style = UIDLUtils.cloneObject(originalElement.style)
      originalElement.style = {}
    }
  }
}
</a>
github teleporthq / teleport-code-generators / packages / teleport-uidl-resolver / src / resolver.ts View on Github external
public resolveUIDL(input: ComponentUIDL, options: GeneratorOptions = {}) {
    const mapping = utils.mergeMappings(this.mapping, options.mapping)
    const newOptions = {
      ...options,
      mapping,
    }

    const uidl = UIDLUtils.cloneObject(input)

    UIDLUtils.setFriendlyOutputOptions(uidl)

    utils.checkForIllegalNames(uidl, mapping)

    utils.resolveNode(uidl.node, newOptions)

    const nodesLookup = {}
    utils.createNodesLookup(uidl.node, nodesLookup)
    utils.generateUniqueKeys(uidl.node, nodesLookup)

    utils.ensureDataSourceUniqueness(uidl.node)

    // There might be urls that need to be prefixed in the metaTags of the component
    utils.resolveMetaTags(uidl, options)
github teleporthq / teleport-code-generators / packages / teleport-uidl-resolver / src / resolver.ts View on Github external
public resolveElement(element: UIDLElement, options: GeneratorOptions = {}) {
    const mapping = utils.mergeMappings(this.mapping, options.mapping)

    const newOptions = {
      ...options,
      mapping,
    }
    const returnElement = UIDLUtils.cloneObject(element)
    utils.resolveElement(returnElement, newOptions)
    return returnElement
  }
}
github teleporthq / teleport-code-generators / packages / teleport-uidl-validator / src / parser / index.ts View on Github external
export const parseProjectJSON = (
  input: Record,
  params: ParseProjectJSONParams = {}
): ProjectUIDL =&gt; {
  const safeInput = params.noClone ? input : UIDLUtils.cloneObject(input)
  const root = safeInput.root as Record

  const result = {
    ...((safeInput as unknown) as ProjectUIDL),
  }

  result.root = parseComponentJSON(root, { noClone: true })
  if (result.components) {
    result.components = Object.keys(result.components).reduce(
      (parsedComponnets: Record, key) =&gt; {
        parsedComponnets[key] = parseComponentJSON((result.components[key] as unknown) as Record&lt;
          string,
          unknown
        &gt;)
        return parsedComponnets
      },