* Performs encode or decode on given content. * @protected * @param {Chain} content * @param {boolean} isEncode True for encoding, false for decoding * @return {number[]|string|Uint8Array|Chain|Promise} Resulting content
(content, isEncode)
| 88 | * @return {number[]|string|Uint8Array|Chain|Promise} Resulting content |
| 89 | */ |
| 90 | performTranslate (content, isEncode) { |
| 91 | const string = content.toString() |
| 92 | const from = this.getSettingValue(isEncode ? 'from' : 'to') |
| 93 | const to = this.getSettingValue(isEncode ? 'to' : 'from') |
| 94 | |
| 95 | const pattern = systemPatterns[systemNames.indexOf(from)] |
| 96 | |
| 97 | // Find numbers using pattern |
| 98 | const result = string.replace(pattern, (match, rawNumber, offset) => { |
| 99 | const alone = |
| 100 | (offset === 0 || StringUtil.isWhitespace(string, offset - 1)) && |
| 101 | (string.length === offset + rawNumber.length || |
| 102 | StringUtil.isWhitespace(string, offset + rawNumber.length)) |
| 103 | |
| 104 | if (!alone) { |
| 105 | // Ignore numbers having adjacent characters |
| 106 | return rawNumber |
| 107 | } |
| 108 | |
| 109 | const decimal = NumeralSystemEncoder.decodeNumber(from, rawNumber) |
| 110 | const encodedValue = decimal !== null |
| 111 | ? NumeralSystemEncoder.encodeNumber(to, decimal) |
| 112 | : null |
| 113 | return encodedValue || rawNumber |
| 114 | }) |
| 115 | |
| 116 | return result |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Translates number from given system to decimal. |
nothing calls this directly
no test coverage detected