• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript lodash.defaults类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript lodash.defaultsdeep类代码示例发布时间:2022-05-25
下一篇:
TypeScript validate.validateReturn函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap