Skip to content

Commit 8b2d509

Browse files
committed
Add pkg to resolve the global object
1 parent 866f749 commit 8b2d509

File tree

12 files changed

+712
-0
lines changed

12 files changed

+712
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# Global
22+
23+
> Return the global object.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var getGlobal = require( '@stdlib/utils/global' );
41+
```
42+
43+
#### getGlobal( \[codegen] )
44+
45+
Returns the global object.
46+
47+
```javascript
48+
var g = getGlobal();
49+
// returns {...}
50+
```
51+
52+
By default, the function does **not** use code generation when resolving the global object. While code generation is the **most** reliable means for resolving the global object, its use may violate [content security policies][mdn-csp] (CSPs). To use code generation, provide a `codegen` argument equal to `true`.
53+
54+
```javascript
55+
var g = getGlobal( true );
56+
// returns {...}
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var getGlobal = require( '@stdlib/utils/global' );
81+
82+
// Resolve the global object:
83+
var g = getGlobal();
84+
85+
// Display the object's contents:
86+
console.log( g );
87+
```
88+
89+
</section>
90+
91+
<!-- /.examples -->
92+
93+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
94+
95+
<section class="references">
96+
97+
</section>
98+
99+
<!-- /.references -->
100+
101+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="links">
104+
105+
[mdn-csp]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
106+
107+
</section>
108+
109+
<!-- /.links -->
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 pkg = require( './../package.json' ).name;
25+
var getGlobal = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+'::default', function benchmark( b ) {
31+
var g;
32+
var i;
33+
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
// NOTE: this may get optimized away, as the return value should be same after every invocation
37+
g = getGlobal();
38+
if ( typeof g !== 'object' ) {
39+
b.fail( 'should return an object' );
40+
}
41+
}
42+
b.toc();
43+
if ( typeof g !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg+':codegen=false', function benchmark( b ) {
51+
var g;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
// NOTE: this may get optimized away, as the return value should be same after every invocation
57+
g = getGlobal( false );
58+
if ( typeof g !== 'object' ) {
59+
b.fail( 'should return an object' );
60+
}
61+
}
62+
b.toc();
63+
if ( typeof g !== 'object' ) {
64+
b.fail( 'should return an object' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
69+
70+
bench( pkg+':codegen=true', function benchmark( b ) {
71+
var g;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
// NOTE: this may get optimized away, as the return value should be same after every invocation
77+
g = getGlobal( true );
78+
if ( typeof g !== 'object' ) {
79+
b.fail( 'should return an object' );
80+
}
81+
}
82+
b.toc();
83+
if ( typeof g !== 'object' ) {
84+
b.fail( 'should return an object' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( [codegen] )
3+
Returns the global object.
4+
5+
Parameters
6+
----------
7+
codegen: boolean (optional)
8+
Boolean indicating whether to use code generation to resolve the global
9+
object. Code generation is the *most* reliable means for resolving the
10+
global object; however, using code generation may violate content
11+
security policies (CSPs). Default: false.
12+
13+
Returns
14+
-------
15+
global: object
16+
Global object.
17+
18+
Examples
19+
--------
20+
> var g = {{alias}}()
21+
{...}
22+
23+
See Also
24+
--------
25+
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) 2018 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 getGlobal = require( './../lib' );
22+
23+
console.log( getGlobal() );
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) 2018 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+
// MAIN //
22+
23+
/**
24+
* Returns the global object using code generation.
25+
*
26+
* @private
27+
* @returns {Object} global object
28+
*/
29+
function getGlobal() {
30+
return new Function( 'return this;' )(); // eslint-disable-line no-new-func
31+
}
32+
33+
34+
// EXPORTS //
35+
36+
module.exports = getGlobal;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
// MAIN //
22+
23+
var obj = ( typeof global === 'object' ) ? global : null;
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = obj;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
* Return the global object.
23+
*
24+
* @module @stdlib/utils/global
25+
*
26+
* @example
27+
* var getGlobal = require( '@stdlib/utils/global' );
28+
*
29+
* var g = getGlobal();
30+
* // returns {...}
31+
*/
32+
33+
// MODULES //
34+
35+
var getGlobal = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = getGlobal;

0 commit comments

Comments
 (0)