gitlab-org--gitlab-foss/app/assets/javascripts/lib/utils/accessor.js
Nathan Friend bee3c7e847 Comply with no-implicit-coercion rule (CE)
This commit is the result of running `yarn eslint --fix` after enabling
the `no-implicit-coercion` ESLint rule.  This rule has been added to
our ESLint config here:

https://gitlab.com/gitlab-org/gitlab-eslint-config/merge_requests/14
2019-06-03 22:51:02 +00:00

47 lines
854 B
JavaScript

function isPropertyAccessSafe(base, property) {
let safe;
try {
safe = Boolean(base[property]);
} catch (error) {
safe = false;
}
return safe;
}
function isFunctionCallSafe(base, functionName, ...args) {
let safe = true;
try {
base[functionName](...args);
} catch (error) {
safe = false;
}
return safe;
}
function isLocalStorageAccessSafe() {
let safe;
const TEST_KEY = 'isLocalStorageAccessSafe';
const TEST_VALUE = 'true';
safe = isPropertyAccessSafe(window, 'localStorage');
if (!safe) return safe;
safe = isFunctionCallSafe(window.localStorage, 'setItem', TEST_KEY, TEST_VALUE);
if (safe) window.localStorage.removeItem(TEST_KEY);
return safe;
}
const AccessorUtilities = {
isPropertyAccessSafe,
isFunctionCallSafe,
isLocalStorageAccessSafe,
};
export default AccessorUtilities;