Skip to content

Commit

Permalink
Added getAvailableVehicleTypes util function
Browse files Browse the repository at this point in the history
  • Loading branch information
souyahia-monk committed Sep 27, 2024
1 parent 04ea870 commit 3f03301
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
10 changes: 10 additions & 0 deletions packages/common/README/UTILITIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ type of variation to use for the interactive colors (lighten or darken the color

---

# Config Utils
### getAvailableVehicleTypes
```typescript
import { getAvailableVehicleTypes } from '@monkvision/common';

console.log(getAvailableVehicleTypes(config));
// Output : [VehicleType.CITY, VehicleType.SUV]
```
Returns the list of available vehicle types based on the `sights` property of a `CaptureAppConfig` object.

# Environment Utils
### getEnvOrThrow
```typescript
Expand Down
9 changes: 1 addition & 8 deletions packages/common/src/apps/appStateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MonkSearchParam, useMonkSearchParams } from './searchParams';
import { MonkAppState, MonkAppStateContext } from './appState';
import { useAppStateMonitoring } from './monitoring';
import { useAppStateAnalytics } from './analytics';
import { getAvailableVehicleTypes } from '../utils';

/**
* Local storage key used within Monk web applications to store the authentication token.
Expand Down Expand Up @@ -64,14 +65,6 @@ function getSights(
});
}

function getAvailableVehicleTypes(config: CaptureAppConfig): VehicleType[] {
return (
config.enableSteeringWheelPosition
? Object.keys(config.sights.left)
: Object.keys(config.sights)
) as VehicleType[];
}

/**
* A React context provider that declares the state for the common parameters used by Monk applications. The parameters
* are described in the `MonkAppState` interface. Using options available in the App config (`config` prop), this
Expand Down
13 changes: 13 additions & 0 deletions packages/common/src/utils/config.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CaptureAppConfig, VehicleType } from '@monkvision/types';
import { uniq } from './array.utils';

/**
* Util function used to extract the list of available vehicle types in a CaptureAppConfig object.
*/
export function getAvailableVehicleTypes(config: CaptureAppConfig): VehicleType[] {
return (
config.enableSteeringWheelPosition
? uniq([...Object.keys(config.sights.left), ...Object.keys(config.sights.right)])
: Object.keys(config.sights)
) as VehicleType[];
}
1 change: 1 addition & 0 deletions packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './zlib.utils';
export * from './browser.utils';
export * from './env.utils';
export * from './state.utils';
export * from './config.utils';
3 changes: 3 additions & 0 deletions packages/common/test/apps/appStateProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ jest.mock('../../src/apps/searchParams', () => ({
...jest.requireActual('../../src/apps/searchParams'),
useMonkSearchParams: jest.fn(() => ({ get: searchParamsGet })),
}));
jest.mock('../../src/utils', () => ({
getAvailableVehicleTypes: jest.fn(({ sights }) => Object.keys(sights)),
}));

import React, { useContext, useEffect } from 'react';
import { CaptureAppConfig, SteeringWheelPosition, VehicleType } from '@monkvision/types';
Expand Down
40 changes: 40 additions & 0 deletions packages/common/test/utils/config.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CaptureAppConfig, SteeringWheelPosition, VehicleType } from '@monkvision/types';
import { getAvailableVehicleTypes } from '../../src';

describe('Config utils', () => {
describe('getAvailableVehicleTypes function', () => {
it('should return the list of available vehicle types in non-steering wheel sights', () => {
const config = {
enableSteeringWheelPosition: false,
sights: { [VehicleType.SEDAN]: [], [VehicleType.HGV]: [] },
} as unknown as CaptureAppConfig;
expect(getAvailableVehicleTypes(config)).toEqual([VehicleType.SEDAN, VehicleType.HGV]);
});

it('should return the list of available vehicle types in steering wheel enabled sights', () => {
const config = {
enableSteeringWheelPosition: true,
sights: {
[SteeringWheelPosition.LEFT]: { [VehicleType.VAN]: [], [VehicleType.CITY]: [] },
[SteeringWheelPosition.RIGHT]: { [VehicleType.VAN]: [], [VehicleType.CITY]: [] },
},
} as unknown as CaptureAppConfig;
expect(getAvailableVehicleTypes(config)).toEqual([VehicleType.VAN, VehicleType.CITY]);
});

it('should return the list of available vehicle types in steering wheel enabled sights with different sights for each side', () => {
const config = {
enableSteeringWheelPosition: true,
sights: {
[SteeringWheelPosition.LEFT]: { [VehicleType.VAN]: [], [VehicleType.LARGE_SUV]: [] },
[SteeringWheelPosition.RIGHT]: { [VehicleType.VAN]: [], [VehicleType.HATCHBACK]: [] },
},
} as unknown as CaptureAppConfig;
expect(getAvailableVehicleTypes(config)).toEqual([
VehicleType.VAN,
VehicleType.LARGE_SUV,
VehicleType.HATCHBACK,
]);
});
});
});

0 comments on commit 3f03301

Please sign in to comment.