@applicaster/zapp-react-native-utils 12.0.0-rc.129 → 12.0.0-rc.130

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.
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable unused-imports/no-unused-vars */
2
2
  import * as R from "ramda";
3
+ import { invariant } from "@applicaster/zapp-react-native-utils/errorUtils";
3
4
 
4
5
  /**
5
6
  * shifts an array by the given offset. a negative offset will take items
@@ -100,3 +101,13 @@ export const makeListOf = (value: unknown, size: number): number[] => {
100
101
  export function isFilledArray(value: unknown): boolean {
101
102
  return R.is(Array, value) && R.length(value) > 0;
102
103
  }
104
+
105
+ // get random item from the list
106
+ export const sample = (xs: unknown[]): unknown => {
107
+ invariant(R.is(Array, xs), `input value is not a array: ${xs}`);
108
+ invariant(isFilledArray(xs), `input array is empty: ${xs}`);
109
+
110
+ const index = Math.floor(Math.random() * xs.length);
111
+
112
+ return xs[index];
113
+ };
@@ -0,0 +1,66 @@
1
+ import { toPositiveNumberWithDefault } from "..";
2
+
3
+ describe("toNumber", () => {
4
+ const DEFAULT = 5;
5
+
6
+ it("return number if input is positive number", () => {
7
+ const inputs = [0, 1, 2, 100, 200.12, Infinity];
8
+ expect.assertions(inputs.length);
9
+
10
+ inputs.forEach((input) => {
11
+ const output = toPositiveNumberWithDefault(DEFAULT, input);
12
+ expect(output).toBe(input);
13
+ });
14
+ });
15
+
16
+ it("return number if input is positive number as string", () => {
17
+ const inputs = ["0", "1", "2", "100", "200.12"];
18
+ expect.assertions(inputs.length);
19
+
20
+ inputs.forEach((input) => {
21
+ const output = toPositiveNumberWithDefault(DEFAULT, input);
22
+ expect(output).toBe(Number(input));
23
+ });
24
+ });
25
+
26
+ it("return default number if input is negative number", () => {
27
+ const inputs = [-1, -2, -100, -Infinity];
28
+ expect.assertions(inputs.length);
29
+
30
+ inputs.forEach((input) => {
31
+ const output = toPositiveNumberWithDefault(DEFAULT, input);
32
+ expect(output).toBe(DEFAULT);
33
+ });
34
+ });
35
+
36
+ it("return default number if input is negative number as string", () => {
37
+ const inputs = ["-1", "-2", "-100"];
38
+ expect.assertions(inputs.length);
39
+
40
+ inputs.forEach((input) => {
41
+ const output = toPositiveNumberWithDefault(DEFAULT, input);
42
+ expect(output).toBe(DEFAULT);
43
+ });
44
+ });
45
+
46
+ it("return default if input is not a number or infinite", () => {
47
+ const inputs = [
48
+ "vfdvf",
49
+ null,
50
+ undefined,
51
+ NaN,
52
+ "",
53
+ {},
54
+ { test: 1 },
55
+ [],
56
+ [1],
57
+ ];
58
+
59
+ expect.assertions(inputs.length);
60
+
61
+ inputs.forEach((input) => {
62
+ const output = toPositiveNumberWithDefault(DEFAULT, input);
63
+ expect(output).toBe(DEFAULT);
64
+ });
65
+ });
66
+ });
@@ -0,0 +1,66 @@
1
+ import { toPositiveNumberWithDefaultZero } from "..";
2
+
3
+ const DEFAULT = 0;
4
+
5
+ describe("toNumber", () => {
6
+ it("return number if input is positive number", () => {
7
+ const inputs = [0, 1, 2, 100, 200.12];
8
+ expect.assertions(inputs.length);
9
+
10
+ inputs.forEach((input) => {
11
+ const output = toPositiveNumberWithDefaultZero(input);
12
+ expect(output).toBe(input);
13
+ });
14
+ });
15
+
16
+ it("return number if input is positive number as string", () => {
17
+ const inputs = ["0", "1", "2", "100", "200.12"];
18
+ expect.assertions(inputs.length);
19
+
20
+ inputs.forEach((input) => {
21
+ const output = toPositiveNumberWithDefaultZero(input);
22
+ expect(output).toBe(Number(input));
23
+ });
24
+ });
25
+
26
+ it("return default number if input is negative number", () => {
27
+ const inputs = [-1, -2, -100];
28
+ expect.assertions(inputs.length);
29
+
30
+ inputs.forEach((input) => {
31
+ const output = toPositiveNumberWithDefaultZero(input);
32
+ expect(output).toBe(DEFAULT);
33
+ });
34
+ });
35
+
36
+ it("return default number if input is negative number as string", () => {
37
+ const inputs = ["-1", "-2", "-100"];
38
+ expect.assertions(inputs.length);
39
+
40
+ inputs.forEach((input) => {
41
+ const output = toPositiveNumberWithDefaultZero(input);
42
+ expect(output).toBe(DEFAULT);
43
+ });
44
+ });
45
+
46
+ it("return default if input is not a number or infinite", () => {
47
+ const inputs = [
48
+ "vfdvf",
49
+ null,
50
+ undefined,
51
+ NaN,
52
+ "",
53
+ {},
54
+ { test: 1 },
55
+ [],
56
+ [1],
57
+ ];
58
+
59
+ expect.assertions(inputs.length);
60
+
61
+ inputs.forEach((input) => {
62
+ const output = toPositiveNumberWithDefaultZero(input);
63
+ expect(output).toBe(DEFAULT);
64
+ });
65
+ });
66
+ });
@@ -66,3 +66,23 @@ export const isZero = (value: unknown): boolean =>
66
66
  export const isMinusZero = (value: number) => {
67
67
  return 1 / value === -Infinity;
68
68
  };
69
+
70
+ export const toPositiveNumberWithDefault = (
71
+ defaultValue: number,
72
+ value: unknown
73
+ ): number => {
74
+ const possibleNumber = toNumber(value);
75
+
76
+ if (R.isNil(possibleNumber)) {
77
+ return defaultValue;
78
+ }
79
+
80
+ if (possibleNumber < 0) {
81
+ return defaultValue;
82
+ }
83
+
84
+ return possibleNumber;
85
+ };
86
+
87
+ export const toPositiveNumberWithDefaultZero = (value: unknown): number =>
88
+ toPositiveNumberWithDefault(0, value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "12.0.0-rc.129",
3
+ "version": "12.0.0-rc.130",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "homepage": "https://github.com/applicaster/quickbrick#readme",
30
30
  "dependencies": {
31
- "@applicaster/applicaster-types": "12.0.0-rc.129",
31
+ "@applicaster/applicaster-types": "12.0.0-rc.130",
32
32
  "buffer": "^5.2.1",
33
33
  "camelize": "^1.0.0",
34
34
  "create-react-context": "^0.2.3",
@@ -60,13 +60,13 @@ const wrapper = ({ children }) => (
60
60
  describe("useCellClick", () => {
61
61
  it("returns a function", () => {
62
62
  const { result } = renderHook(() => useCellClick(mockProps));
63
- expect(typeof result.current).toBe("function");
63
+ expect(typeof result.current.current).toBe("function");
64
64
  cleanup();
65
65
  });
66
66
 
67
67
  it("runs default logic", () => {
68
68
  const { result } = renderHook(() => useCellClick(mockProps));
69
- const useCellClickHook = result.current;
69
+ const useCellClickHook = result.current.current;
70
70
 
71
71
  useCellClickHook();
72
72
 
@@ -76,7 +76,7 @@ describe("useCellClick", () => {
76
76
 
77
77
  it("runs onCellTap", async () => {
78
78
  const { result } = renderHook(() => useCellClick(mockProps), { wrapper });
79
- const useCellClickHook = result.current;
79
+ const useCellClickHook = result.current.current;
80
80
 
81
81
  useCellClickHook();
82
82
 
@@ -30,7 +30,15 @@ type Props = {
30
30
  zappPipesData?: ZappPipesData;
31
31
  };
32
32
 
33
- export const useCellClick = ({ component, zappPipesData, item }: Props) => {
33
+ type onPressReturnFn =
34
+ | ((_item?: ZappEntry | any, _index?: number) => Promise<void>)
35
+ | (() => void);
36
+
37
+ export const useCellClick = ({
38
+ component,
39
+ zappPipesData,
40
+ item,
41
+ }: Props): React.MutableRefObject<onPressReturnFn> => {
34
42
  const { push, currentRoute } = useNavigation();
35
43
  const { pathname } = useRoute();
36
44
 
@@ -106,9 +114,15 @@ export const useCellClick = ({ component, zappPipesData, item }: Props) => {
106
114
  ]
107
115
  );
108
116
 
117
+ const onPressRef = React.useRef<onPressReturnFn>();
118
+
109
119
  if (!cellSelectable) {
110
- return noop;
120
+ // update ref with dummy function
121
+ onPressRef.current = noop;
122
+ } else {
123
+ // update ref with the latest onPress function
124
+ onPressRef.current = onPress;
111
125
  }
112
126
 
113
- return onPress;
127
+ return onPressRef;
114
128
  };