* Validates plugboard setting value. * @protected * @param {Chain} rawValue * @param {Setting} setting * @return {boolean} Returns true, if value is valid.
(rawValue, setting)
| 810 | * @return {boolean} Returns true, if value is valid. |
| 811 | */ |
| 812 | validatePlugboardValue (rawValue, setting) { |
| 813 | const plugboard = rawValue.getString() |
| 814 | |
| 815 | // Empty plugboard is valid |
| 816 | if (plugboard === '') { |
| 817 | return true |
| 818 | } |
| 819 | |
| 820 | // Check format (ab cd ef) |
| 821 | if (plugboard.match(/^([a-z]{2}\s)*([a-z]{2})$/) === null) { |
| 822 | return { |
| 823 | key: 'enigmaPlugboardInvalidFormat', |
| 824 | message: |
| 825 | 'Invalid plugboard format: pairs of letters to be swapped ' + |
| 826 | 'expected (e.g. \'ab cd ef\')' |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // Check if character pairs are unique |
| 831 | if (!ArrayUtil.isUnique(plugboard.replace(/\s/g, '').split(''))) { |
| 832 | return { |
| 833 | key: 'enigmaPlugboardPairsNotUnique', |
| 834 | message: 'Pairs of letters to be swapped need to be unique' |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | return true |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Converts plugboard value to rotor wiring string. |