猫史档案馆


小苏打_

小苏打_

Lv.1

keep thinking

获赞:53收藏:8浏览:672作品收藏:24

签名:felling fish

回复帖子评论
上一页15 页 / 共 15下一页

【更好的控制台】你是否在为神岛的控制台无法输出完整的对象/列表而烦恼? 中回复

示例用法:

const obj = {
  a: {
    c: {
      f: 123,
      g: [3, 3, null, 4, 5],
      h: 2n**1024n
    },
    i: "A quick brown fox jumps over a lazy dog."
  }
}
Rich.print(obj);

输出效果(注:由于box3控制台会合并所有空格,只能使用一些字符撑起宽度。可以在Rich.config中配置替代字符。):

{
|---a: {
|---|---c: {
|---|---|---f: 123,
|---|---|---g: [3, 3, null, 4, 5],
|---|---|---h: 179769313486231...329624224137216
|---|---},
|---|---i: A quick brown f...ver a lazy dog.
|---}
}


typescript源文件:

interface ConvertHandler {
  condition: (obj: any, depth: number) => boolean;
  handler: (obj: any, depth: number, fn: (obj: any, depth: number) => string) => string;
  cat?: boolean
}

interface RichConfig {
  maxDepth: number;
  maxLength: number;
  placeholder: string;
  tabChar: string;
}

/**
 * 得到更好的控制台输出
 * @example Rich.print(obj)
 * @todo 稀疏数组支持
 */
export abstract class Rich {
  static config: RichConfig = {
    maxDepth: 5,
    maxLength: 30,
    placeholder: '(...)',
    tabChar: '|---'
  };

  static handlers: ConvertHandler[] = [
    {
      condition: obj => obj === null,
      handler: () => 'null',
    },
    {
      condition: obj => obj === undefined,
      handler: () => 'undefined'
    },
    {
      condition: obj => typeof obj === 'string',
      handler: obj => obj,
      cat: true
    },
    {
      condition: obj => typeof obj === 'number'
        || typeof obj === 'bigint'
        || typeof obj === 'boolean'
        || typeof obj === 'symbol',
      handler: obj => obj.toString(),
      cat: true
    },
    {
      condition: (_, depth) => depth >= Rich.config.maxDepth,
      handler: _ => '...'
    },
    {
      condition: obj => obj instanceof Array,
      handler: (obj, depth, fn) => '[' + (obj as Array<any>).map(v => fn(v, depth)).join(', ') + ']'
    },
    {
      condition: obj => obj instanceof Object,
      handler: (obj, depth, fn) => {
        const ctxTab = Rich.config.tabChar.repeat(depth+1);
        const ctx = Object.keys(obj)
          .map(key => `${key}: ${fn(obj[key], depth + 1)}`)
          .join(`,\n${ctxTab}`);
        return `{\n${ctxTab}${ctx}\n${Rich.config.tabChar.repeat(depth)}}`;
      }
    }
  ]

  static getRiched(obj: any, depth: number = 0): string {
    for (const handler of Rich.handlers) {
      if(!handler.condition(obj, depth)){
        continue;
      }
      let ctx = handler.handler(obj, depth, Rich.getRiched);
      if(handler.cat && ctx.length >= Rich.config.maxLength){
        ctx = ctx.slice(0, Rich.config.maxLength / 2) + '...' + ctx.slice(-Rich.config.maxLength / 2);
      }
      return ctx;
    }
    return `${typeof obj}(${obj.string})`;
  }

  static print(obj: any){
    console.log(Rich.getRiched(obj));
  }
}

2024-08-05T13:15:51 点赞:0

[求助]我的node程序出了一些小状况 中回复

\n表示光标下移一行,需要\r\n才能实现正常的换行

2024-08-08T00:21:03 点赞:0

时间胶囊。。。 中回复

时间过得好快

2025-01-01T23:12:40 点赞:1

联机还在怕云变量不够用?云列表速度太慢吗?巅峰之战(已弃作品)教你搭建联机作品! 中回复

经典永流传emotion_doge

2025-03-10T22:25:30 点赞:0