MCPcopy Create free account
hub / github.com/contentlayerdev/contentlayer / encode

Function encode

packages/@contentlayer/utils/src/base64.ts:77–103  ·  view source on GitHub ↗
(data: ArrayBuffer | string)

Source from the content-addressed store, hash-verified

75 * @param data
76 */
77export const encode = (data: ArrayBuffer | string): string => {
78 const uint8 =
79 typeof data === 'string' ? new TextEncoder().encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data)
80 let result = '',
81 i
82 const l = uint8.length
83 for (i = 2; i < l; i += 3) {
84 result += base64abc[uint8[i - 2]! >> 2]
85 result += base64abc[((uint8[i - 2]! & 0x03) << 4) | (uint8[i - 1]! >> 4)]
86 result += base64abc[((uint8[i - 1]! & 0x0f) << 2) | (uint8[i]! >> 6)]
87 result += base64abc[uint8[i]! & 0x3f]
88 }
89 if (i === l + 1) {
90 // 1 octet yet to write
91 result += base64abc[uint8[i - 2]! >> 2]
92 result += base64abc[(uint8[i - 2]! & 0x03) << 4]
93 result += '=='
94 }
95 if (i === l) {
96 // 2 octets yet to write
97 result += base64abc[uint8[i - 2]! >> 2]
98 result += base64abc[((uint8[i - 2]! & 0x03) << 4) | (uint8[i - 1]! >> 4)]
99 result += base64abc[(uint8[i - 1]! & 0x0f) << 2]
100 result += '='
101 }
102 return result
103}
104
105/**
106 * Decodes a given RFC4648 base64 encoded string

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…