Skip to the content.

Upper Case First

NPM version NPM downloads Bundle size License: MIT TypeScript

Transform text by making the first character uppercase while preserving the rest.

πŸš€ Features

πŸ“¦ Installation

# npm
npm install text-upper-case-first

# yarn
yarn add text-upper-case-first

# pnpm
pnpm add text-upper-case-first

# bun
bun add text-upper-case-first

🎯 Quick Start

import { upperCaseFirst } from "text-upper-case-first";

console.log(upperCaseFirst("hello world")); // "Hello world"
console.log(upperCaseFirst("HELLO WORLD")); // "HELLO WORLD"
console.log(upperCaseFirst("camelCase")); // "CamelCase"

πŸ“– Usage

import { upperCaseFirst } from "text-upper-case-first";

console.log(upperCaseFirst("hello")); // "Hello"

CommonJS

const { upperCaseFirst } = require("text-upper-case-first");

console.log(upperCaseFirst("hello")); // "Hello"

TypeScript

import { upperCaseFirst } from "text-upper-case-first";

const result: string = upperCaseFirst("hello world");
console.log(result); // "Hello world"

πŸ”„ Transformation Examples

Basic Transformations

import { upperCaseFirst } from "text-upper-case-first";

// Simple cases
upperCaseFirst("hello"); // "Hello"
upperCaseFirst("HELLO"); // "HELLO"
upperCaseFirst("Hello"); // "Hello"

// Multiple words
upperCaseFirst("hello world"); // "Hello world"
upperCaseFirst("HELLO WORLD"); // "HELLO WORLD"
upperCaseFirst("Hello World"); // "Hello World"

// Programming cases
upperCaseFirst("camelCase"); // "CamelCase"
upperCaseFirst("pascalCase"); // "PascalCase"
upperCaseFirst("snake_case"); // "Snake_case"
upperCaseFirst("kebab-case"); // "Kebab-case"

Edge Cases

import { upperCaseFirst } from "text-upper-case-first";

// Empty and single character
upperCaseFirst(""); // ""
upperCaseFirst("a"); // "A"
upperCaseFirst("A"); // "A"

// Numbers and symbols
upperCaseFirst("123hello"); // "123hello"
upperCaseFirst("@hello"); // "@hello"
upperCaseFirst("hello123"); // "Hello123"

// Unicode characters
upperCaseFirst("Γ±ice"); // "Γ‘ice"
upperCaseFirst("über"); // "Über"
upperCaseFirst("cafΓ©"); // "CafΓ©"

🌍 Real-World Examples

Sentence Capitalization

import { upperCaseFirst } from "text-upper-case-first";

// Capitalize sentences
upperCaseFirst("this is a sentence."); // "This is a sentence."
upperCaseFirst("welcome to our app"); // "Welcome to our app"
upperCaseFirst("error: invalid input"); // "Error: invalid input"

Name Formatting

import { upperCaseFirst } from "text-upper-case-first";

// Format names
upperCaseFirst("john"); // "John"
upperCaseFirst("mary jane"); // "Mary jane"
upperCaseFirst("o'connor"); // "O'connor"
upperCaseFirst("van der berg"); // "Van der berg"

Content Processing

import { upperCaseFirst } from "text-upper-case-first";

// Process content titles
const titles = [
  "getting started",
  "installation guide",
  "best practices",
  "troubleshooting",
  "frequently asked questions",
];

const formattedTitles = titles.map(upperCaseFirst);
console.log(formattedTitles);
// [
//   "Getting started",
//   "Installation guide",
//   "Best practices",
//   "Troubleshooting",
//   "Frequently asked questions"
// ]

Form Field Processing

import { upperCaseFirst } from "text-upper-case-first";

function formatFormField(value) {
  return upperCaseFirst(value.trim().toLowerCase());
}

console.log(formatFormField("  JOHN DOE  ")); // "John doe"
console.log(formatFormField("jane smith")); // "Jane smith"
console.log(formatFormField("BOB WILSON")); // "Bob wilson"

Message Formatting

import { upperCaseFirst } from "text-upper-case-first";

function formatMessage(message) {
  return (
    upperCaseFirst(message.trim()) +
    (message.endsWith(".") || message.endsWith("!") || message.endsWith("?")
      ? ""
      : ".")
  );
}

console.log(formatMessage("hello world")); // "Hello world."
console.log(formatMessage("welcome back!")); // "Welcome back!"
console.log(formatMessage("are you sure?")); // "Are you sure?"

Comment Processing

import { upperCaseFirst } from "text-upper-case-first";

function formatComment(comment) {
  // Capitalize first letter and ensure proper punctuation
  const formatted = upperCaseFirst(comment.trim());

  if (!formatted.match(/[.!?]$/)) {
    return formatted + ".";
  }

  return formatted;
}

console.log(formatComment("great article"));
// "Great article."

console.log(formatComment("thanks for sharing!"));
// "Thanks for sharing!"

Notification Processing

import { upperCaseFirst } from "text-upper-case-first";

class NotificationFormatter {
  static format(message, type = "info") {
    const formattedMessage = upperCaseFirst(message.trim());

    return {
      type,
      message: formattedMessage,
      timestamp: new Date().toISOString(),
    };
  }

  static formatBatch(messages) {
    return messages.map((msg) => this.format(msg));
  }
}

console.log(NotificationFormatter.format("user logged in successfully"));
// {
//   type: "info",
//   message: "User logged in successfully",
//   timestamp: "2023-..."
// }

Text Input Processing

import { upperCaseFirst } from "text-upper-case-first";

function processTextInput(input, options = {}) {
  const {
    autoCapitalize = true,
    trimWhitespace = true,
    addPunctuation = false,
  } = options;

  let processed = input;

  if (trimWhitespace) {
    processed = processed.trim();
  }

  if (autoCapitalize) {
    processed = upperCaseFirst(processed);
  }

  if (addPunctuation && !processed.match(/[.!?]$/)) {
    processed += ".";
  }

  return processed;
}

console.log(
  processTextInput("  hello world  ", {
    autoCapitalize: true,
    addPunctuation: true,
  }),
);
// "Hello world."

Blog Post Processing

import { upperCaseFirst } from "text-upper-case-first";

function processBlogPost(post) {
  return {
    ...post,
    title: upperCaseFirst(post.title),
    excerpt: upperCaseFirst(post.excerpt),
    tags: post.tags.map((tag) => upperCaseFirst(tag)),
  };
}

const blogPost = {
  title: "getting started with react",
  excerpt: "learn the basics of react development",
  tags: ["react", "javascript", "frontend"],
  content: "...",
};

console.log(processBlogPost(blogPost));
// {
//   title: "Getting started with react",
//   excerpt: "Learn the basics of react development",
//   tags: ["React", "Javascript", "Frontend"],
//   content: "..."
// }

Error Message Processing

import { upperCaseFirst } from "text-upper-case-first";

class ErrorFormatter {
  static format(error) {
    if (typeof error === "string") {
      return upperCaseFirst(error);
    }

    if (error.message) {
      return {
        ...error,
        message: upperCaseFirst(error.message),
      };
    }

    return error;
  }

  static formatValidationErrors(errors) {
    const formatted = {};

    Object.entries(errors).forEach(([field, message]) => {
      formatted[field] = upperCaseFirst(message);
    });

    return formatted;
  }
}

console.log(ErrorFormatter.format("invalid email address"));
// "Invalid email address"

console.log(
  ErrorFormatter.formatValidationErrors({
    email: "email is required",
    password: "password must be at least 8 characters",
  }),
);
// {
//   email: "Email is required",
//   password: "Password must be at least 8 characters"
// }

πŸ“– API Reference

upperCaseFirst(input)

Makes the first character of a string uppercase while preserving the rest.

Parameters

Returns

πŸ“Š Bundle Size

This package is optimized for minimal bundle size:

🌍 Browser Support

πŸ§ͺ Testing

# Run tests
pnpm test

# Run tests in watch mode
pnpm test --watch

# Run tests with coverage
pnpm test --coverage

# Type checking
pnpm typecheck

# Linting
pnpm lint

πŸ“œ License

MIT Β© Dmitry Selikhov

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ†˜ Support


Made with ❀️ by Dmitry Selikhov