fix(functions): insert underscore between a digit and a capital letter in idToSnakeCaseFast#1416
Open
sarmah-rup wants to merge 1 commit into
Open
Conversation
…akeCaseFast idToSnakeCaseFast only set needUnderscore after a latin letter, so an identifier like field1Name became field1name instead of field1_name. The reference idToSnakeCaseRegex uses a [a-zA-Z0-9] lookbehind and includes digits, so the two diverged on digit boundaries. Include the digit range in needUnderscore using the existing zeroCode and nineCode constants, and add regression tests co-validating the fast and regex implementations.
Contributor
|
Hey @sarmah-rup, thank you for the PR! If we merge it, would it mean that columns containing digits will be renamed? May be too disruptive |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
idToSnakeCaseFastinlibs/functions/src/lib/strings.tsdoes not insert an underscore between a digit and a following capital letter. An id likefield1Namebecomesfield1nameinstead offield1_name.Why I hit this
I run events into a warehouse through Jitsu and noticed some of my columns came out mis-named. Properties like
field1Name,address2Lineandplan9FromOuterSpacelanded asfield1name,address2lineandplan9from_outer_spacerather than the snake_case I expected. That breaks downstream models that referencefield1_name.Root cause
In the loop,
needUnderscoreis only turned on after a latin letter, so the digit to capital boundary is missed:The comment even says "only between latin letters", but the canonical reference implementation
idToSnakeCaseRegexinlibs/core-functions-lib/src/functions/lib/strings.tsuses a(?<=[a-zA-Z0-9])lookbehind, which includes digits. So the fast and regex versions disagree exactly on digit boundaries.Fix
Include the digit range in
needUnderscore, reusing thezeroCode/nineCodeconstants already declared at the top of the file:Tests
Added regression cases to
libs/core-functions-lib/__tests__/strings.test.tsasserting the corrected output (field1Namebecomesfield1_name,address2Linebecomesaddress2_line,plan9FromOuterSpacebecomesplan9_from_outer_space) and thatidToSnakeCaseFastnow matchesidToSnakeCaseRegex. The two new tests fail onnewjitsubefore the change and pass after. The fullcore-functions-libsuite is green at 42 tests.