Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pascal): struggles with camel-cased strings #178

Merged
merged 7 commits into from
Aug 16, 2024

Conversation

MarlonPassos-git
Copy link
Contributor

@MarlonPassos-git MarlonPassos-git commented Aug 16, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

Fix #176.

Performance details

I changed the code suggestion because it degraded performance a bit. After spending some time playing with regular expressions, So far, the best performance I've achieved.

togle performance
original code (with error) image
code sujestion image
pr code image

other implementations that had worse performance

export function pascal(str: string): string {
  if (!str) return "";
  
  return str
    .replace(/([a-z])([A-Z])/g, '$1 $2')
    .split(/[\.\-\s_]/)
    .map(
      (value) => value.charAt(0).toUpperCase() + value.slice(1).toLowerCase()
    )
    .join("");
}

export function pascal(str: string): string {
  if (!str) return "";
  
  let result = "";
  let capitalize = true;

  for (let i = 0; i < str.length; i++) {
    const char = str[i];
    if (/[\.\-\s_]/.test(char)) {
      capitalize = true;
    } else if (capitalize || (i > 0 && /[a-z]/.test(str[i-1]) && /[A-Z]/.test(char))) {
      result += char.toUpperCase();
      capitalize = false;
    } else {
      result += char.toLowerCase();
    }
  }

  return result;
}

Related issue, if any:

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1 Difference (%)
M src/string/pascal.ts 201 +31 (+18%)

Footnotes

  1. Function size includes the import dependencies of the function.

@radashi-bot
Copy link

radashi-bot commented Aug 16, 2024

Benchmark Results

Name Current Baseline Change
pascal ▶︎ with valid input 1,033,812.93 ops/sec ±0.42% 1,474,986.26 ops/sec ±0.4% 🔗 🐢 -29.91%
pascal ▶︎ with camelCase input 2,145,898.24 ops/sec ±0.32% 2,116,697.39 ops/sec ±0.44% 🔗 🚀 +1.38%
pascal ▶︎ with non alphanumerics 606,438.59 ops/sec ±2.99% 1,018,684.19 ops/sec ±0.17% 🔗 🐢 -40.47%

Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure.

@aleclarson aleclarson merged commit a1c8822 into radashi-org:main Aug 16, 2024
7 checks passed
@aleclarson
Copy link
Member

Great work, @MarlonPassos-git 🥳

Copy link

A new beta version 12.2.0-beta.af825f4 has been published to NPM. 🚀

To install:

pnpm add radashi@12.2.0-beta.af825f4

The radashi@beta tag also includes this PR.

See the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pascal struggles with camel-cased strings
3 participants