forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement(Share): Enhancement base64 encoding/decoding using UTF-8 …
…charset to support the characters outside the latin1 range. (rjsf-team#4024) (rjsf-team#4034) * Enhancement(Share): Declare and export an object that provides base64 encoding and decoding functions using the utf-8 charset to support the characters outside the latin1 range. (rjsf-team#4024) * Enhancement(Share): Add the 'base64.test.ts' to test the base64. (rjsf-team#4024) * Enhancement(Share): Update the base64 reference in 'Playground' to the new customized base64 in 'utils' (rjsf-team#4024). * Enhancement(Share): Update 'CHANGELOG.md' (rjsf-team#4024). * Enhancement(Share): Update 'CHANGELOG.md' (rjsf-team#4024). * Enhancement(Share): Add test to test the platform behavior (rjsf-team#4024). * Enhancement: Fix comments and updating utility-functions.md to add introduction of base64 object * Update CHANGELOG.md * Update CHANGELOG.md * Update base64.ts
- Loading branch information
1 parent
4f8d080
commit f31bef1
Showing
7 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* An object that provides base64 encoding and decoding functions using the utf-8 charset to support the characters outside the latin1 range | ||
* By default, btoa() and atob() only support the latin1 character range. | ||
*/ | ||
const base64 = (function () { | ||
// If we are in the browser, we can use the built-in TextEncoder and TextDecoder | ||
// Otherwise, it is assumed that we are in node.js, and we can use the util module's TextEncoder and TextDecoder | ||
return { | ||
encode(text: string): string { | ||
let encoder: any; | ||
if (typeof TextEncoder !== 'undefined') { | ||
encoder = new TextEncoder(); | ||
} else { | ||
const { TextEncoder } = require('util'); | ||
encoder = new TextEncoder(); | ||
} | ||
return btoa(String.fromCharCode(...encoder.encode(text))); | ||
}, | ||
decode(text: string): string { | ||
let decoder: any; | ||
if (typeof TextDecoder !== 'undefined') { | ||
decoder = new TextDecoder(); | ||
} else { | ||
const { TextDecoder } = require('util'); | ||
decoder = new TextDecoder(); | ||
} | ||
return decoder.decode(Uint8Array.from(atob(text), (c) => c.charCodeAt(0))); | ||
}, | ||
}; | ||
})(); | ||
|
||
export default base64; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { base64 } from '../src'; | ||
|
||
describe('base64', () => { | ||
it('should successfully encode a ascii character', () => { | ||
expect(base64.encode('v')).toEqual('dg=='); | ||
}); | ||
it('should successfully encode a Chinese character', () => { | ||
expect(base64.encode('我')).toEqual('5oiR'); | ||
}); | ||
it('should successfully encode ascii characters', () => { | ||
expect(base64.encode('vs')).toEqual('dnM='); | ||
}); | ||
it('should successfully encode a Chinese characters', () => { | ||
expect(base64.encode('我是')).toEqual('5oiR5piv'); | ||
}); | ||
it('should successfully decode a ascii character', () => { | ||
expect(base64.decode('dg==')).toEqual('v'); | ||
}); | ||
it('should successfully decode a Chinese character', () => { | ||
expect(base64.decode('5oiR')).toEqual('我'); | ||
}); | ||
it('should successfully decode ascii characters', () => { | ||
expect(base64.decode('dnM=')).toEqual('vs'); | ||
}); | ||
it('should successfully decode a Chinese characters', () => { | ||
expect(base64.decode('5oiR5piv')).toEqual('我是'); | ||
}); | ||
}); | ||
|
||
describe('nodejs behavior', () => { | ||
it('should successfully create a base64 object and encode/decode string in node.js', () => { | ||
expect(base64.encode('我是')).toEqual('5oiR5piv'); | ||
expect(base64.decode('5oiR5piv')).toEqual('我是'); | ||
}); | ||
}); | ||
|
||
describe('browser behavior', () => { | ||
// capture the TextEncoder and TextDecoder from the util module and assign them to the global object (for mocking browser environment) | ||
beforeAll(() => { | ||
const { TextDecoder } = require('util'); | ||
global.TextDecoder = TextDecoder; | ||
|
||
const { TextEncoder } = require('util'); | ||
global.TextEncoder = TextEncoder; | ||
}); | ||
// restore the TextEncoder and TextDecoder to undefined | ||
afterAll(() => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
global.TextEncoder = undefined; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
global.TextDecoder = undefined; | ||
}); | ||
it('should successfully create a base64 object and encode/decode string in browser', () => { | ||
expect(base64.encode('我是')).toEqual('5oiR5piv'); | ||
expect(base64.decode('5oiR5piv')).toEqual('我是'); | ||
}); | ||
}); |