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

Method performEncode

src/Encoder/Punycode.js:66–89  ·  view source on GitHub ↗

* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content

(content)

Source from the content-addressed store, hash-verified

64 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
65 */
66 async performEncode (content) {
67 // Punycode is case insensitive, lowercase the content before continuing
68 content = content.toLowerCase()
69
70 // Split domain into labels
71 const labels = content.split('.')
72 const encodedParts = new Array(labels.length)
73
74 // Encode each part separately
75 for (let i = 0; i < labels.length; i++) {
76 // Check if the code points are all basic
77 if (this._nonBasicCodePointIndex(labels[i].getCodePoints()) === -1) {
78 // Return the part as is
79 encodedParts[i] = labels[i]
80 } else {
81 // Encode part using Bootstring and prepend IDNA prefix
82 const result = await this._bootstringEncoder.encode(labels[i])
83 encodedParts[i] = prefix + result.getString()
84 }
85 }
86
87 // Stick domain labels back together
88 return Chain.join(encodedParts, '.')
89 }
90
91 /**
92 * Performs decode on given content.

Callers

nothing calls this directly

Calls 7

toLowerCaseMethod · 0.80
splitMethod · 0.80
getCodePointsMethod · 0.80
encodeMethod · 0.80
getStringMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected