@pcg/text-kit
v1.0.0
Published
Helps you transform text and words programmatically
Readme
@deeepvision/textkit
@deeepvision/textkit is a text transformation and manipulation library that provides a collection of utility functions to help you work with strings effortlessly. This package is designed to simplify the process of changing cases, declination, pluralization, and more.
Features
- Case conversion (PascalCase, camelCase, param-case, snake_case, CONSTANT_CASE)
- Sentence transformation (to sentence, upper cased sentence, lower cased sentence)
- Abbreviation generation
- Declination based on number
- Pluralization and singularization
Installation
Install the package using npm or yarn:
npm install @deeepvision/textkitUsage
Import and use the desired functions from the package:
import {
ucfirst,
pascalCase,
camelCase,
// ...
decline,
singularToPlural,
pluralToSingular,
} from '@deeepvision/textkit';
// Change cases
const upperFirstChar = ucfirst('hello');
const pascal = pascalCase('hello world');
const camel = camelCase('hello world');
// ...
// Declination
const declinedNoun = decline('apple', 3);
// Pluralization
const plural = singularToPlural('cat');
const singular = pluralToSingular('cats');For a comprehensive list of available functions, please refer to the feature list provided in the package description.
API
Case conversion
ucfirst(string: string): string
Convert a string to string with the first character in uppercase.
ucfirst('hello world'); // Hello world
ucfirst('HELLO WORLD'); // Hello worldlcfirst(string: string): string
Convert a string to string with the first character in lower case.
lcfirst('Hello world'); // hello worldpascalCase(string: string): string
Convert a string to PascalCase.
pascalCase('simple') //=> "Simple"
pascalCase('some.dot.case') //=> "SomeDotCase"
pascalCase('some-param-case') //=> "SomeParamCase"
pascalCase('some spaced case') //=> "SomeSpacedCase"
pascalCase('someCamelCase')) //=> "SomeCamelCase"
pascalCase('SomePascalCase') //=> "SomePascalCase"camelCase(string: string): string
Convert a string to camelCase.
camelCase('simple') //=> "simple"
camelCase('some.dot.case') //=> "someDotCase"
camelCase('some-param-case') //=> "someParamCase"
camelCase('some spaced case') //=> "someSpacedCase"
camelCase('someCamelCase')) //=> "someCamelCase"
camelCase('SomePascalCase') //=> "somePascalCase"paramCase(string: string): string - Convert a string to param-case.
paramCase('simple') //=> "simple"
paramCase('some.dot.case') //=> "some-dot-case"
paramCase('some-param-case') //=> "some-param-case"
paramCase('some spaced case') //=> "some-spaced-case"
paramCase('someCamelCase')) //=> "some-camel-case"
paramCase('SomePascalCase') //=> "some-pascal-case"snakeCase(string: string): string
Convert a string to snake_case.
camelCase('simple') //=> "simple"
camelCase('some.dot.case') //=> "some_dot_case"
camelCase('some-param-case') //=> "some_param_case"
camelCase('some spaced case') //=> "some_spaced_case"
camelCase('someCamelCase')) //=> "some_camel_case"
camelCase('SomePascalCase') //=> "some_pascal_case"constantCase(string: string): string
Convert a string to CONSTANT_CASE.
constantCase('foo bar'); //=> 'FOO_BAR'
constantCase('foo_bar'); //=> 'FOO_BAR'
constantCase('foo-bar'); //=> 'FOO_BAR'
constantCase('foo.bar'); //=> 'FOO_BAR'
constantCase('fooBar'); //=> 'FOO_BAR'
constantCase('FooBar'); //=> 'FOO_BAR'Sentence transformation
toSentence(string: string): string
Convert a string to sentence.
toSentence('foo bar'); //=> 'Foo bar'
toSentence('foo_bar'); //=> 'Foo bar'
toSentence('foo-bar'); //=> 'Foo bar'
toSentence('foo.bar'); //=> 'Foo bar'
toSentence('fooBar'); //=> 'Foo bar'
toSentence('FooBar'); //=> 'Foo bar'
toSentence('foo bar baz'); //=> 'Foo bar baz'toUcSentence(string: string): string
Convert a string to upper cased sentence.
toUcSentence('foo bar'); //=> 'Foo Bar'
toUcSentence('foo_bar'); //=> 'Foo Bar'
toUcSentence('foo-bar'); //=> 'Foo Bar'
toUcSentence('foo.bar'); //=> 'Foo Bar'
toUcSentence('fooBar'); //=> 'Foo Bar'
toUcSentence('FooBar'); //=> 'Foo Bar'
toUcSentence('foo bar baz'); //=> 'Foo Bar Baz'toLcSentence(string: string): string
Convert a string to lower cased sentence.
toLcSentence('foo bar'); //=> 'foo bar'
toLcSentence('foo_bar'); //=> 'foo bar'
toLcSentence('foo-bar'); //=> 'foo bar'
toLcSentence('foo.bar'); //=> 'foo bar'
toLcSentence('fooBar'); //=> 'foo bar'
toLcSentence('FooBar'); //=> 'foo bar'
toLcSentence('Foo bar baz'); //=> 'foo bar baz'SQL alias and abbreviation
toSqlAlias(string: string): string
Convert a table name to SQL alias.
toSqlAlias('foo bar'); //=> 'fb'
toSqlAlias('book'); //=> 'b'
toSqlAlias('article-category'); //=> 'ac'toAbbreviation(string: string): string
Convert a string to abbreviation.
toAbbreviation('Deep Vision'); //=> 'DV'
toAbbreviation('Рожеві окуляри', {
lang: 'uk',
}); //=> 'RO'
toAbbreviation('Удивительные факты', {
lang: 'ru'
}); //=> 'UF'
toAbbreviation('The very long title that can be shorter but I love long titles', {
maxLength: 3
}); //=> 'TVL'
toAbbreviation('Vision'); //=> 'VSN'
toAbbreviation('Deep Vision', {
iteration: 1,
}); //=> 'DV1'Declination
decline(noun: string, number: number): string
Decline a noun based on a number.
const words = ['элемент', 'элемента', 'элементов'];
decline(0, words, false) // => 'элементов'
decline(24, words, false) // => 'элемента'
decline(21, words, false) // => 'элемент'Pluralization
singularToPlural(singular: string): string
Change the plural form of a word to its singular form.
singularToPlural('book'); //=> 'books'pluralToSingular(plural: string): string
Change the singular form of a word to its plural form.
pluralToSingular('books'); //=> 'book'