Skip to content

Commit

Permalink
feat: added window utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Jan 28, 2020
1 parent dc2a517 commit eb22f9a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 81 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"cz-conventional-changelog": "2.1.0",
"jest": "24.3.1",
"rimraf": "2.6.3",
"rollup-all": "1.10.0",
"rollup-all": "1.10.4",
"semantic-release": "^15.13.24",
"semantic-release-cli": "5.2.1",
"tslint": "5.12.0",
Expand Down
75 changes: 75 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,78 @@ export const uniqueArray = <T = any>(array: T[]): T[] => {

return unique;
};

export const isBrowser = () => typeof window !== 'undefined';

/**
* detects if local storage is supported
*/
export const localStorageSupported = () => {
return isBrowser() && typeof window.localStorage !== 'undefined';
};

/**
* detects if service worker is supported
*/
export const serviceWorkerSupported = () => {
return isBrowser() && 'serviceWorker' in window.navigator;
};

/**
* detects if push notifications is supported
*/
export const notificationSupported = () => {
return isBrowser() && 'Notification' in window;
};

/**
* retrieves a value from storage
*/
export const getFromStorage = (key: string, defaultValue: any = null) => {
if (localStorageSupported()) {
const value = window.localStorage.getItem(key);
if (isNull(value) || isUndefined(value)) {
return defaultValue;
} else {
return value;
}
} else {
return defaultValue;
}
};

/**
* sets a value to storage if storage is a supported.
* returns true if succeeds, else returns false
*/
export const setToStorage = (key: string, value: string) => {
if (localStorageSupported()) {
window.localStorage.setItem(key, value);
return true;
}
return false;
};

/**
* request for notification permission
*/
export const requestNotification = () => {
const hasPromiseSupport = () => {
try {
Notification.requestPermission().then();
} catch (e) {
return false;
}
return true;
};

return new Promise(resolve => {
if (notificationSupported()) {
if (hasPromiseSupport()) {
Notification.requestPermission().then(status => resolve());
} else {
Notification.requestPermission(status => resolve(status));
}
}
});
};
98 changes: 18 additions & 80 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,10 @@
exec-sh "^0.3.2"
minimist "^1.2.0"

"@forensic-js/node-utils@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@forensic-js/node-utils/-/node-utils-1.0.0.tgz#1534b19b9becd699a1cd9d46702b6660f7f35744"
integrity sha512-W1e+dS8J7+IuKlQb3wS3OYIvkRonXg0FKa2oXj3uSU3A82XiXHkQk8PUAYfEtPwp0K3SbTNko5wX1qk4tmf19g==
"@forensic-js/node-utils@1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@forensic-js/node-utils/-/node-utils-1.0.2.tgz#376c010cebbe268ef5e719124f07816579c2d4c9"
integrity sha512-bFbUJJ32x7ls+qGAoDnVAKqTSI2dxOx8Kyk77lP1gSofzjqBhF46OTS339q6KZIg2PuzvrfJp+ithev/cagCGA==
dependencies:
"@babel/plugin-transform-runtime" "7.4.3"
"@babel/runtime" "7.4.3"
Expand Down Expand Up @@ -3169,14 +3169,14 @@ debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9:
dependencies:
ms "2.0.0"

debug@^3.1.0, debug@^3.2.6:
debug@^3.1.0:
version "3.2.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3294,11 +3294,6 @@ detect-indent@^5.0.0, detect-indent@~5.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
integrity sha1-OHHMCmoALow+Wzz38zYmRnXwa50=

detect-libc@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=

detect-newline@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"
Expand Down Expand Up @@ -4359,7 +4354,7 @@ humanize-ms@^1.2.1:
dependencies:
ms "^2.0.0"

iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@^0.4.4, iconv-lite@~0.4.13:
iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@~0.4.13:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==
Expand Down Expand Up @@ -4416,7 +4411,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -5688,11 +5683,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -5701,33 +5691,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -5773,11 +5741,6 @@ lodash.map@^4.5.1:
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down Expand Up @@ -6223,15 +6186,6 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

needle@^2.2.1:
version "2.4.0"
resolved "https://registry.yarnpkg.com/needle/-/needle-2.4.0.tgz#6833e74975c444642590e15a750288c5f939b57c"
integrity sha512-4Hnwzr3mi5L97hMYeNl8wRW/Onhy4nUKR/lVemJ8gJedxxUyBLm9kkrDColJvoSfwi0jCNhD+xCdOtiGDQiRZg==
dependencies:
debug "^3.2.6"
iconv-lite "^0.4.4"
sax "^1.2.4"

neo-async@^2.6.0:
version "2.6.1"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c"
Expand Down Expand Up @@ -6306,22 +6260,6 @@ node-notifier@^5.4.2:
shellwords "^0.1.1"
which "^1.3.0"

node-pre-gyp@*:
version "0.14.0"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz#9a0596533b877289bcad4e143982ca3d904ddc83"
integrity sha512-+CvDC7ZttU/sSt9rFjix/P05iS43qHCOOGzcr3Ry99bXG7VX953+vFyEuph/tfqoYu8dttBkE86JSKBO2OzcxA==
dependencies:
detect-libc "^1.0.2"
mkdirp "^0.5.1"
needle "^2.2.1"
nopt "^4.0.1"
npm-packlist "^1.1.6"
npmlog "^4.0.2"
rc "^1.2.7"
rimraf "^2.6.1"
semver "^5.3.0"
tar "^4.4.2"

node-releases@^1.1.44:
version "1.1.46"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.46.tgz#6b262afef1bdc9a950a96df2e77e0d2290f484bf"
Expand Down Expand Up @@ -6420,7 +6358,7 @@ npm-normalize-package-bin@^1.0.0, npm-normalize-package-bin@^1.0.1:
semver "^5.6.0"
validate-npm-package-name "^3.0.0"

npm-packlist@^1.1.12, npm-packlist@^1.1.6, npm-packlist@^1.4.7:
npm-packlist@^1.1.12, npm-packlist@^1.4.7:
version "1.4.7"
resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.4.7.tgz#9e954365a06b80b18111ea900945af4f88ed4848"
integrity sha512-vAj7dIkp5NhieaGZxBJB8fF4R0078rqsmhJcAfXZ6O7JJhjhPK96n5Ry1oZcfLXgfun0GWTZPOxaEyqv8GBykQ==
Expand Down Expand Up @@ -6599,7 +6537,7 @@ npm@^6.0.0, npm@^6.10.3:
worker-farm "^1.7.0"
write-file-atomic "^2.4.3"

npmlog@^4.0.0, npmlog@^4.0.2, npmlog@^4.1.2, npmlog@~4.1.2:
npmlog@^4.0.0, npmlog@^4.1.2, npmlog@~4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b"
integrity sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==
Expand Down Expand Up @@ -7254,7 +7192,7 @@ qw@~1.0.1:
resolved "https://registry.yarnpkg.com/qw/-/qw-1.0.1.tgz#efbfdc740f9ad054304426acb183412cc8b996d4"
integrity sha1-77/cdA+a0FQwRCassYNBLMi5ltQ=

rc@^1.0.1, rc@^1.1.6, rc@^1.2.7, rc@^1.2.8:
rc@^1.0.1, rc@^1.1.6, rc@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
integrity sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==
Expand Down Expand Up @@ -7757,17 +7695,17 @@ rimraf@2.6.3:
dependencies:
glob "^7.1.3"

rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2, rimraf@^2.6.3:
rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.2, rimraf@^2.6.3:
version "2.7.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
dependencies:
glob "^7.1.3"

rollup-all@1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/rollup-all/-/rollup-all-1.10.0.tgz#95095942a1a70c32a69d565176e987ae7f1e46fa"
integrity sha512-pz3yyG5cMWeUe19/ceDaSJ+yXDUQ7rFIx+nMiQu6LP7R0FjmpxgPg17lRfnQKNyMNDIP+3ocfLvQkODatjgYJQ==
rollup-all@1.10.4:
version "1.10.4"
resolved "https://registry.yarnpkg.com/rollup-all/-/rollup-all-1.10.4.tgz#3e0828d1480bfafcdb562e6587233f79e4c6f390"
integrity sha512-ftUsxtdaqnloI0hDdcR2wqMpGq4inKtcjT912t4BqEbVM4CKt5/YddJh8iRwjn44Auohj9y1CoJmK1H8mnpxPw==
dependencies:
"@babel/core" "7.7.5"
"@babel/plugin-proposal-class-properties" "^7.4.0"
Expand All @@ -7778,7 +7716,7 @@ rollup-all@1.10.0:
"@babel/preset-env" "7.7.6"
"@babel/preset-typescript" "7.7.4"
"@babel/runtime" "7.7.6"
"@forensic-js/node-utils" "1.0.0"
"@forensic-js/node-utils" "1.0.2"
"@forensic-js/utils" "2.9.0"
"@rollup/plugin-commonjs" "11.0.1"
"@rollup/plugin-json" "4.0.0"
Expand Down Expand Up @@ -8525,7 +8463,7 @@ symbol-tree@^3.2.2:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

tar@^4.4.10, tar@^4.4.12, tar@^4.4.13, tar@^4.4.2:
tar@^4.4.10, tar@^4.4.12, tar@^4.4.13:
version "4.4.13"
resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525"
integrity sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==
Expand Down

0 comments on commit eb22f9a

Please sign in to comment.