@dynamic-labs/utils 0.19.0-alpha.8 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +321 -0
- package/package.json +7 -5
- package/src/CancellablePromise/CancellablePromise.cjs +126 -126
- package/src/CancellablePromise/CancellablePromise.d.ts +54 -54
- package/src/CancellablePromise/CancellablePromise.js +126 -126
- package/src/CancellablePromise/index.d.ts +1 -1
- package/src/bigIntToHex/bigIntToHex.cjs +15 -0
- package/src/bigIntToHex/bigIntToHex.d.ts +1 -0
- package/src/bigIntToHex/bigIntToHex.js +11 -0
- package/src/bigIntToHex/index.d.ts +1 -0
- package/src/bufferToBase64.cjs +5 -5
- package/src/bufferToBase64.d.ts +1 -1
- package/src/bufferToBase64.js +5 -5
- package/src/errors/AccountExistsError.cjs +6 -6
- package/src/errors/AccountExistsError.d.ts +13 -13
- package/src/errors/AccountExistsError.js +6 -6
- package/src/errors/ChainalysisError.cjs +5 -5
- package/src/errors/ChainalysisError.d.ts +5 -5
- package/src/errors/ChainalysisError.js +5 -5
- package/src/errors/CustomError.cjs +16 -16
- package/src/errors/CustomError.d.ts +11 -11
- package/src/errors/CustomError.js +16 -16
- package/src/errors/DynamicError.cjs +1 -1
- package/src/errors/DynamicError.d.ts +3 -3
- package/src/errors/DynamicError.js +1 -1
- package/src/errors/EmailAlreadyExistsError.cjs +4 -4
- package/src/errors/EmailAlreadyExistsError.d.ts +4 -4
- package/src/errors/EmailAlreadyExistsError.js +4 -4
- package/src/errors/EmailProviderError.cjs +4 -4
- package/src/errors/EmailProviderError.d.ts +4 -4
- package/src/errors/EmailProviderError.js +4 -4
- package/src/errors/EmailVerificationError.cjs +4 -4
- package/src/errors/EmailVerificationError.d.ts +4 -4
- package/src/errors/EmailVerificationError.js +4 -4
- package/src/errors/GateBlockedError.cjs +5 -5
- package/src/errors/GateBlockedError.d.ts +5 -5
- package/src/errors/GateBlockedError.js +5 -5
- package/src/errors/MissingEnvironmentIdError.cjs +4 -4
- package/src/errors/MissingEnvironmentIdError.d.ts +4 -4
- package/src/errors/MissingEnvironmentIdError.js +4 -4
- package/src/errors/NoAccessError.cjs +6 -6
- package/src/errors/NoAccessError.d.ts +9 -9
- package/src/errors/NoAccessError.js +6 -6
- package/src/errors/NotSupportedError.cjs +1 -1
- package/src/errors/NotSupportedError.d.ts +3 -3
- package/src/errors/NotSupportedError.js +1 -1
- package/src/errors/SocialAccountAlreadyExistsError.cjs +4 -4
- package/src/errors/SocialAccountAlreadyExistsError.d.ts +4 -4
- package/src/errors/SocialAccountAlreadyExistsError.js +4 -4
- package/src/errors/UserHasAccountWithEmailError.cjs +5 -5
- package/src/errors/UserHasAccountWithEmailError.d.ts +5 -5
- package/src/errors/UserHasAccountWithEmailError.js +5 -5
- package/src/errors/UsernameAlreadyExistsError.cjs +4 -4
- package/src/errors/UsernameAlreadyExistsError.d.ts +4 -4
- package/src/errors/UsernameAlreadyExistsError.js +4 -4
- package/src/errors/WalletNotDeployedError.cjs +4 -4
- package/src/errors/WalletNotDeployedError.d.ts +4 -4
- package/src/errors/WalletNotDeployedError.js +4 -4
- package/src/errors/WalletUsedError.cjs +4 -4
- package/src/errors/WalletUsedError.d.ts +4 -4
- package/src/errors/WalletUsedError.js +4 -4
- package/src/errors/index.d.ts +16 -16
- package/src/getOrMapViemChain.cjs +77 -0
- package/src/getOrMapViemChain.d.ts +11 -0
- package/src/getOrMapViemChain.js +51 -0
- package/src/getProvidersFromWindow.cjs +38 -0
- package/src/getProvidersFromWindow.d.ts +7 -0
- package/src/getProvidersFromWindow.js +34 -0
- package/src/index.cjs +18 -0
- package/src/index.d.ts +15 -9
- package/src/index.js +7 -1
- package/src/isBigInt/index.d.ts +1 -0
- package/src/isBigInt/isBigInt.cjs +7 -0
- package/src/isBigInt/isBigInt.d.ts +1 -0
- package/src/isBigInt/isBigInt.js +3 -0
- package/src/isFunction/index.d.ts +1 -1
- package/src/isFunction/isFunction.cjs +1 -1
- package/src/isFunction/isFunction.d.ts +1 -1
- package/src/isFunction/isFunction.js +1 -1
- package/src/isMobile.cjs +122 -56
- package/src/isMobile.d.ts +18 -15
- package/src/isMobile.js +120 -57
- package/src/last.cjs +14 -14
- package/src/last.d.ts +15 -15
- package/src/last.js +14 -14
- package/src/localStorageAsync.cjs +17 -17
- package/src/localStorageAsync.d.ts +3 -3
- package/src/localStorageAsync.js +17 -17
- package/src/logger/index.d.ts +1 -1
- package/src/logger/logger.d.ts +2 -2
- package/src/parseChainId.d.ts +1 -1
- package/src/parseEvmNetworks.cjs +6 -6
- package/src/parseEvmNetworks.d.ts +2 -2
- package/src/parseEvmNetworks.js +6 -6
- package/src/retryableFn.cjs +53 -0
- package/src/retryableFn.d.ts +14 -0
- package/src/retryableFn.js +48 -0
- package/src/sleep/index.d.ts +1 -0
- package/src/sleep/sleep.cjs +12 -0
- package/src/sleep/sleep.d.ts +11 -0
- package/src/sleep/sleep.js +8 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var chains = require('viem/chains');
|
|
6
|
+
|
|
7
|
+
function _interopNamespace(e) {
|
|
8
|
+
if (e && e.__esModule) return e;
|
|
9
|
+
var n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
Object.keys(e).forEach(function (k) {
|
|
12
|
+
if (k !== 'default') {
|
|
13
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return e[k]; }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
n["default"] = e;
|
|
22
|
+
return Object.freeze(n);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var chains__namespace = /*#__PURE__*/_interopNamespace(chains);
|
|
26
|
+
|
|
27
|
+
// eslint-disable-next-line import/no-namespace
|
|
28
|
+
/**
|
|
29
|
+
* Gets the chain object for the given chain id.
|
|
30
|
+
* @param chainId - Chain id of the target EVM chain.
|
|
31
|
+
* @returns Viem's chain object.
|
|
32
|
+
*/
|
|
33
|
+
const getChain = (chainId) => {
|
|
34
|
+
for (const chain of Object.values(chains__namespace)) {
|
|
35
|
+
if ('id' in chain) {
|
|
36
|
+
if (chain.id === chainId) {
|
|
37
|
+
return chain;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Chain with id ${chainId} not found`);
|
|
42
|
+
};
|
|
43
|
+
const mapChain = (network) => {
|
|
44
|
+
var _a;
|
|
45
|
+
return ({
|
|
46
|
+
blockExplorers: ((_a = network.blockExplorerUrls) === null || _a === void 0 ? void 0 : _a[0])
|
|
47
|
+
? {
|
|
48
|
+
default: {
|
|
49
|
+
name: network.blockExplorerUrls[0],
|
|
50
|
+
url: network.blockExplorerUrls[0],
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
id: network.chainId,
|
|
55
|
+
name: network.vanityName || network.chainName,
|
|
56
|
+
nativeCurrency: network.nativeCurrency,
|
|
57
|
+
network: network.chainName,
|
|
58
|
+
rpcUrls: {
|
|
59
|
+
default: { http: network.rpcUrls },
|
|
60
|
+
public: { http: network.rpcUrls },
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
const getOrMapViemChain = (network) => {
|
|
65
|
+
let chain;
|
|
66
|
+
try {
|
|
67
|
+
chain = getChain(network.chainId);
|
|
68
|
+
}
|
|
69
|
+
catch (_a) {
|
|
70
|
+
chain = mapChain(network);
|
|
71
|
+
}
|
|
72
|
+
return chain;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
exports.getChain = getChain;
|
|
76
|
+
exports.getOrMapViemChain = getOrMapViemChain;
|
|
77
|
+
exports.mapChain = mapChain;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Chain } from 'viem';
|
|
2
|
+
import * as chains from 'viem/chains';
|
|
3
|
+
import { EvmNetwork } from '@dynamic-labs/types';
|
|
4
|
+
/**
|
|
5
|
+
* Gets the chain object for the given chain id.
|
|
6
|
+
* @param chainId - Chain id of the target EVM chain.
|
|
7
|
+
* @returns Viem's chain object.
|
|
8
|
+
*/
|
|
9
|
+
export declare const getChain: (chainId: number) => chains.Chain;
|
|
10
|
+
export declare const mapChain: (network: EvmNetwork) => Chain;
|
|
11
|
+
export declare const getOrMapViemChain: (network: EvmNetwork) => Chain;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as chains from 'viem/chains';
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line import/no-namespace
|
|
4
|
+
/**
|
|
5
|
+
* Gets the chain object for the given chain id.
|
|
6
|
+
* @param chainId - Chain id of the target EVM chain.
|
|
7
|
+
* @returns Viem's chain object.
|
|
8
|
+
*/
|
|
9
|
+
const getChain = (chainId) => {
|
|
10
|
+
for (const chain of Object.values(chains)) {
|
|
11
|
+
if ('id' in chain) {
|
|
12
|
+
if (chain.id === chainId) {
|
|
13
|
+
return chain;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`Chain with id ${chainId} not found`);
|
|
18
|
+
};
|
|
19
|
+
const mapChain = (network) => {
|
|
20
|
+
var _a;
|
|
21
|
+
return ({
|
|
22
|
+
blockExplorers: ((_a = network.blockExplorerUrls) === null || _a === void 0 ? void 0 : _a[0])
|
|
23
|
+
? {
|
|
24
|
+
default: {
|
|
25
|
+
name: network.blockExplorerUrls[0],
|
|
26
|
+
url: network.blockExplorerUrls[0],
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
: undefined,
|
|
30
|
+
id: network.chainId,
|
|
31
|
+
name: network.vanityName || network.chainName,
|
|
32
|
+
nativeCurrency: network.nativeCurrency,
|
|
33
|
+
network: network.chainName,
|
|
34
|
+
rpcUrls: {
|
|
35
|
+
default: { http: network.rpcUrls },
|
|
36
|
+
public: { http: network.rpcUrls },
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const getOrMapViemChain = (network) => {
|
|
41
|
+
let chain;
|
|
42
|
+
try {
|
|
43
|
+
chain = getChain(network.chainId);
|
|
44
|
+
}
|
|
45
|
+
catch (_a) {
|
|
46
|
+
chain = mapChain(network);
|
|
47
|
+
}
|
|
48
|
+
return chain;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { getChain, getOrMapViemChain, mapChain };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* return the destination (value)
|
|
7
|
+
*
|
|
8
|
+
* @param {string} path - path in the obj to the item
|
|
9
|
+
* @returns {T[]} - array of found items
|
|
10
|
+
*/
|
|
11
|
+
const getProvidersFromWindow = (path) => {
|
|
12
|
+
const splitPath = path.split('.');
|
|
13
|
+
let result = window;
|
|
14
|
+
const foundProviders = [];
|
|
15
|
+
for (const [index, p] of Object.entries(splitPath)) {
|
|
16
|
+
result = result[p];
|
|
17
|
+
if (typeof result !== 'object' || result === null) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
// we need to check if we have hit the end of the requested path
|
|
21
|
+
if (Number(index) === splitPath.length - 1) {
|
|
22
|
+
if (Array.isArray(result)) {
|
|
23
|
+
result.forEach((p) => foundProviders.push(p));
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
if (result.providers) {
|
|
27
|
+
result.providers.forEach((p) => foundProviders.push(p));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
foundProviders.push(result);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return foundProviders;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.getProvidersFromWindow = getProvidersFromWindow;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* return the destination (value)
|
|
3
|
+
*
|
|
4
|
+
* @param {string} path - path in the obj to the item
|
|
5
|
+
* @returns {T[]} - array of found items
|
|
6
|
+
*/
|
|
7
|
+
const getProvidersFromWindow = (path) => {
|
|
8
|
+
const splitPath = path.split('.');
|
|
9
|
+
let result = window;
|
|
10
|
+
const foundProviders = [];
|
|
11
|
+
for (const [index, p] of Object.entries(splitPath)) {
|
|
12
|
+
result = result[p];
|
|
13
|
+
if (typeof result !== 'object' || result === null) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
// we need to check if we have hit the end of the requested path
|
|
17
|
+
if (Number(index) === splitPath.length - 1) {
|
|
18
|
+
if (Array.isArray(result)) {
|
|
19
|
+
result.forEach((p) => foundProviders.push(p));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
if (result.providers) {
|
|
23
|
+
result.providers.forEach((p) => foundProviders.push(p));
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
foundProviders.push(result);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return foundProviders;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { getProvidersFromWindow };
|
package/src/index.cjs
CHANGED
|
@@ -26,6 +26,12 @@ var isMobile = require('./isMobile.cjs');
|
|
|
26
26
|
var localStorageAsync = require('./localStorageAsync.cjs');
|
|
27
27
|
var bufferToBase64 = require('./bufferToBase64.cjs');
|
|
28
28
|
var last = require('./last.cjs');
|
|
29
|
+
var getProvidersFromWindow = require('./getProvidersFromWindow.cjs');
|
|
30
|
+
var sleep = require('./sleep/sleep.cjs');
|
|
31
|
+
var getOrMapViemChain = require('./getOrMapViemChain.cjs');
|
|
32
|
+
var bigIntToHex = require('./bigIntToHex/bigIntToHex.cjs');
|
|
33
|
+
var isBigInt = require('./isBigInt/isBigInt.cjs');
|
|
34
|
+
var retryableFn = require('./retryableFn.cjs');
|
|
29
35
|
|
|
30
36
|
|
|
31
37
|
|
|
@@ -49,15 +55,27 @@ exports.UserHasAccountWithEmailError = UserHasAccountWithEmailError.UserHasAccou
|
|
|
49
55
|
exports.SocialAccountAlreadyExistsError = SocialAccountAlreadyExistsError.SocialAccountAlreadyExistsError;
|
|
50
56
|
exports.CancellablePromise = CancellablePromise.CancellablePromise;
|
|
51
57
|
exports.isFunction = isFunction.isFunction;
|
|
58
|
+
exports.getAndroidVersion = isMobile.getAndroidVersion;
|
|
52
59
|
exports.isAndroid = isMobile.isAndroid;
|
|
53
60
|
exports.isIOS = isMobile.isIOS;
|
|
54
61
|
exports.isIPad = isMobile.isIPad;
|
|
55
62
|
exports.isIPhone = isMobile.isIPhone;
|
|
63
|
+
exports.isIPhone8OrEarlier = isMobile.isIPhone8OrEarlier;
|
|
56
64
|
exports.isLegacySafari = isMobile.isLegacySafari;
|
|
57
65
|
exports.isMobile = isMobile.isMobile;
|
|
58
66
|
exports.isSamsungBrowser = isMobile.isSamsungBrowser;
|
|
67
|
+
exports.isWindows = isMobile.isWindows;
|
|
59
68
|
exports.getItemAsync = localStorageAsync.getItemAsync;
|
|
60
69
|
exports.removeItemAsync = localStorageAsync.removeItemAsync;
|
|
61
70
|
exports.setItemAsync = localStorageAsync.setItemAsync;
|
|
62
71
|
exports.bufferToBase64 = bufferToBase64.bufferToBase64;
|
|
63
72
|
exports.last = last.last;
|
|
73
|
+
exports.getProvidersFromWindow = getProvidersFromWindow.getProvidersFromWindow;
|
|
74
|
+
exports.sleep = sleep.sleep;
|
|
75
|
+
exports.getChain = getOrMapViemChain.getChain;
|
|
76
|
+
exports.getOrMapViemChain = getOrMapViemChain.getOrMapViemChain;
|
|
77
|
+
exports.mapChain = getOrMapViemChain.mapChain;
|
|
78
|
+
exports.bigIntToHex = bigIntToHex.bigIntToHex;
|
|
79
|
+
exports.isBigInt = isBigInt.isBigInt;
|
|
80
|
+
exports.FALLBACK_UNDEFINED = retryableFn.FALLBACK_UNDEFINED;
|
|
81
|
+
exports.retryableFn = retryableFn.retryableFn;
|
package/src/index.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
export * from './parseChainId';
|
|
2
|
-
export * from './parseEvmNetworks';
|
|
3
|
-
export * from './errors';
|
|
4
|
-
export * from './CancellablePromise';
|
|
5
|
-
export * from './isFunction';
|
|
6
|
-
export * from './isMobile';
|
|
7
|
-
export * from './localStorageAsync';
|
|
8
|
-
export * from './bufferToBase64';
|
|
9
|
-
export * from './last';
|
|
1
|
+
export * from './parseChainId';
|
|
2
|
+
export * from './parseEvmNetworks';
|
|
3
|
+
export * from './errors';
|
|
4
|
+
export * from './CancellablePromise';
|
|
5
|
+
export * from './isFunction';
|
|
6
|
+
export * from './isMobile';
|
|
7
|
+
export * from './localStorageAsync';
|
|
8
|
+
export * from './bufferToBase64';
|
|
9
|
+
export * from './last';
|
|
10
|
+
export * from './getProvidersFromWindow';
|
|
11
|
+
export * from './sleep';
|
|
12
|
+
export * from './getOrMapViemChain';
|
|
13
|
+
export * from './bigIntToHex';
|
|
14
|
+
export * from './isBigInt';
|
|
15
|
+
export * from './retryableFn';
|
package/src/index.js
CHANGED
|
@@ -18,7 +18,13 @@ export { UserHasAccountWithEmailError } from './errors/UserHasAccountWithEmailEr
|
|
|
18
18
|
export { SocialAccountAlreadyExistsError } from './errors/SocialAccountAlreadyExistsError.js';
|
|
19
19
|
export { CancellablePromise } from './CancellablePromise/CancellablePromise.js';
|
|
20
20
|
export { isFunction } from './isFunction/isFunction.js';
|
|
21
|
-
export { isAndroid, isIOS, isIPad, isIPhone, isLegacySafari, isMobile, isSamsungBrowser } from './isMobile.js';
|
|
21
|
+
export { getAndroidVersion, isAndroid, isIOS, isIPad, isIPhone, isIPhone8OrEarlier, isLegacySafari, isMobile, isSamsungBrowser, isWindows } from './isMobile.js';
|
|
22
22
|
export { getItemAsync, removeItemAsync, setItemAsync } from './localStorageAsync.js';
|
|
23
23
|
export { bufferToBase64 } from './bufferToBase64.js';
|
|
24
24
|
export { last } from './last.js';
|
|
25
|
+
export { getProvidersFromWindow } from './getProvidersFromWindow.js';
|
|
26
|
+
export { sleep } from './sleep/sleep.js';
|
|
27
|
+
export { getChain, getOrMapViemChain, mapChain } from './getOrMapViemChain.js';
|
|
28
|
+
export { bigIntToHex } from './bigIntToHex/bigIntToHex.js';
|
|
29
|
+
export { isBigInt } from './isBigInt/isBigInt.js';
|
|
30
|
+
export { FALLBACK_UNDEFINED, retryableFn } from './retryableFn.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isBigInt } from './isBigInt';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isBigInt: (value: bigint | unknown) => value is bigint;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { isFunction } from './isFunction';
|
|
1
|
+
export { isFunction } from './isFunction';
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
6
6
|
const isFunction = (value) => typeof value === 'function';
|
|
7
7
|
|
|
8
8
|
exports.isFunction = isFunction;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const isFunction: (value: Function | unknown) => value is Function;
|
|
1
|
+
export declare const isFunction: (value: Function | unknown) => value is Function;
|
package/src/isMobile.cjs
CHANGED
|
@@ -2,68 +2,134 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* Sourced from http://detectmobilebrowsers.com/
|
|
7
|
-
*/
|
|
8
|
-
/* eslint-disable max-len, no-useless-escape */
|
|
9
|
-
const userAgentRegex1 = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i;
|
|
10
|
-
const userAgentRegex2 = /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i;
|
|
11
|
-
// This is a regex that covers both iphone, android and any other mobile device.
|
|
12
|
-
const isMobile = (maxTouchPointsOverride) => {
|
|
13
|
-
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
return (userAgentRegex1.test(navigator.userAgent) ||
|
|
17
|
-
userAgentRegex2.test(navigator.userAgent.substring(0, 4)) ||
|
|
18
|
-
isIPad(maxTouchPointsOverride) ||
|
|
19
|
-
isIPhone());
|
|
20
|
-
};
|
|
21
|
-
const isIPhone = () => typeof window === 'undefined' || typeof navigator === 'undefined'
|
|
22
|
-
? false
|
|
23
|
-
: /iPhone/.test(navigator.userAgent);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Sourced from http://detectmobilebrowsers.com/
|
|
7
|
+
*/
|
|
8
|
+
/* eslint-disable max-len, no-useless-escape */
|
|
9
|
+
const userAgentRegex1 = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i;
|
|
10
|
+
const userAgentRegex2 = /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i;
|
|
11
|
+
// This is a regex that covers both iphone, android and any other mobile device.
|
|
12
|
+
const isMobile = (maxTouchPointsOverride) => {
|
|
13
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return (userAgentRegex1.test(navigator.userAgent) ||
|
|
17
|
+
userAgentRegex2.test(navigator.userAgent.substring(0, 4)) ||
|
|
18
|
+
isIPad(maxTouchPointsOverride) ||
|
|
19
|
+
isIPhone());
|
|
20
|
+
};
|
|
21
|
+
const isIPhone = () => typeof window === 'undefined' || typeof navigator === 'undefined'
|
|
22
|
+
? false
|
|
23
|
+
: /iPhone/.test(navigator.userAgent);
|
|
24
|
+
const isIPhone8OrEarlier = () => typeof window === 'undefined' || typeof navigator === 'undefined'
|
|
25
|
+
? false
|
|
26
|
+
: iPhoneLegacyRegex.some(({ regex }) => regex.test(navigator.userAgent));
|
|
27
|
+
/**
|
|
28
|
+
* @param [optional] maxTouchPointsOverride - this is used for testing since
|
|
29
|
+
* it seems that JSDOM doesn't support maxTouchPoints, so it was impossible
|
|
30
|
+
* to mock it
|
|
31
|
+
*/
|
|
32
|
+
const isIPad = (maxTouchPointsOverride) => {
|
|
33
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
const maxTouchPoints = maxTouchPointsOverride || navigator.maxTouchPoints;
|
|
37
|
+
return (/iPad/.test(navigator.userAgent) ||
|
|
38
|
+
(/Mac/.test(navigator.userAgent) && maxTouchPoints === 5));
|
|
39
|
+
};
|
|
40
|
+
const isIOS = (maxTouchPointsOverride) => {
|
|
41
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return isIPhone() || isIPad(maxTouchPointsOverride);
|
|
45
|
+
};
|
|
46
|
+
const isAndroid = () => isMobile() && !isIOS();
|
|
47
|
+
const isLegacySafari = () => {
|
|
48
|
+
// We need to check if window.CSS exists and if it has supports function.
|
|
49
|
+
// We can use it only in browsers. This prevents customer tests from failing because of that.
|
|
50
|
+
if (!window.CSS || typeof window.CSS.supports !== 'function') {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
// Older versions of Safari <15 are not supporting aspect-ratio css property.
|
|
54
|
+
// We can use that to check if user is on older browser.
|
|
55
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
|
|
56
|
+
const cssValue = 'aspect-ratio: 1 / 1';
|
|
57
|
+
return !CSS.supports(cssValue);
|
|
58
|
+
};
|
|
59
|
+
const isSamsungBrowser = () => {
|
|
60
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return navigator.userAgent.includes('SamsungBrowser');
|
|
64
|
+
};
|
|
65
|
+
const isWindows = () => window.navigator.userAgent.indexOf('Windows') !== -1;
|
|
66
|
+
// regex from: https://github.com/matomo-org/device-detector/blob/master/regexes/device/mobiles.yml
|
|
67
|
+
const iPhoneLegacyRegex = [
|
|
68
|
+
{
|
|
69
|
+
model: 'iPhone 5',
|
|
70
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?5[C,_]12|5)$/,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
model: 'iPhone 5C',
|
|
74
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?5[C,_]34)$/,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
model: 'iPhone 5S',
|
|
78
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?6[C,_]12|5S)$/,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
model: 'iPhone 6 Plus',
|
|
82
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?7[C,_]1|1C2%257enohPi|6PLUS)$/,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
model: 'iPhone 6',
|
|
86
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?7[C,_]2|6)$/,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
model: 'iPhone 6s Plus',
|
|
90
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?8[C,_]2|6SPLUS)$/,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
model: 'iPhone 6s',
|
|
94
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?8[C,_]1|iPhone[ /]6s|6S)$/,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
model: 'iPhone SE',
|
|
98
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?8[C,_]4|SE)$/,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
model: 'iPhone 7',
|
|
102
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?9[C,_]13|iphone7|7)$/,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
model: 'iPhone 7 Plus',
|
|
106
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?9[C,_]24|7PLUS)$/,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
model: 'iPhone 8',
|
|
110
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?10[C,_]14|8)$/,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
model: 'iPhone 8 Plus',
|
|
114
|
+
regex: /(?:MDCR_|ICRU_|Apple-)?(?:iPh(?:one)?10[C,_]25|8PLUS)$/,
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
const getAndroidVersion = () => {
|
|
118
|
+
const androidVersionMatch = navigator.userAgent.match(/Android (\d+(\.\d+)?)/);
|
|
119
|
+
if (!androidVersionMatch) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const androidVersion = parseFloat(androidVersionMatch[1]);
|
|
123
|
+
return androidVersion;
|
|
61
124
|
};
|
|
62
125
|
|
|
126
|
+
exports.getAndroidVersion = getAndroidVersion;
|
|
63
127
|
exports.isAndroid = isAndroid;
|
|
64
128
|
exports.isIOS = isIOS;
|
|
65
129
|
exports.isIPad = isIPad;
|
|
66
130
|
exports.isIPhone = isIPhone;
|
|
131
|
+
exports.isIPhone8OrEarlier = isIPhone8OrEarlier;
|
|
67
132
|
exports.isLegacySafari = isLegacySafari;
|
|
68
133
|
exports.isMobile = isMobile;
|
|
69
134
|
exports.isSamsungBrowser = isSamsungBrowser;
|
|
135
|
+
exports.isWindows = isWindows;
|
package/src/isMobile.d.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sourced from http://detectmobilebrowsers.com/
|
|
3
|
-
*/
|
|
4
|
-
export declare const isMobile: (maxTouchPointsOverride?: number) => boolean;
|
|
5
|
-
export declare const isIPhone: () => boolean;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const
|
|
14
|
-
export declare const
|
|
15
|
-
export declare const
|
|
1
|
+
/**
|
|
2
|
+
* Sourced from http://detectmobilebrowsers.com/
|
|
3
|
+
*/
|
|
4
|
+
export declare const isMobile: (maxTouchPointsOverride?: number) => boolean;
|
|
5
|
+
export declare const isIPhone: () => boolean;
|
|
6
|
+
export declare const isIPhone8OrEarlier: () => boolean;
|
|
7
|
+
/**
|
|
8
|
+
* @param [optional] maxTouchPointsOverride - this is used for testing since
|
|
9
|
+
* it seems that JSDOM doesn't support maxTouchPoints, so it was impossible
|
|
10
|
+
* to mock it
|
|
11
|
+
*/
|
|
12
|
+
export declare const isIPad: (maxTouchPointsOverride?: number) => boolean;
|
|
13
|
+
export declare const isIOS: (maxTouchPointsOverride?: number) => boolean;
|
|
14
|
+
export declare const isAndroid: () => boolean;
|
|
15
|
+
export declare const isLegacySafari: () => boolean;
|
|
16
|
+
export declare const isSamsungBrowser: () => boolean;
|
|
17
|
+
export declare const isWindows: () => boolean;
|
|
18
|
+
export declare const getAndroidVersion: () => number | undefined;
|