npm.io
1.1.3 • Published 1 year agoCLI

pragi-string

Licence
MIT
Version
1.1.3
Deps
0
Size
18 kB
Vulns
0
Weekly
0

pragiString

A simple utility to transform text into different capitalization styles!

Installation

Install using npm or yarn:

npm install pragi-string

Features

  • Transform text into various cases (title, sentence, camel, snake, kebab, etc.)
  • Smart text truncation with customizable length and suffix
  • Intelligent whitespace trimming
  • Exclude specific words from case transformations
  • Convenient method-based API (e.g., pragiString.titleCase())
  • CLI tool for quick transformations
  • Full TypeScript support

Usage

Importing the Package

JavaScript

const pragiString = require("pragi-string");

TypeScript

import { pragiString } from "pragi-string";
Text Transformations
// Method-based API for better readability
pragiString.titleCase("hello world");  // Hello World
pragiString.camelCase("hello world");  // helloWorld
pragiString.snakeCase("hello world");  // hello_world
Smart Features
Text Truncation
// Truncate with custom length and suffix
pragiString.titleCase("this is a very long text", { 
  truncate: { length: 10, suffix: "..." } 
});
// Output: "This Is..." (truncates before case transformation)

// Custom suffix
pragiString.titleCase("this is a very long text", { 
  truncate: { length: 12, suffix: " [...]" } 
});
// Output: "This Is [...]"
Intelligent Trimming
// Automatically handles various whitespace scenarios
pragiString.titleCase("  hello   world  ", { trim: true });
// Output: "Hello World"

// Trim specific characters
pragiString.titleCase("***hello***world***", { 
  trim: { chars: "*" } 
});
// Output: "Hello World"
Word Exclusions
// Exclude specific words from case transformation
pragiString.titleCase("the quick brown fox", { 
  exclude: ["the", "of", "and"] 
});
// Output: "the Quick Brown Fox"

// Combine with other features
pragiString.titleCase("  the quick brown fox  ", { 
  exclude: ["the"],
  trim: true,
  truncate: { length: 15, suffix: "..." }
});
// Output: "the Quick Brown..."
Number Conversions
// Convert numbers to words
pragiString.toWords(42);  // "forty two"
pragiString.toWords(1234);  // "one thousand two hundred thirty four"

// Convert words to numbers
pragiString.toNumbers("forty two");  // 42
pragiString.toNumbers("one thousand two hundred thirty four");  // 1234

// Convert to ordinal numbers
pragiString.toOrdinal(1);  // "1st"
pragiString.toOrdinal(42);  // "42nd"

// Roman numeral conversions
pragiString.toRoman(42);  // "XLII"
pragiString.fromRoman("XLII");  // 42

// Humanize large numbers
pragiString.humanizeNumber(1234567);  // "1.2M"

// Time and duration formatting
pragiString.humanizeDuration(3665);  // "1 hour, 1 minute, 5 seconds"
pragiString.toDigitalTime(3665);  // "01:01:05"

Options

Trim Whitespace
console.log(pragiString("  hello world  ", "titlecase", { trim: true }));
// Output: "Hello World"
Exclude Words from Capitalization
console.log(pragiString("hello world example", "titlecase", { excludeWords: ["world"] }));
// Output: Hello world Example

CLI Usage

You can use pragiString as a command-line tool after installing it globally:

npm install -g pragi-string
Convert Text
pragi-string "hello world" uppercase
# Output: HELLO WORLD
Trim Before Conversion
pragi-string "   hello world   " titlecase --trim
# Output: Hello World
Exclude Specific Words
pragi-string "hello world example" titlecase --exclude world
# Output: Hello world Example

API Reference

Methods

Each transformation is available as a direct method:

  • pragiString.titleCase(text: string, options?: PragiOptions)
  • pragiString.sentenceCase(text: string, options?: PragiOptions)
  • pragiString.camelCase(text: string, options?: PragiOptions)
  • pragiString.snakeCase(text: string, options?: PragiOptions)
  • pragiString.kebabCase(text: string, options?: PragiOptions)
  • pragiString.upperCase(text: string, options?: PragiOptions)
  • pragiString.lowerCase(text: string, options?: PragiOptions)
Options
interface PragiOptions {
  trim?: boolean | { chars?: string };
  truncate?: {
    length: number;
    suffix?: string;
  };
  exclude?: string[];
}
  • trim: Enable trimming (boolean) or specify characters to trim
  • truncate: Configure text truncation with custom length and suffix
  • exclude: Array of words to exclude from case transformation

License

This package is licensed under the MIT License.


Visit my portfolio: https://pragatheeswaran.vercel.app/

Keywords