window-getters
Version:
Safe Window Getters
59 lines • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function getFromWindow(name) {
let res = undefined;
if (typeof window !== 'undefined' && typeof window[name] !== 'undefined') {
res = window[name];
}
return res;
}
exports.getFromWindow = getFromWindow;
function getFromWindowOrThrow(name) {
const res = getFromWindow(name);
if (!res) {
throw new Error(`${name} is not defined in Window`);
}
return res;
}
exports.getFromWindowOrThrow = getFromWindowOrThrow;
function getDocumentOrThrow() {
return getFromWindowOrThrow('document');
}
exports.getDocumentOrThrow = getDocumentOrThrow;
function getDocument() {
return getFromWindow('document');
}
exports.getDocument = getDocument;
function getNavigatorOrThrow() {
return getFromWindowOrThrow('navigator');
}
exports.getNavigatorOrThrow = getNavigatorOrThrow;
function getNavigator() {
return getFromWindow('navigator');
}
exports.getNavigator = getNavigator;
function getLocationOrThrow() {
return getFromWindowOrThrow('location');
}
exports.getLocationOrThrow = getLocationOrThrow;
function getLocation() {
return getFromWindow('location');
}
exports.getLocation = getLocation;
function getCryptoOrThrow() {
return getFromWindowOrThrow('crypto');
}
exports.getCryptoOrThrow = getCryptoOrThrow;
function getCrypto() {
return getFromWindow('crypto');
}
exports.getCrypto = getCrypto;
function getLocalStorageOrThrow() {
return getFromWindowOrThrow('localStorage');
}
exports.getLocalStorageOrThrow = getLocalStorageOrThrow;
function getLocalStorage() {
return getFromWindow('localStorage');
}
exports.getLocalStorage = getLocalStorage;
//# sourceMappingURL=index.js.map