Skip to content

Commit 37e0d70

Browse files
committed
feat: add string/base/atob
1 parent a3f4242 commit 37e0d70

File tree

16 files changed

+837
-0
lines changed

16 files changed

+837
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
# atob
22+
23+
> Decode a string of data which has been encoded using Base64 encoding.
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 atob = require( '@stdlib/string/base/atob' );
35+
```
36+
37+
#### atob( str )
38+
39+
Decodes a string of data which has been encoded using Base64 encoding.
40+
41+
<!-- eslint-disable stdlib/no-redeclare -->
42+
43+
```javascript
44+
var out = atob( 'SGVsbG8sIHdvcmxk' );
45+
// returns 'Hello, world'
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="notes">
53+
54+
## Notes
55+
56+
- This function differs from the global [`atob`][mdn-atob] available in web browsers and more recent Node.js versions in that the function returns `null` when provided a string containing non-ASCII characters, rather than raising an exception.
57+
58+
</section>
59+
60+
<!-- /.notes -->
61+
62+
<!-- Package usage examples. -->
63+
64+
<section class="examples">
65+
66+
## Examples
67+
68+
<!-- eslint-disable stdlib/no-redeclare -->
69+
70+
```javascript
71+
var atob = require( '@stdlib/string/base/atob' );
72+
73+
var str = 'SGVsbG8gV29ybGQh';
74+
var out = atob( str );
75+
// returns 'Hello World!'
76+
77+
str = 'SEVMTE8gV09STEQh';
78+
out = atob( str );
79+
// returns 'HELLO WORLD!'
80+
81+
str = 'VG8gYmUsIG9yIG5vdCB0byBiZTogdGhhdCBpcyB0aGUgcXVlc3Rpb24u';
82+
out = atob( str );
83+
// returns 'To be, or not to be: that is the question.'
84+
```
85+
86+
</section>
87+
88+
<!-- /.examples -->
89+
90+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
91+
92+
<section class="related">
93+
94+
</section>
95+
96+
<!-- /.related -->
97+
98+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
99+
100+
<section class="links">
101+
102+
[mdn-atob]: https://developer.mozilla.org/en-US/docs/Web/API/Window/atob
103+
104+
<!-- <related-links> -->
105+
106+
<!-- </related-links> -->
107+
108+
</section>
109+
110+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var atob = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'beepboop',
38+
'foobar',
39+
'xyzabc'
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = atob( values[ i%values.length ] );
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( out ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 55 additions & 0 deletions
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var atob = require( './../lib/polyfill.js' ); // eslint-disable-line stdlib/no-redeclare
27+
28+
29+
// MAIN //
30+
31+
bench( pkg + '::polyfill', function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'beepboop',
38+
'foobar',
39+
'xyzabc'
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = atob( values[ i%values.length ] );
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( out ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( str )
3+
Decodes a string of data which has been encoded using Base64 encoding.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Binary string containing base64-encoded data.
9+
10+
Returns
11+
-------
12+
out: string
13+
An ASCII string containing decoded data.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( 'SGVsbG8sIHdvcmxk' )
18+
'Hello, world'
19+
20+
See Also
21+
--------
22+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
* Decodes a string of data which has been encoded using Base64 encoding.
23+
*
24+
* @param str - binary string containing base64-encoded data
25+
* @returns an ASCII string containing decoded data
26+
*
27+
* @example
28+
* var out = atob( 'SGVsbG8sIHdvcmxk' );
29+
* // returns 'Hello, world'
30+
*/
31+
declare function atob( str: string ): string;
32+
33+
34+
// EXPORTS //
35+
36+
export = atob;
Lines changed: 44 additions & 0 deletions
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 atob = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
atob( 'Hello World!' ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
atob( true ); // $ExpectError
32+
atob( false ); // $ExpectError
33+
atob( null ); // $ExpectError
34+
atob( undefined ); // $ExpectError
35+
atob( 5 ); // $ExpectError
36+
atob( [] ); // $ExpectError
37+
atob( {} ); // $ExpectError
38+
atob( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
atob(); // $ExpectError
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 atob = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare
22+
23+
var str = 'SGVsbG8gV29ybGQh';
24+
var out = atob( str );
25+
console.log( out );
26+
// => 'Hello World!'
27+
28+
str = 'SEVMTE8gV09STEQh';
29+
out = atob( str );
30+
console.log( out );
31+
// => 'HELLO WORLD!'
32+
33+
str = 'VG8gYmUsIG9yIG5vdCB0byBiZTogdGhhdCBpcyB0aGUgcXVlc3Rpb24u';
34+
out = atob( str );
35+
console.log( out );
36+
// => 'To be, or not to be: that is the question.'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// EXPORTS //
22+
23+
module.exports = atob;

0 commit comments

Comments
 (0)