Skip to content

Commit cec25d9

Browse files
committed
feat: linking utils
1 parent 6664b06 commit cec25d9

File tree

6 files changed

+62
-120
lines changed

6 files changed

+62
-120
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ getStatusBarHeight, hasNotch, hasNotchOnly, hasDynamicIsland
134134
| hasDynamicIsland | function | returns if the device has dynamic island |
135135
| getStatusBarHeight | number | returns status bar height of the device |
136136
137+
## Utils Props
138+
139+
| Property | Type | Description |
140+
|--------------------|:------------------------------------:|--------------------------------------------------------|
141+
| openAppInPlayStore | function(appPackageName) | opens the app on android or Google Play |
142+
| openAppInAppStore | function(appStoreId) | opens the app on ios or App Store |
143+
| openExternalApp | function(appPackageName, appStoreId) | opens the app on ios or android - handles the platform |
144+
145+
137146
## Normalize Text Props
138147
139148
| Property | Type | Description |

lib/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ import {
2727
} from "./helpers/notch/Notch";
2828
import normalizeText from "./helpers/normalize/normalizeText";
2929

30+
import {
31+
openExternalApp,
32+
openAppInAppStore,
33+
openAppInPlayStore,
34+
} from "./utils";
35+
3036
import { numberFormat } from "./helpers/text/Text";
3137

3238
export {
@@ -54,4 +60,7 @@ export {
5460
hasDynamicIsland,
5561
numberFormat,
5662
normalizeText,
63+
openExternalApp,
64+
openAppInAppStore,
65+
openAppInPlayStore,
5766
};

lib/utils/index.ts

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,7 @@
1-
export const capitalizeFirstLetter = (str: string) =>
2-
str && str.length ? str.charAt(0).toUpperCase() + str.slice(1) : str;
1+
import {
2+
openExternalApp,
3+
openAppInAppStore,
4+
openAppInPlayStore,
5+
} from "./linking";
36

4-
export const capitalizeAllStartingWords = (
5-
str: string,
6-
lower: boolean = false,
7-
) =>
8-
(lower ? str.toLowerCase() : str).replace(/(?:^|\s|[''({])+\S/g, (match) =>
9-
match.toUpperCase(),
10-
);
11-
12-
export const capitalize = (str: string) =>
13-
str.charAt(0).toUpperCase() + str.slice(1);
14-
15-
export const generateRandomNumber = (min: number, max: number) =>
16-
Math.floor(Math.random() * (max - min + 1) + min);
17-
18-
export const diffArrays = (arr1: any[], arr2: any[]): any[] =>
19-
arr1 &&
20-
arr2 &&
21-
arr1.filter(
22-
(obj1) => !arr2.find((obj2) => obj1.id === obj2.id && obj2.isChecked),
23-
);
24-
25-
export const diffArraysWithId = (arr1: any[], arr2: any[]): any[] =>
26-
arr1 &&
27-
arr2 &&
28-
arr1.filter((obj1) => !arr2.find((obj2) => obj1.id === obj2.id && obj2.id));
29-
30-
export const diffDates = (date: number, date2: number) =>
31-
Math.floor(Math.abs(date - date2) / 86400000);
32-
33-
export const isBlankString = (str: string) =>
34-
!str || str.length === 0 || /^\s*$/.test(str);
35-
36-
export const randomBoolean = () => Math.random() >= 0.5;
37-
export const coinflip = () => Math.random() >= 0.5;
38-
39-
export const isEven = (val: number) => val % 2 === 0;
40-
41-
export const removeAllDuplicateValuesInArray = (arr: any[]) => [
42-
...new Set(arr),
43-
];
44-
45-
export const isArray = (arr: any[]) => Array.isArray(arr);
46-
47-
export const generateRandomString = () => Math.random().toString(36).slice(2);
48-
49-
export const mergeArrays = (a: any[], b: any[]) => [...a, ...b];
50-
51-
export const mergeArraysAndRemoveDuplicates = (a: any[], b: any[]) => [
52-
...new Set([...a, ...b]),
53-
];
54-
55-
export const getTrueType = (obj: any) =>
56-
Object.prototype.toString.call(obj).slice(8, -1).toLocaleLowerCase();
57-
58-
export const isNotEmptyArray = (arr: any[]) =>
59-
Array.isArray(arr) && arr.length > 0;
60-
61-
export const safelyParseJson = (string?: string | null) => {
62-
try {
63-
return JSON.parse(string as string);
64-
} catch {
65-
return string;
66-
}
67-
};
68-
69-
export const isEmptyObj = (obj: any) =>
70-
Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;
71-
72-
export const shuffleArray = (arr: any[]) => arr.sort(() => 0.5 - Math.random());
73-
74-
export const convertSnakeToCamelCase = (str: string) => {
75-
return str.replace(/([-_][a-z])/g, (group) =>
76-
group.toUpperCase().replace("-", "").replace("_", ""),
77-
);
78-
};
79-
80-
export const getRandomHexColor = () =>
81-
`#${Math.floor(Math.random() * 0xffffff)
82-
.toString(16)
83-
.padEnd(6, "0")}`;
84-
85-
export const convertRGBToHexColor = (r: number, g: number, b: number) =>
86-
"#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
87-
88-
export const getMinMaxOfArray = (arr: any[]) => [
89-
Math.min(...arr),
90-
Math.max(...arr),
91-
];
92-
93-
export const sleep = (delay: number): Promise<void> => {
94-
return new Promise((resolve) => setTimeout(resolve, delay));
95-
};
96-
97-
/**
98-
* Create an object from the pairs of key and value
99-
* @param arr
100-
* @returns
101-
*/
102-
export const toObj = (arr: any[]) => Object.fromEntries(arr);
103-
104-
/**
105-
* Get union of arrays
106-
* @param arr
107-
* @returns
108-
*/
109-
export const getUnion = (...arr) => [...new Set(arr.flat())];
110-
111-
/**
112-
* Partition an array based on the criteria/condition
113-
* @param arr
114-
* @param criteria
115-
* @returns
116-
*/
117-
export const partition = (arr: any[], criteria: any) =>
118-
arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
7+
export { openExternalApp, openAppInAppStore, openAppInPlayStore };

lib/utils/linking.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Linking, Platform } from "react-native";
2+
3+
export const openAppInPlayStore = (appPackageName: string) => {
4+
const url = `market://details?id=${appPackageName}`;
5+
Linking.canOpenURL(url)
6+
.then((supported) => {
7+
if (!supported) {
8+
return console.log("Cannot handle URL: " + url);
9+
} else {
10+
return Linking.openURL(url);
11+
}
12+
})
13+
.catch((err) => console.error("An error occurred", err));
14+
};
15+
16+
export const openAppInAppStore = (appStoreId: string) => {
17+
const url = `https://apps.apple.com/app/id${appStoreId}`;
18+
Linking.canOpenURL(url)
19+
.then((supported) => {
20+
if (!supported) {
21+
return console.log("Cannot handle URL: " + url);
22+
} else {
23+
return Linking.openURL(url);
24+
}
25+
})
26+
.catch((err) => console.error("An error occurred", err));
27+
};
28+
29+
export const openExternalApp = (appPackageName: string, appStoreId: string) => {
30+
if (Platform.OS === "ios") {
31+
openAppInAppStore(appStoreId);
32+
} else if (Platform.OS === "android") {
33+
openAppInPlayStore(appPackageName);
34+
}
35+
};

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@freakycoder/react-native-helpers",
3-
"version": "2.2.2",
3+
"version": "2.3.0",
44
"description": "All helpers in one; iPhone series support, dimensions helper, hasNotch helper, normalize text helper and text helpers for React Native with very easy use",
55
"keywords": [
66
"ios",

0 commit comments

Comments
 (0)