Skip to content

Commit 486779d

Browse files
Merge pull request #4 from wintondeshong/feature/porting-code-batch3
A few ports from previous projects; Largely wrapping lodash
2 parents 6a925b3 + c05c5cf commit 486779d

15 files changed

Lines changed: 344 additions & 27 deletions

package-lock.json

Lines changed: 7 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
},
1010
"dependencies": {
1111
"axios": "0.19.2",
12-
"immutable": "4.0.0-rc.12"
12+
"immutable": "4.0.0-rc.12",
13+
"lodash": "4.17.15"
1314
},
1415
"description": "Common patterns, functions, etc... used when building react applications",
1516
"devDependencies": {
1617
"@testing-library/jest-dom": "5.5.0",
1718
"@testing-library/react": "10.0.4",
1819
"@types/jest": "25.1.5",
20+
"@types/lodash": "4.14.108",
1921
"@types/node": "13.11.0",
2022
"@types/rosie": "0.0.37",
2123
"jest": "25.5.4",

src/constants/video-resolutions.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const VideoResolutions = [
2+
{
3+
height: 1080,
4+
label: "1080p(FHD)",
5+
ratio: "16:9",
6+
width: 1920,
7+
},
8+
{
9+
height: 1200,
10+
label: "UXGA",
11+
ratio: "4:3",
12+
width: 1600,
13+
},
14+
{
15+
height: 720,
16+
label: "720p(HD)",
17+
ratio: "16:9",
18+
width: 1280,
19+
},
20+
{
21+
height: 600,
22+
label: "SVGA",
23+
ratio: "4:3",
24+
width: 800,
25+
},
26+
{
27+
height: 480,
28+
label: "VGA",
29+
ratio: "4:3",
30+
width: 640,
31+
},
32+
{
33+
height: 360,
34+
label: "360p(nHD)",
35+
ratio: "16:9",
36+
width: 640,
37+
},
38+
{
39+
height: 288,
40+
label: "CIF",
41+
ratio: "4:3",
42+
width: 352,
43+
},
44+
{
45+
height: 240,
46+
label: "QVGA",
47+
ratio: "4:3",
48+
width: 320,
49+
},
50+
{
51+
height: 144,
52+
label: "QCIF",
53+
ratio: "4:3",
54+
width: 176,
55+
},
56+
{
57+
height: 120,
58+
label: "QQVGA",
59+
ratio: "4:3",
60+
width: 160,
61+
},
62+
63+
];
64+
65+
export { VideoResolutions };

src/interfaces/key-value-pair.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface KeyValuePair<TKey, TValue> {
2+
key: TKey;
3+
value: TValue;
4+
}
5+
6+
export { KeyValuePair };

src/tests/mocks/mock-axios.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ interface MockAxios {
6464
// ---------------------------------------------------------
6565

6666
const deleteSuccess = (record?: any, delay?: number) =>
67-
_mockSuccess(mockAxios.delete, record, delay);
67+
_mockSuccess(MockAxios.delete, record, delay);
6868

6969
const getSuccess = (record: any, delay?: number) =>
70-
_mockSuccess(mockAxios.get, record, delay);
70+
_mockSuccess(MockAxios.get, record, delay);
7171

7272
const listSuccess = (records: any[], delay?: number) =>
73-
_mockSuccess(mockAxios.get, records, delay);
73+
_mockSuccess(MockAxios.get, records, delay);
7474

7575
const postSuccess = (record: any, delay?: number) =>
76-
_mockSuccess(mockAxios.post, record, delay);
76+
_mockSuccess(MockAxios.post, record, delay);
7777

7878
const putSuccess = (record: any, delay?: number) =>
79-
_mockSuccess(mockAxios.put, record, delay);
79+
_mockSuccess(MockAxios.put, record, delay);
8080

8181
// #endregion Public Functions
8282

@@ -131,7 +131,7 @@ const _resultObjectToJS = (resultObject: any | any[]): any | any[] => {
131131
// #region Exports
132132
// ---------------------------------------------------------
133133

134-
const mockAxios: MockAxios = {
134+
export const MockAxios: MockAxios = {
135135
delete: axios.delete as AxiosJestMock,
136136
deleteSuccess,
137137
get: axios.get as AxiosJestMock,
@@ -143,6 +143,4 @@ const mockAxios: MockAxios = {
143143
putSuccess,
144144
};
145145

146-
export default mockAxios;
147-
148146
// #endregion Exports

src/types/cancellable-promise.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type CancellablePromise<T = any> = {
2+
cancel: () => void;
3+
promise: Promise<T>;
4+
};
5+
6+
export { CancellablePromise };

src/utilities/collection-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { List } from "immutable";
2+
import _ from "lodash";
23

34
// -----------------------------------------------------------------------------------------
45
// #region Private Methods
@@ -180,12 +181,18 @@ const _replaceElementAt = <T>(
180181
// -----------------------------------------------------------------------------------------
181182

182183
export const CollectionUtils = {
184+
difference: _.difference,
183185
equalsBy: _equalsBy,
186+
first: _.head,
187+
flattenDeep: _.flattenDeep,
184188
hasValues: _hasValues,
185189
isEmpty: _isEmpty,
186190
isNotEmpty: _isNotEmpty,
187191
length: _length,
188192
replaceElementAt: _replaceElementAt,
193+
sample: _.sample,
194+
sampleSize: _.sampleSize,
195+
take: _.take,
189196
};
190197

191198
// #endregion Exports

src/utilities/core-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CollectionUtils } from "./collection-utils";
2+
import _ from "lodash";
23

34
// -----------------------------------------------------------------------------------------
45
// #region Private Methods
@@ -84,10 +85,16 @@ const _timer = (name: string) => {
8485
// -----------------------------------------------------------------------------------------
8586

8687
export const CoreUtils = {
88+
bindAll: _.bindAll,
89+
curry: _.curry,
90+
memoize: _.memoize,
8791
numericEnumToPojo: _numericEnumToPojo,
8892
objectToArray: _objectToArray,
93+
range: _.range,
8994
sleep: _sleep,
95+
throttle: _.throttle,
9096
timer: _timer,
97+
times: _.times,
9198
};
9299

93100
// #endregion Exports
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { EnvironmentUtils } from "./environment-utils";
2+
3+
describe("EnvironmentUtils", () => {
4+
const originalEnv = process.env;
5+
6+
// Save old process.env and replace it after each test
7+
// See https://stackoverflow.com/a/48042799 for reference
8+
beforeEach(() => {
9+
jest.resetModules(); // this is important - it clears the cache
10+
process.env = { ...originalEnv };
11+
delete process.env;
12+
});
13+
14+
afterEach(() => {
15+
process.env = originalEnv;
16+
});
17+
18+
// -----------------------------------------------------------------------------------------
19+
// #region isDevelopment()
20+
// -----------------------------------------------------------------------------------------
21+
22+
describe("isDevelopment()", () => {
23+
test("when process.env.NODE_ENV is set to 'development', it returns true", () => {
24+
// Arrange
25+
const newEnv = { ...originalEnv };
26+
newEnv.NODE_ENV = "development";
27+
process.env = newEnv;
28+
29+
// Act
30+
const result = EnvironmentUtils.isDevelopment();
31+
32+
// Assert
33+
expect(result).toBeTrue();
34+
});
35+
36+
test("when process.env.NODE_ENV is set to 'production', it returns false", () => {
37+
// Arrange
38+
const newEnv = { ...originalEnv };
39+
newEnv.NODE_ENV = "production";
40+
process.env = newEnv;
41+
42+
// Act
43+
const result = EnvironmentUtils.isDevelopment();
44+
45+
// Assert
46+
expect(result).toBeFalse();
47+
});
48+
});
49+
50+
// #endregion isDevelopment()
51+
52+
// -----------------------------------------------------------------------------------------
53+
// #region runIfDevelopment()
54+
// -----------------------------------------------------------------------------------------
55+
56+
describe("runIfDevelopment()", () => {
57+
test("when process.env.NODE_ENV is set to 'development', it runs the function", () => {
58+
// Arrange
59+
const newEnv = { ...originalEnv };
60+
newEnv.NODE_ENV = "development";
61+
process.env = newEnv;
62+
const fn = jest.fn();
63+
64+
// Act
65+
EnvironmentUtils.runIfDevelopment(() => fn());
66+
67+
// Assert
68+
expect(fn).toHaveBeenCalled();
69+
});
70+
71+
test("when process.env.NODE_ENV is set to 'production', it does not run the function", () => {
72+
// Arrange
73+
const newEnv = { ...originalEnv };
74+
newEnv.NODE_ENV = "production";
75+
process.env = newEnv;
76+
const fn = jest.fn();
77+
78+
// Act
79+
EnvironmentUtils.runIfDevelopment(() => fn());
80+
81+
// Assert
82+
expect(fn).not.toHaveBeenCalled();
83+
});
84+
});
85+
86+
// #endregion runIfDevelopment()
87+
});

src/utilities/environment-utils.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// -----------------------------------------------------------------------------------------
2+
// #region Functions
3+
// -----------------------------------------------------------------------------------------
4+
5+
/**
6+
* Function to return whether or not the current environment is development.
7+
*
8+
* @returns {boolean}
9+
*/
10+
const isDevelopment = (): boolean => {
11+
return process.env.NODE_ENV === "development";
12+
};
13+
14+
/**
15+
* Conditionally runs the given function, depending on whether the current environment is development or not.
16+
*
17+
* @param {() => any} fn Function to be run in a development environment only.
18+
*/
19+
const runIfDevelopment = (fn: () => any): void => {
20+
if (!isDevelopment()) {
21+
return;
22+
}
23+
24+
fn();
25+
};
26+
27+
// #endregion Functions
28+
29+
// -----------------------------------------------------------------------------------------
30+
// #region Export
31+
// -----------------------------------------------------------------------------------------
32+
33+
export const EnvironmentUtils = {
34+
isDevelopment,
35+
runIfDevelopment,
36+
};
37+
38+
// #endregion Export

0 commit comments

Comments
 (0)