Skip to content

Commit 09e7120

Browse files
committed
feat: add string/base/base64-to-uint8array
1 parent 9f848ed commit 09e7120

File tree

10 files changed

+580
-0
lines changed

10 files changed

+580
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# base64ToUint8Array
22+
23+
> Convert a Base64-encoded string to a [Uint8Array][@stdlib/array/uint8].
24+
25+
<!-- Package usage documentation. -->
26+
27+
<section class="usage">
28+
29+
## Usage
30+
31+
<!-- eslint-disable stdlib/no-redeclare -->
32+
33+
```javascript
34+
var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );
35+
```
36+
37+
#### base64ToUint8Array( str )
38+
39+
Converts a base64-encoded string to a [Uint8Array][@stdlib/array/uint8].
40+
41+
```javascript
42+
var string2buffer = require( '@stdlib/buffer/from-string' );
43+
44+
var str = string2buffer( 'Hello World!' ).toString( 'base64' );
45+
// returns 'SGVsbG8gV29ybGQh'
46+
47+
var out = base64ToUint8Array( str );
48+
// returns <Uint8Array>[ 72, 101, ... ]
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
## Notes
58+
59+
- The function returns `null` when provided a string containing non-ASCII characters.
60+
61+
</section>
62+
63+
<!-- /.notes -->
64+
65+
<!-- Package usage examples. -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
```javascript
72+
var string2buffer = require( '@stdlib/buffer/from-string' );
73+
var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );
74+
75+
var buf = string2buffer( 'Hello World!' ).toString( 'base64' );
76+
// returns 'SGVsbG8gV29ybGQh'
77+
78+
var arr = base64ToUint8Array( buf );
79+
// returns <Uint8Array>
80+
81+
buf = string2buffer( 'HELLO WORLD!' ).toString( 'base64' );
82+
// returns 'SEVMTE8gV09STEQh'
83+
84+
arr = base64ToUint8Array( buf );
85+
// returns <Uint8Array>
86+
87+
buf = string2buffer( 'To be, or not to be: that is the question.' ).toString( 'base64' );
88+
// returns 'VG8gYmUsIG9yIG5vdCB0byBiZTogdGhhdCBpcyB0aGUgcXVlc3Rpb24u'
89+
90+
arr = base64ToUint8Array( buf );
91+
// returns <Uint8Array>
92+
```
93+
94+
</section>
95+
96+
<!-- /.examples -->
97+
98+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
99+
100+
<section class="related">
101+
102+
</section>
103+
104+
<!-- /.related -->
105+
106+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
107+
108+
<section class="links">
109+
110+
[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8
111+
112+
<!-- <related-links> -->
113+
114+
<!-- </related-links> -->
115+
116+
</section>
117+
118+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
25+
var pkg = require( './../package.json' ).name;
26+
var base64ToUint8Array = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'SGVsbG8gV29ybGQh',
38+
'VG8gYmUsIG9yIG5vdCB0byBiZTogdGhhdCBpcyB0aGUgcXVlc3Rpb24u',
39+
'SEVMTE8gV09STEQh'
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = base64ToUint8Array( values[ i%values.length ] );
45+
if ( typeof out !== 'object' ) {
46+
b.fail( 'should return a Uint8Array' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isUint8Array( out ) ) {
51+
b.fail( 'should return a Uint8Array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( str )
3+
Converts a base64-encoded string to a Uint8Array.
4+
5+
If provided a string containing non-ASCII characters, the function returns
6+
`null`.
7+
8+
Parameters
9+
----------
10+
str: string
11+
Base64-encoded string.
12+
13+
Returns
14+
-------
15+
out: Uint8Array|null
16+
Output array.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'SGVsbG8gV29ybGQh' )
21+
<Uint8Array>
22+
23+
See Also
24+
--------
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Converts a base64-encoded string to a Uint8Array.
23+
*
24+
* ## Notes
25+
*
26+
* - If provided a string containing non-ASCII characters, the function returns `null`.
27+
*
28+
* @param str - base64-encoded string
29+
* @returns output array
30+
*
31+
* @example
32+
* var string2buffer = require( '@stdlib/buffer/from-string' );
33+
*
34+
* var str = string2buffer( 'Hello World!' ).toString( 'base64' );
35+
* // returns 'SGVsbG8gV29ybGQh'
36+
*
37+
* var out = base64ToUint8Array( str );
38+
* // returns <Uint8Array>[ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]
39+
*/
40+
declare function base64ToUint8Array( str: string ): Uint8Array | null;
41+
42+
43+
// EXPORTS //
44+
45+
export = base64ToUint8Array;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import base64ToUint8Array = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a Uint8Array (or null)...
25+
{
26+
base64ToUint8Array( 'Hello World!' ); // $ExpectType Uint8Array | null
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
base64ToUint8Array( true ); // $ExpectError
32+
base64ToUint8Array( false ); // $ExpectError
33+
base64ToUint8Array( null ); // $ExpectError
34+
base64ToUint8Array( undefined ); // $ExpectError
35+
base64ToUint8Array( 5 ); // $ExpectError
36+
base64ToUint8Array( [] ); // $ExpectError
37+
base64ToUint8Array( {} ); // $ExpectError
38+
base64ToUint8Array( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
base64ToUint8Array(); // $ExpectError
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var string2buffer = require( '@stdlib/buffer/from-string' );
22+
var base64ToUint8Array = require( './../lib' );
23+
24+
var buf = string2buffer( 'Hello World!' ).toString( 'base64' );
25+
// returns 'SGVsbG8gV29ybGQh'
26+
27+
var arr = base64ToUint8Array( buf );
28+
console.log( arr );
29+
30+
buf = string2buffer( 'HELLO WORLD!' ).toString( 'base64' );
31+
// returns 'SEVMTE8gV09STEQh'
32+
33+
arr = base64ToUint8Array( buf );
34+
console.log( arr );
35+
36+
buf = string2buffer( 'To be, or not to be: that is the question.' ).toString( 'base64' );
37+
// returns 'VG8gYmUsIG9yIG5vdCB0byBiZTogdGhhdCBpcyB0aGUgcXVlc3Rpb24u'
38+
39+
arr = base64ToUint8Array( buf );
40+
console.log( arr );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Convert a base64-encoded string to a Uint8Array.
23+
*
24+
* @module @stdlib/string/base/base64-to-uint8array
25+
*
26+
* @example
27+
* var string2buffer = require( '@stdlib/buffer/from-string' );
28+
* var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );
29+
*
30+
* var str = string2buffer( 'Hello World!' ).toString( 'base64' );
31+
* // returns 'SGVsbG8gV29ybGQh'
32+
*
33+
* var out = base64ToUint8Array( str );
34+
* // returns <Uint8Array>[ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]
35+
*/
36+
37+
// MODULES //
38+
39+
var main = require( './main.js' );
40+
41+
42+
// EXPORTS //
43+
44+
module.exports = main;

0 commit comments

Comments
 (0)