domchristie / humps

🐫 Underscore-to-camelCase converter (and vice versa) for strings and object keys in JavaScript.
1.42k stars 100 forks source link

[🐛BUG] Unexpected underscore when using decamelize function #62

Open Styx11 opened 2 years ago

Styx11 commented 2 years ago

Desc

Hey guys, brilliant module! But I ran into a few problems when I used it

here is my test code:

const humps = require('humps')

const formatTestStr = (target, result) => `Test case: ${target}, result: ${result}`

console.log(formatTestStr('Raw_Message', humps.decamelize('Raw_Message')))

console.log(formatTestStr('raw_Message', humps.decamelize('raw_Message')))

console.log(formatTestStr('Raw_message', humps.decamelize('Raw_message')))

console.log(formatTestStr('raw_Message_Another', humps.decamelize('raw_Message_Another')))

and it comes out:

Test case: Raw_Message, result: raw__message // <- oops
Test case: raw_Message, result: raw__message  // <- oops
Test case: Raw_message, result: raw_message
Test case: raw_Message_Another, result: raw__message__another  // <- oops

as you can see, there is an unexpected underscore when target string is an underscore case string and _ is followed by an upper case character

Version

2.0.1

Expect

just turn underscore case string into lower case

Styx11 commented 2 years ago

well then, I think change-case is a better choice