Skip to content

Commit

Permalink
Merge pull request #218 from UN-OCHA/HPC-10029
Browse files Browse the repository at this point in the history
HPC-10029: Optimize `toCamelCase`
  • Loading branch information
Pl217 authored Feb 12, 2025
2 parents 9907e74 + 416120f commit c0a0f51
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
4 changes: 4 additions & 0 deletions bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ if [ $KEEP -eq 0 ]; then
fi

yarn jest "$COMMAND_ARGS" $FORCE_STOP_JEST
TEST_EXIT_CODE=$?

if [ $KEEP -eq 0 ]; then
# Stop Docker containers
echo 'Stopping docker containers'
moveToTestDir
docker compose down
fi

# Pass through the exit code of the unit tests
exit $TEST_EXIT_CODE
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unocha/hpc-api-core",
"version": "10.10.0",
"version": "10.10.1",
"description": "Core libraries supporting HPC.Tools API Backend",
"license": "Apache-2.0",
"private": false,
Expand Down
13 changes: 10 additions & 3 deletions src/lib/data/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ const getDisaggregations = ({

const disaggregatedCategories = disaggregated.categories;
const disaggregatedLocations = disaggregated.locations;
const camelCaseMap = new Map<string, string>();

const disaggregationData: ExtendedDisaggregationData = [];

Expand Down Expand Up @@ -519,10 +520,16 @@ const getDisaggregations = ({

const value = cleanNumberVal(dmCell);

let metricName = metric.name;
if (specificMetricTypes?.length) {
if (!camelCaseMap.has(metric.name)) {
camelCaseMap.set(metric.name, toCamelCase(metric.name));
}
metricName = camelCaseMap.get(metric.name) ?? 'N/A';
}

dataMatrix.push({
metricType: !specificMetricTypes?.length
? metric.type
: toCamelCase(metric.name),
metricType: !specificMetricTypes?.length ? metric.type : metricName,
lIndex,
cIndex,
value: value !== null && !Number.isNaN(value) ? value : null,
Expand Down
28 changes: 10 additions & 18 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,13 @@ export const cleanNumberVal = (value: number | string | null): number | null =>
? parseFloat(value.trim().replaceAll(',', ''))
: null;

export const toCamelCase = (originalString: string) => {
let isNextLetterUppercase = false;
let convertedString = '';

for (const char of originalString) {
if (char === ' ') {
isNextLetterUppercase = true;
continue;
}

convertedString += isNextLetterUppercase
? char.toUpperCase()
: char.toLowerCase();
isNextLetterUppercase = false;
}

return convertedString;
};
export const toCamelCase = (originalString: string) =>
originalString
.trim()
.split(' ')
.map((word, index) =>
index === 0
? word.toLowerCase()
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join('');
8 changes: 8 additions & 0 deletions tests/utils/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { toCamelCase } from '../../src/util';

describe('Test utility functions', () => {
it('should convert string to camel case', () => {
expect(toCamelCase('')).toBe('');
expect(toCamelCase('Reached')).toBe('reached');
expect(toCamelCase('Cumulative reach')).toBe('cumulativeReach');
expect(toCamelCase('Double space')).toBe('doubleSpace');
expect(toCamelCase(' leading space')).toBe('leadingSpace');
expect(toCamelCase('trailing space ')).toBe('trailingSpace');
expect(toCamelCase(' both ends ')).toBe('bothEnds');
expect(toCamelCase('with three words')).toBe('withThreeWords');
expect(toCamelCase('Mixed CASE')).toBe('mixedCase');
expect(toCamelCase('Another Test CASE')).toBe('anotherTestCase');
expect(toCamelCase('version 1.0')).toBe('version1.0');
expect(toCamelCase('2fast 2furious')).toBe('2fast2furious');
});
});

0 comments on commit c0a0f51

Please sign in to comment.