DictConverter.js 597 B

1234567891011121314151617
  1. import DictOptions from './DictOptions'
  2. import DictData from './DictData'
  3. export default function(dict, dictMeta) {
  4. const label = determineDictField(dict, dictMeta.labelField, ...DictOptions.DEFAULT_LABEL_FIELDS)
  5. const value = determineDictField(dict, dictMeta.valueField, ...DictOptions.DEFAULT_VALUE_FIELDS)
  6. return new DictData(dict[label], dict[value], dict)
  7. }
  8. /**
  9. * 确定字典字段
  10. * @param {DictData} dict
  11. * @param {...String} fields
  12. */
  13. function determineDictField(dict, ...fields) {
  14. return fields.find(f => Object.prototype.hasOwnProperty.call(dict, f))
  15. }