Create algorithm for converting binary data to base64

This commit is contained in:
Luis De Anda
2021-10-08 00:19:00 -05:00
parent 2ae093b97c
commit 281c675d63
2 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { bufferToBase64 } from '../ArrayBufferToBase64'
import { TextEncoder } from 'util'
describe('ArrayBufferToBase64', () => {
it('should encode "Hello, world!" as "SGVsbG8sIHdvcmxkIQ=="', () => {
const testString = 'Hello, world!'
const encoder = new TextEncoder()
const helloWorldBuffer = encoder.encode(testString)
const result = bufferToBase64(helloWorldBuffer)
expect(result).toBe('SGVsbG8sIHdvcmxkIQ==')
})
it('should encode binary buffer [55,23,177,234,68,26,90] as "Nxex6kQaWg=="', () => {
const testBuffer = new Uint8Array([55, 23, 177, 234, 68, 26, 90])
const result = bufferToBase64(testBuffer)
expect(result).toBe('Nxex6kQaWg==')
})
it('should encode binary buffer [0,1,2,3,4,5,6,7,8,9] as "AAECAwQFBgcICQ=="', () => {
const testBuffer = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
const result = bufferToBase64(testBuffer)
expect(result).toBe('AAECAwQFBgcICQ==')
})
})