Package Exports
- wordizer
- wordizer/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (wordizer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
# Wordizer
Wordizer is a library for pluralizing and singularizing words in both English and Portuguese. It supports dynamic word transformations based on language-specific rules.
## Installation
To install Wordizer, use npm:
```bash
npm install wordizer
```
Usage
Pluralizing and Singularizing Words
Wordizer provides functions to pluralize and singularize words for supported languages. Here is an example of how to use Wordizer with English and Portuguese:
import { pluralize, singularize } from 'wordizer'
// Example of pluralizing and singularizing English words
const wordsEn = {
singular: ['apple', 'banana', 'orange', 'strawberry', 'grape'],
plural: ['apples', 'bananas', 'oranges', 'strawberries', 'grapes'],
}
wordsEn.singular.forEach((word, index) => {
const plural = pluralize(word, 'en')
console.log(`${word} -> ${plural}`) // apple -> apples
})
wordsEn.plural.forEach((word, index) => {
const singular = singularize(word, 'en')
console.log(`${word} -> ${singular}`) // apples -> apple
})
// Example of pluralizing and singularizing Portuguese words
const wordsPt = {
singular: ['maçã', 'banana', 'laranja', 'morango', 'uva'],
plural: ['maçãs', 'bananas', 'laranjas', 'morangos', 'uvas'],
}
wordsPt.singular.forEach((word, index) => {
const plural = pluralize(word, 'pt')
console.log(`${word} -> ${plural}`) // maçã -> maçãs
})
wordsPt.plural.forEach((word, index) => {
const singular = singularize(word, 'pt')
console.log(`${word} -> ${singular}`) // maçãs -> maçã
})
Contributing
Adding New Languages
To add a new language to Wordizer, follow these steps:
- Create a new directory for the language (e.g.,
fr
for French) under the root directory. - Inside the new directory, create a
rules
directory and add the following files:irregular.js
pluralization.js
singularization.js
uncountable.js
- Populate these files with the appropriate rules for the new language.
- Create
index.js
,pluralize.js
, andsingularize.js
files in the new language directory. - Initialize the inflector with the rules in
index.js
:
import createInflector from '../inflector.js'
import {
pluralizationRules,
singularizationRules,
uncountableRules,
irregularRules,
} from './rules'
export const pluralize = createInflector(
pluralizationRules,
uncountableRules,
irregularRules
)
export const singularize = createInflector(
singularizationRules,
uncountableRules,
irregularRules
)
- Update the main
index.js
file to include the new language:
import { pluralize as enPluralize, singularize as enSingularize } from './en'
import { pluralize as ptPluralize, singularize as ptSingularize } from './pt'
// Import the new language
import { pluralize as frPluralize, singularize as frSingularize } from './fr'
export const Wordizer = {
en: {
pluralize: enPluralize,
singularize: enSingularize,
},
pt: {
pluralize: ptPluralize,
singularize: ptSingularize,
},
// Add the new language
fr: {
pluralize: frPluralize,
singularize: frSingularize,
},
}
export const pluralize = (word, lang) => {
return Wordizer[lang].pluralize(word)
}
export const singularize = (word, lang) => {
return Wordizer[lang].singularize(word)
}
Running Tests
To run tests for both English and Portuguese, use the following command:
npm test
Here is an example of the test script (wordizer.test.js
):
import { Wordizer } from './index.js'
const testWords = {
en: {
singular: ['apple', 'banana', 'orange', 'strawberry', 'grape'],
plural: ['apples', 'bananas', 'oranges', 'strawberries', 'grapes'],
},
pt: {
singular: ['maçã', 'banana', 'laranja', 'morango', 'uva'],
plural: ['maçãs', 'bananas', 'laranjas', 'morangos', 'uvas'],
},
}
const runTests = (language, wordizer) => {
const { singular, plural } = testWords[language]
console.log(`\nTesting ${language.toUpperCase()} Pluralization:`)
singular.forEach((word, index) => {
const result = wordizer.pluralize(word)
const expected = plural[index]
const status = result === expected ? '🟢' : '🔴'
console.log(`${status} ${word} -> ${result} (expected: ${expected})`)
})
console.log(`\nTesting ${language.toUpperCase()} Singularization:`)
plural.forEach((word, index) => {
const result = wordizer.singularize(word)
const expected = singular[index]
const status = result === expected ? '🟢' : '🔴'
console.log(`${status} ${word} -> ${result} (expected: ${expected})`)
})
}
runTests('en', Wordizer.en)
runTests('pt', Wordizer.pt)
License
This project is licensed under the ISC License.
Acknowledgements
This package is a fork of the Pluralize and PluralizePTBR libraries. I have refactored it to create Wordizer, enhancing its functionality to support both English and Portuguese with a modular approach for easy addition of new languages.