|
| 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. |
| 25 | +* |
| 26 | +* @returns {Object} global object |
| 27 | +* |
| 28 | +* @example |
| 29 | +* var g = getGlobal(); |
| 30 | +* // returns <global_object> |
| 31 | +*/ |
| 32 | +function getGlobal() { |
| 33 | + // Check for global: |
| 34 | + if ( typeof global !== 'undefined' ) { |
| 35 | + return global; |
| 36 | + } |
| 37 | + // Check for self (e.g., web workers): |
| 38 | + if ( typeof self !== 'undefined' ) { |
| 39 | + return self; |
| 40 | + } |
| 41 | + // Check for window: |
| 42 | + if ( typeof window !== 'undefined' ) { |
| 43 | + return window; |
| 44 | + } |
| 45 | + // Fall back to Function constructor: |
| 46 | + try { |
| 47 | + return Function( 'return this' )(); |
| 48 | + } catch ( err ) { |
| 49 | + // Empty... |
| 50 | + } |
| 51 | + return {}; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +// EXPORTS // |
| 56 | + |
| 57 | +module.exports = getGlobal; |
| 58 | +EOF |
| 59 | + |
| 60 | +# Create index.js |
| 61 | +cat > lib/index.js << 'EOF' |
| 62 | +/** |
| 63 | +* @license Apache-2.0 |
| 64 | +* |
| 65 | +* Copyright (c) 2018 The Stdlib Authors. |
| 66 | +* |
| 67 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 68 | +* you may not use this file except in compliance with the License. |
| 69 | +* You may obtain a copy of the License at |
| 70 | +* |
| 71 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 72 | +* |
| 73 | +* Unless required by applicable law or agreed to in writing, software |
| 74 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 75 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 76 | +* See the License for the specific language governing permissions and |
| 77 | +* limitations under the License. |
| 78 | +*/ |
| 79 | + |
| 80 | +'use strict'; |
| 81 | + |
| 82 | +/** |
| 83 | +* Return the global object. |
| 84 | +* |
| 85 | +* @module @stdlib/utils/global-object |
| 86 | +* |
| 87 | +* @example |
| 88 | +* var getGlobal = require( '@stdlib/utils/global-object' ); |
| 89 | +* |
| 90 | +* var g = getGlobal(); |
| 91 | +* // returns <global_object> |
| 92 | +*/ |
| 93 | + |
| 94 | +// MODULES // |
| 95 | + |
| 96 | +var getGlobal = require( './main.js' ); |
| 97 | + |
| 98 | + |
| 99 | +// EXPORTS // |
| 100 | + |
| 101 | +module.exports = getGlobal; |
| 102 | +EOF |
| 103 | + |
| 104 | +# Create package.json |
| 105 | +cat > package.json << 'EOF' |
| 106 | +{ |
| 107 | + "name": "@stdlib/utils/global-object", |
| 108 | + "version": "0.0.0", |
| 109 | + "description": "Return the global object.", |
| 110 | + "license": "Apache-2.0", |
| 111 | + "author": { |
| 112 | + "name": "The Stdlib Authors", |
| 113 | + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" |
| 114 | + }, |
| 115 | + "contributors": [ |
| 116 | + { |
| 117 | + "name": "The Stdlib Authors", |
| 118 | + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" |
| 119 | + } |
| 120 | + ], |
| 121 | + "main": "./lib", |
| 122 | + "directories": { |
| 123 | + "doc": "./docs", |
| 124 | + "example": "./examples", |
| 125 | + "lib": "./lib", |
| 126 | + "test": "./test" |
| 127 | + }, |
| 128 | + "types": "./docs/types", |
| 129 | + "scripts": {}, |
| 130 | + "homepage": "https://github.com/stdlib-js/stdlib", |
| 131 | + "repository": { |
| 132 | + "type": "git", |
| 133 | + "url": "git://github.com/stdlib-js/stdlib.git" |
| 134 | + }, |
| 135 | + "bugs": { |
| 136 | + "url": "https://github.com/stdlib-js/stdlib/issues" |
| 137 | + }, |
| 138 | + "dependencies": {}, |
| 139 | + "devDependencies": {}, |
| 140 | + "engines": { |
| 141 | + "node": ">=0.10.0", |
| 142 | + "npm": ">2.7.0" |
| 143 | + }, |
| 144 | + "keywords": [ |
| 145 | + "stdlib", |
| 146 | + "stdutils", |
| 147 | + "utils", |
| 148 | + "util", |
| 149 | + "utilities", |
| 150 | + "utility", |
| 151 | + "global", |
| 152 | + "window", |
| 153 | + "self", |
| 154 | + "object" |
| 155 | + ] |
| 156 | +} |
| 157 | +EOF |
| 158 | + |
| 159 | + |
| 160 | +mkdir -p docs examples test |
0 commit comments