(arr, textTpl='{text}', valueTpl='{id}')
| 34 | } |
| 35 | |
| 36 | export function toTextValue (arr, textTpl='{text}', valueTpl='{id}') { |
| 37 | if (!arr) { |
| 38 | return []; |
| 39 | } |
| 40 | if (!Array.isArray(arr)) { |
| 41 | arr = Object.keys(arr).map((key) => { |
| 42 | return { |
| 43 | id: key, |
| 44 | text: arr[key] |
| 45 | }; |
| 46 | }); |
| 47 | } |
| 48 | arr = arr.map(function (s) { |
| 49 | if (typeof s !== 'object') { |
| 50 | s = s.toString(); |
| 51 | return { $text: s, $value: s, $key: hashcode(s) }; |
| 52 | } else { |
| 53 | s.$text = substitute(textTpl, s); |
| 54 | s.$value = substitute(valueTpl, s); |
| 55 | s.$key = s.id ? s.id : hashcode(`${s.$text}-${s.$value}`); |
| 56 | return s; |
| 57 | } |
| 58 | }); |
| 59 | return arr; |
| 60 | } |
| 61 | |
| 62 | export function hashcode(obj) { |
| 63 | let hash = 0, i, chr, len, str; |
no test coverage detected
searching dependent graphs…