-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from creative-commoners/pulls/4/php8
DEP PHP 8 support
- Loading branch information
Showing
9 changed files
with
71 additions
and
29 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
export const base64ToByteArray = string => | ||
Uint8Array.from(atob(string), c => c.charCodeAt(0)); | ||
export const base64ToByteArray = string => { | ||
// String replace because server with encode with 'web safe' base64_encode | ||
const b = atob(string.replace(/_/g, '/').replace(/-/g, '+')); | ||
return Uint8Array.from(b, c => c.charCodeAt(0)); | ||
}; | ||
|
||
export const byteArrayToBase64 = byteArray => | ||
btoa(String.fromCharCode(...(new Uint8Array(byteArray)))); | ||
export const byteArrayToBase64 = byteArray => { | ||
// We specifically do not want to make the 'web safe' string replacements above | ||
// doing so will break this functionality | ||
const uarr = new Uint8Array(byteArray); | ||
return btoa(String.fromCharCode(...uarr)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* global jest, describe, it, expect, window */ | ||
|
||
import { base64ToByteArray, byteArrayToBase64 } from '../convert'; | ||
|
||
describe('convert', () => { | ||
it('converts base64ToByteArray', () => { | ||
const string = 'abc/def+ghi'; | ||
const expected = new Uint8Array([105, 183, 63, 117, 231, 254, 130, 24]); | ||
const actual = base64ToByteArray(string); | ||
expect(expected).toEqual(actual); | ||
}); | ||
|
||
it('converts base64ToByteArray when encoded web-safe', () => { | ||
const string = 'abc_def-ghi'; | ||
const expected = new Uint8Array([105, 183, 63, 117, 231, 254, 130, 24]); | ||
const actual = base64ToByteArray(string); | ||
expect(expected).toEqual(actual); | ||
}); | ||
|
||
it('converts byteArrayToBase64', () => { | ||
const byteArray = [105, 183, 63, 117, 231, 254, 130, 24]; | ||
// this is supposed to be slightly different from string | ||
// used in base64ToByteArray tests | ||
const expected = 'abc/def+ghg='; | ||
const actual = byteArrayToBase64(byteArray); | ||
expect(expected).toEqual(actual); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters