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

Method performTranslate

src/Encoder/Replace.js:56–96  ·  view source on GitHub ↗

* Performs encode or decode on given content. * @param {Chain} content * @param {boolean} isEncode True for encoding, false for decoding * @return {number[]|string|Uint8Array|Chain} Resulting content

(content, isEncode)

Source from the content-addressed store, hash-verified

54 * @return {number[]|string|Uint8Array|Chain} Resulting content
55 */
56 performTranslate (content, isEncode) {
57 const caseSensitivity = this.getSettingValue('caseSensitivity')
58
59 // Swap find and replace values if decoding
60 let { find, replace } = this.getSettingValues()
61 if (!isEncode) {
62 ;[find, replace] = [replace, find]
63 }
64
65 // Lowercase search and find text if searching case insensitive
66 let search = content
67 if (!caseSensitivity) {
68 search = search.toLowerCase()
69 find = find.toLowerCase()
70 }
71
72 // Proceed with code point array replacement
73 search = search.getCodePoints()
74 content = content.getCodePoints()
75 find = find.getCodePoints()
76 replace = replace.getCodePoints()
77
78 // Find each occurrence of the `find` value in the search array
79 // This algorithm is similar to `ArrayUtil.replaceSlice` with the
80 // difference of having separate search and content arrays
81 let i = 0
82 let j = -1
83 let result = []
84
85 while ((j = ArrayUtil.indexOfSlice(search, find, j + 1)) !== -1) {
86 // Stich together the content up to the reference and append
87 // the `replace` value
88 result = result.concat(content.slice(i, j))
89 result = result.concat(replace)
90 // Move the cursor behind the last found occurrence
91 i = j + find.length
92 }
93
94 // Append string tail and return the resulting code points
95 return result.concat(content.slice(i))
96 }
97}

Callers

nothing calls this directly

Calls 5

getSettingValueMethod · 0.80
getSettingValuesMethod · 0.80
toLowerCaseMethod · 0.80
getCodePointsMethod · 0.80
indexOfSliceMethod · 0.80

Tested by

no test coverage detected