Skip to content

Instantly share code, notes, and snippets.

@kleberksms
Created September 4, 2020 09:23
Show Gist options
  • Select an option

  • Save kleberksms/1ccb5919ec0c78c66cb4080ab1bd0d85 to your computer and use it in GitHub Desktop.

Select an option

Save kleberksms/1ccb5919ec0c78c66cb4080ab1bd0d85 to your computer and use it in GitHub Desktop.
export class Inflector {
static pluralize(word: string): string {
if (!word) {
return;
}
const isPluralAlready = word.endsWith('ies') || word.endsWith('es') || (!word.endsWith('us') && word.endsWith('s'));
if (isPluralAlready) {
return word;
}
if (word.endsWith('y') && !word.endsWith('ay')) {
return `${word.substring(0, word.length - 1)}ies`;
}
return word.endsWith('us') ? `${word}es` : `${word}s`;
}
}
@Vulwsztyn
Copy link

const isPluralAlready = word.endsWith('s') && !word.endsWith('us')

@kleberksms
Copy link
Author

const isPluralAlready = word.endsWith('s') && !word.endsWith('us')

Thank you 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment