Skip to content

Commit

Permalink
feat: linking utils
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathChaos committed Nov 30, 2023
1 parent 6664b06 commit cec25d9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 120 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ getStatusBarHeight, hasNotch, hasNotchOnly, hasDynamicIsland
| hasDynamicIsland | function | returns if the device has dynamic island |
| getStatusBarHeight | number | returns status bar height of the device |
## Utils Props
| Property | Type | Description |
|--------------------|:------------------------------------:|--------------------------------------------------------|
| openAppInPlayStore | function(appPackageName) | opens the app on android or Google Play |
| openAppInAppStore | function(appStoreId) | opens the app on ios or App Store |
| openExternalApp | function(appPackageName, appStoreId) | opens the app on ios or android - handles the platform |
## Normalize Text Props
| Property | Type | Description |
Expand Down
9 changes: 9 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import {
} from "./helpers/notch/Notch";
import normalizeText from "./helpers/normalize/normalizeText";

import {
openExternalApp,
openAppInAppStore,
openAppInPlayStore,
} from "./utils";

import { numberFormat } from "./helpers/text/Text";

export {
Expand Down Expand Up @@ -54,4 +60,7 @@ export {
hasDynamicIsland,
numberFormat,
normalizeText,
openExternalApp,
openAppInAppStore,
openAppInPlayStore,
};
123 changes: 6 additions & 117 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,7 @@
export const capitalizeFirstLetter = (str: string) =>
str && str.length ? str.charAt(0).toUpperCase() + str.slice(1) : str;
import {
openExternalApp,
openAppInAppStore,
openAppInPlayStore,
} from "./linking";

export const capitalizeAllStartingWords = (
str: string,
lower: boolean = false,
) =>
(lower ? str.toLowerCase() : str).replace(/(?:^|\s|[''({])+\S/g, (match) =>
match.toUpperCase(),
);

export const capitalize = (str: string) =>
str.charAt(0).toUpperCase() + str.slice(1);

export const generateRandomNumber = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1) + min);

export const diffArrays = (arr1: any[], arr2: any[]): any[] =>
arr1 &&
arr2 &&
arr1.filter(
(obj1) => !arr2.find((obj2) => obj1.id === obj2.id && obj2.isChecked),
);

export const diffArraysWithId = (arr1: any[], arr2: any[]): any[] =>
arr1 &&
arr2 &&
arr1.filter((obj1) => !arr2.find((obj2) => obj1.id === obj2.id && obj2.id));

export const diffDates = (date: number, date2: number) =>
Math.floor(Math.abs(date - date2) / 86400000);

export const isBlankString = (str: string) =>
!str || str.length === 0 || /^\s*$/.test(str);

export const randomBoolean = () => Math.random() >= 0.5;
export const coinflip = () => Math.random() >= 0.5;

export const isEven = (val: number) => val % 2 === 0;

export const removeAllDuplicateValuesInArray = (arr: any[]) => [
...new Set(arr),
];

export const isArray = (arr: any[]) => Array.isArray(arr);

export const generateRandomString = () => Math.random().toString(36).slice(2);

export const mergeArrays = (a: any[], b: any[]) => [...a, ...b];

export const mergeArraysAndRemoveDuplicates = (a: any[], b: any[]) => [
...new Set([...a, ...b]),
];

export const getTrueType = (obj: any) =>
Object.prototype.toString.call(obj).slice(8, -1).toLocaleLowerCase();

export const isNotEmptyArray = (arr: any[]) =>
Array.isArray(arr) && arr.length > 0;

export const safelyParseJson = (string?: string | null) => {
try {
return JSON.parse(string as string);
} catch {
return string;
}
};

export const isEmptyObj = (obj: any) =>
Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

export const shuffleArray = (arr: any[]) => arr.sort(() => 0.5 - Math.random());

export const convertSnakeToCamelCase = (str: string) => {
return str.replace(/([-_][a-z])/g, (group) =>
group.toUpperCase().replace("-", "").replace("_", ""),
);
};

export const getRandomHexColor = () =>
`#${Math.floor(Math.random() * 0xffffff)
.toString(16)
.padEnd(6, "0")}`;

export const convertRGBToHexColor = (r: number, g: number, b: number) =>
"#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

export const getMinMaxOfArray = (arr: any[]) => [
Math.min(...arr),
Math.max(...arr),
];

export const sleep = (delay: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, delay));
};

/**
* Create an object from the pairs of key and value
* @param arr
* @returns
*/
export const toObj = (arr: any[]) => Object.fromEntries(arr);

/**
* Get union of arrays
* @param arr
* @returns
*/
export const getUnion = (...arr) => [...new Set(arr.flat())];

/**
* Partition an array based on the criteria/condition
* @param arr
* @param criteria
* @returns
*/
export const partition = (arr: any[], criteria: any) =>
arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
export { openExternalApp, openAppInAppStore, openAppInPlayStore };
35 changes: 35 additions & 0 deletions lib/utils/linking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Linking, Platform } from "react-native";

export const openAppInPlayStore = (appPackageName: string) => {
const url = `market://details?id=${appPackageName}`;
Linking.canOpenURL(url)
.then((supported) => {
if (!supported) {
return console.log("Cannot handle URL: " + url);
} else {
return Linking.openURL(url);
}
})
.catch((err) => console.error("An error occurred", err));
};

export const openAppInAppStore = (appStoreId: string) => {
const url = `https://apps.apple.com/app/id${appStoreId}`;
Linking.canOpenURL(url)
.then((supported) => {
if (!supported) {
return console.log("Cannot handle URL: " + url);
} else {
return Linking.openURL(url);
}
})
.catch((err) => console.error("An error occurred", err));
};

export const openExternalApp = (appPackageName: string, appStoreId: string) => {
if (Platform.OS === "ios") {
openAppInAppStore(appStoreId);
} else if (Platform.OS === "android") {
openAppInPlayStore(appPackageName);
}
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freakycoder/react-native-helpers",
"version": "2.2.2",
"version": "2.3.0",
"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",
"keywords": [
"ios",
Expand Down

0 comments on commit cec25d9

Please sign in to comment.