Currently, the scenario is that the data coming from the Nodejs backend is in snake_case but the frontend is using camelCase.
I am using snakeize and camelize to convert casing but with a compromise.
Problem
ObjectId _id
using camelize is converted to Id
while I am expecting as id
.
Expectation.
_id
must to converted to id
2
Answers
I think the problem is that the package you’re using removes every underscore he finds and converts the letter after it to upper case.
You just need to check if the first letter is underscore and remove it:
Edit:
Recursive:
It would be hard to discern your architecture or data flow from the statement above.
But I would say your current approach of reformatting the entire data structure to either be snake or camel as unnecessary. unless this data structure is being passed through functions and you have no control how it’s retreated.
Reformatting the data structure requires a recursive loop. your time and complexity will increase as the data structure grows in size and nesting.
I would suggest that you create a function that wraps lo-dash.get and change-case.
Exmaple:
getData(dataStructure, path, case, defaultValue);
Formatting an entire data structure up front can be costly and cause performance issues