如何在下面的 value: _value(this.from, this.to, line) 行中获取 DRY?

How to get DRY on the lines `value: _value(this.from, this.to, line)` below?

函数 getRegistry() 创建一个具有多个属性的 Registry 对象,这些属性在 JavaScript 中共享相同的结构:

'use strict'

function getRegistry(line) {
  return {
    systemId: {
      from: 1, to: 3,
      name: 'system_id',
      value: _value(this.from, this.to, line)
    },
    originUnity: {
      from: 4, to: 7,
      name: 'origin_unity',
      value: _value(this.from, this.to, line)
    },
    // ... similar property definition as above
  }
}

function _value (from, to, line) {
  return line.substring(from - 1, to)
}

module.exports = getRegistry

如何更改 value: _value(this.from, this.to, line) 行中的 属性 定义以消除代码重复?

代码在我看来并不马虎......但如果你真的想尽可能地提高代码效率,这是我看到的唯一改进:

   function getRegistry(line) {
      return {
        systemId: {
          from: 1, to: 3,
          name: 'system_id',
          value: _value(this, line)
        },
        originUnity: {
          from: 4, to: 7,
          name: 'origin_unity',
          value: _value(this, line)
        },
        // ... similar property definition as above
      }
    }

    function _value (obj, line) {
      return line.substring(obj.from - 1, obj.to)
    }