gustavoguichard / string-ts

Strongly typed string functions
MIT License
1.19k stars 17 forks source link

Handling of apostrophe in titleCase does not align with writing standards #122

Closed paradoxloop closed 10 months ago

paradoxloop commented 11 months ago

Great little library!

Probably slightly obscure bug but the handling of an apostrophe in a string with titleCase does not align with expectations or common writing style guides (AMA, AP, MLA etc) since this library adds a space on either side regardless of whether it is a special case or not.

Across all the title case style guides (overview here) the implementation is not to capitalise after ' expect in special cases but never to add spaces.

Normal: string-ts: titleCase('harry's house) -> Harry ' S House suggested implementation: titleCase('harry's house') => Harry's House

Special Case: string-ts: titleCase('peter o'connely') -> Peter O ' Connely suggested implementation: titleCase('peter o'connely') => Peter O'Connely

p9f commented 11 months ago

Hi @paradoxloop, thanks for the report!

I fear we will never be able to meet the common title case style guides. For example, style guides (such as the one you linked to) would want This is a title to convert to This Is a Title (lowercased a), etc. (and I am not talking about other languages). So maybe there is more value in keeping the contract simple (it's words, being capitalized) rather than trying to pursue the impossible?

Note that you also can easily implement "improvements" to the titleCase function in your app using the primitives exposed by string-ts, like:

import { titleCase, replaceAll } from "string-ts";

const betterTitleCase = <const T extends string>(str: T) =>
  replaceAll(replaceAll(titleCase(str), " ' ", "'"), " , ", ", ");

const res = betterTitleCase("hello world, O'maha");
//    ^?  const res: "Hello World, O'Maha"

@gustavoguichard, any opinion on that?

gustavoguichard commented 11 months ago

@p9f I think you nailed the answer, I'd indeed rather keep the library simple and compose with the existing utilities like you did ☺️

gustavoguichard commented 10 months ago

string-ts@2.0.0 was released and should deal with apostrophes by suppressing them as discussed.