MCPcopy Create free account
hub / github.com/cryptii/cryptii / performTranslate

Method performTranslate

src/Encoder/NumeralSystem.js:90–117  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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.

Callers

nothing calls this directly

Calls 6

toStringMethod · 0.80
getSettingValueMethod · 0.80
indexOfMethod · 0.80
isWhitespaceMethod · 0.80
decodeNumberMethod · 0.80
encodeNumberMethod · 0.80

Tested by

no test coverage detected