telegram-typings icon indicating copy to clipboard operation
telegram-typings copied to clipboard

SOVA-71 Upgrade typings to Bot API 4.4

Open sergeysova opened this issue 6 years ago • 2 comments

Issuehunt badges

https://core.telegram.org/bots/api


IssueHunt Summary

Backers (Total: $5.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

sergeysova avatar Aug 08 '19 18:08 sergeysova

@sergeysova has funded $5.00 to this issue.


issuehunt-oss[bot] avatar Aug 08 '19 18:08 issuehunt-oss[bot]

const nodes = document.getElementsByTagName('h4')
const types = Array.from(nodes).filter((node) => node.innerText.match(/^(?:[A-Z][a-z]+)+$/))
const methods = Array.from(nodes).filter((node) => node.innerText.match(/^[a-z]+(?:[A-Z][a-z]+)+$/))

types.map((el) => {
  let comment = ''
  let props = ''
  let next = el

  while (next.nextElementSibling.tagName === 'P') {
    comment += next.innerText
    next = next.nextElementSibling
  }
  const tableEl = next.nextElementSibling.tagName === 'TABLE' ? next.nextElementSibling : null

  if (tableEl) {
    props = Array.from(tableEl.children[1].children).map((tr) => {
      const arr = Array.from(tr.children)

      return arr.length 
        ? arr[2].innerText.length > 72 
          ? `
  /**${arr[2].innerText.replace(/(.{1,72}\s)\s*?/g, '\n   * $1')}
   */
  ${arr[0].innerText}${arr[2].innerText.startsWith('Optional.') ? '?' : ''}: ${arr[1].innerText.replace(/Array of (\w+)/, '$1[]')},
`
          : `\n  /** ${arr[2].innerText} */\n  ${arr[0].innerText}${arr[2].innerText.startsWith('Optional.') ? '?' : ''}: ${arr[1].innerText.replace(/Array of (\w+)/, '$1[]')},\n`
        : ''
}).join('')
  }

  return `
/**
 * ${comment.replace(/(.{1,72}\s)\s*?/g, '\n * $1')}
 */
export type ${el.innerText} = {${props}}
`}).join('') + '\n\n' +
methods.map((el) => {
  let comment = ''
  let props = ''
  let next = el

  while (next.nextElementSibling.tagName === 'P') {
    comment += next.innerText
    next = next.nextElementSibling
  }
  const tableEl = next.nextElementSibling.tagName === 'TABLE' ? next.nextElementSibling : null

  if (tableEl) {
    props = Array.from(tableEl.children[1].children).map((tr) => {
      const arr = Array.from(tr.children)

      return arr.length 
        ? arr[3].innerText.length > 72 
          ? `
  /**
   * ${arr[3].innerText.replace(/(.{1,72}\s)\s*?/g, '\n   * $1')}
   */
  ${arr[0].innerText}${arr[2].innerText === 'Optional' ? '?' : ''}: ${arr[1].innerText.replace(/Array of (\w+)/, '$1[]')},
`
          : `\n  /** ${arr[3].innerText} */\n  ${arr[0].innerText}${arr[2].innerText === 'Optional' ? '?' : ''}: ${arr[1].innerText.replace(/Array of (\w+)/, '$1[]')},\n`
        : ''
}).join('')
  }

  return `
/**
 * ${comment.replace(/(.{1,72}\s)\s*?/g, '\n * $1')}
 */
export type ${el.innerText} = {${props}}
`}).join('')

Piterden avatar Aug 08 '19 21:08 Piterden