当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript lodash.defaults类代码示例

本文整理汇总了TypeScript中lodash.defaults的典型用法代码示例。如果您正苦于以下问题:TypeScript defaults类的具体用法?TypeScript defaults怎么用?TypeScript defaults使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了defaults类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1:

 .then((validIntents: Array<Intent>) => {
   if (this.debugOn) { console.log('validIntents', util.inspect(validIntents, { depth: null })); };
   if (validIntents.length === 0) {
     const unknownIntent: Intent = { action: 'none', topic: null };
     return unknownIntent;
   }
   const mergedDetails = _.defaults.apply(this, validIntents.map(intent => intent.details));
   const firstIntent = validIntents[0];
   firstIntent.details = mergedDetails;
   if (this.debugOn) { console.log(firstIntent); };
   return firstIntent;
 });
开发者ID:LexieCore,项目名称:bot-framework,代码行数:12,代码来源:index.ts

示例2: render

  function render() {
    let columns: Array<TableColumn<T>> =
      options!.columns || (Object.keys((data[0] as any) || {}) as any)

    if (typeof columns[0] === 'string') {
      columns = (columns as any).map(key => ({ key }))
    }

    let defaultsApplied = false
    for (const row of data) {
      row.height = 1
      for (const col of columns) {
        if (!defaultsApplied) {
          defaults(col, colDefaults)
        }

        const cell = col.get(row)

        col.width = Math.max(
          result(col, 'label').length,
          col.width || 0,
          calcWidth(cell),
        )

        row.height = Math.max(row.height || 0, cell.split(/[\r\n]+/).length)
      }
      defaultsApplied = true
    }

    if (options!.printHeader) {
      options!.printHeader!(
        columns.map(col => {
          const label = result(col, 'label')
          return pad(label, col.width || label.length)
        }),
      )
    }

    function getNthLineOfCell(n, row, col) {
      // TODO memoize this
      const lines = col.get(row).split(/[\r\n]+/)
      return pad(lines[n] || '', col.width)
    }

    for (const row of data) {
      for (let i = 0; i < (row.height || 0); i++) {
        const cells = columns.map(partial(getNthLineOfCell, i, row))
        options!.printRow!(cells)
      }
      options!.after!(row, options!)
    }
  }
开发者ID:dhruvcodeword,项目名称:prisma,代码行数:52,代码来源:table.ts

示例3: require

/**
 * Generates a Unicode table and feeds it into configured printer.
 *
 * Top-level arguments:
 *
 * @arg {Object[]} data - the records to format as a table.
 * @arg {Object} options - configuration for the table.
 *
 * @arg {Object[]} [options.columns] - Options for formatting and finding values for table columns.
 * @arg {function(string)} [options.headerAnsi] - Zero-width formattter for entire header.
 * @arg {string} [options.colSep] - Separator between columns.
 * @arg {function(row, options)} [options.after] - Function called after each row is printed.
 * @arg {function(string)} [options.printLine] - Function responsible for printing to terminal.
 * @arg {function(cells)} [options.printHeader] - Function to print header cells as a row.
 * @arg {function(cells)} [options.printRow] - Function to print cells as a row.
 *
 * @arg {function(row)|string} [options.columns[].key] - Path to the value in the row or function to retrieve the pre-formatted value for the cell.
 * @arg {function(string)} [options.columns[].label] - Header name for column.
 * @arg {function(string, row)} [options.columns[].format] - Formatter function for column value.
 * @arg {function(row)} [options.columns[].get] - Function to return a value to be presented in cell without formatting.
 *
 */
function table<T = { height?: number }>(
  out: Output,
  data: any[],
  options: TableOptions<T> = {},
) {
  const ary = require('lodash.ary')
  const defaults = require('lodash.defaults')
  const get = require('lodash.get')
  const identity = require('lodash.identity')
  const partial = require('lodash.partial')
  const property = require('lodash.property')
  const result = require('lodash.result')

  const defaultOptions = {
    colSep: '  ',
    after: () => {
      // noop
    },
    headerAnsi: identity,
    printLine: s => out.log(s),
    printRow(cells) {
      this.printLine(cells.join(this.colSep).trimRight())
    },
    printHeader(cells) {
      this.printRow(cells.map(ary(this.headerAnsi, 1)))
      this.printRow(cells.map(hdr => hdr.replace(/./g, '─')))
    },
  }

  const colDefaults = {
    format: value => (value ? value.toString() : ''),
    width: 0,
    label() {
      return this.key.toString()
    },

    get(row) {
      const path = result(this, 'key')
      const value = !path ? row : get(row, path)
      return this.format(value, row)
    },
  }

  function calcWidth(cell) {
    const lines = stripAnsi(cell).split(/[\r\n]+/)
    const lineLengths = lines.map(property('length'))
    return Math.max.apply(Math, lineLengths)
  }

  function pad(str: string, length: number) {
    const visibleLength = stripAnsi(str).length
    const diff = length - visibleLength

    return str + ' '.repeat(Math.max(0, diff))
  }

  function render() {
    let columns: Array<TableColumn<T>> =
      options!.columns || (Object.keys((data[0] as any) || {}) as any)

    if (typeof columns[0] === 'string') {
      columns = (columns as any).map(key => ({ key }))
    }

    let defaultsApplied = false
    for (const row of data) {
      row.height = 1
      for (const col of columns) {
        if (!defaultsApplied) {
          defaults(col, colDefaults)
        }

        const cell = col.get(row)

        col.width = Math.max(
          result(col, 'label').length,
          col.width || 0,
          calcWidth(cell),
//.........这里部分代码省略.........
开发者ID:dhruvcodeword,项目名称:prisma,代码行数:101,代码来源:table.ts


注:本文中的lodash.defaults类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。