text-lower-case
Version:
Convert text to all lowercase letters
477 lines (364 loc) β’ 11.4 kB
Markdown
# Lower Case
[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Bundle size][bundlephobia-image]][bundlephobia-url]
[](https://opensource.org/licenses/MIT)
[](http://www.typescriptlang.org/)
> Transform text to **lowercase** format - all characters converted to lowercase.
## π Features
- **Lightweight** - Only ~200B minified + gzipped
- **Type-safe** - Full TypeScript support with comprehensive type definitions
- **Zero dependencies** - No external dependencies
- **Tree-shakeable** - ES modules support
- **Universal** - Works in browsers, Node.js, and serverless environments
- **Well-tested** - Comprehensive test suite with edge cases
## π¦ Installation
```bash
# npm
npm install text-lower-case
# yarn
yarn add text-lower-case
# pnpm
pnpm add text-lower-case
# bun
bun add text-lower-case
```
## π― Quick Start
```javascript
import { lowerCase } from "text-lower-case";
console.log(lowerCase("HELLO WORLD")); // "hello world"
console.log(lowerCase("CamelCase")); // "camelcase"
console.log(lowerCase("KEBAB-CASE")); // "kebab-case"
```
## π Usage
### ES Modules (Recommended)
```javascript
import { lowerCase } from "text-lower-case";
console.log(lowerCase("HELLO")); // "hello"
```
### CommonJS
```javascript
const { lowerCase } = require("text-lower-case");
console.log(lowerCase("HELLO")); // "hello"
```
### TypeScript
```typescript
import { lowerCase } from "text-lower-case";
const result: string = lowerCase("HELLO WORLD");
console.log(result); // "hello world"
```
## π Transformation Examples
### Basic Transformations
```javascript
import { lowerCase } from "text-lower-case";
// Simple cases
lowerCase("HELLO"); // "hello"
lowerCase("WORLD"); // "world"
lowerCase("Hello World"); // "hello world"
// Mixed case
lowerCase("hELLo WoRLD"); // "hello world"
lowerCase("CamelCase"); // "camelcase"
lowerCase("PascalCase"); // "pascalcase"
// Programming cases
lowerCase("SNAKE_CASE"); // "snake_case"
lowerCase("KEBAB-CASE"); // "kebab-case"
lowerCase("DOT.CASE"); // "dot.case"
```
### Edge Cases
```javascript
import { lowerCase } from "text-lower-case";
// Empty and single character
lowerCase(""); // ""
lowerCase("A"); // "a"
lowerCase("a"); // "a"
// Numbers and symbols
lowerCase("HELLO123"); // "hello123"
lowerCase("TEST@EMAIL.COM"); // "test@email.com"
lowerCase("USER-123"); // "user-123"
// Unicode characters
lowerCase("CAFΓ"); // "cafΓ©"
lowerCase("NAΓVE"); // "naΓ―ve"
lowerCase("RΓSUMΓ"); // "rΓ©sumΓ©"
```
## π Real-World Examples
### Email and Username Processing
```javascript
import { lowerCase } from "text-lower-case";
// Email normalization
lowerCase("USER@EXAMPLE.COM"); // "user@example.com"
lowerCase("John.Doe@Gmail.Com"); // "john.doe@gmail.com"
lowerCase("ADMIN@COMPANY.ORG"); // "admin@company.org"
// Username normalization
lowerCase("JohnDoe123"); // "johndoe123"
lowerCase("ADMIN_USER"); // "admin_user"
lowerCase("Guest-User"); // "guest-user"
```
### URL and Domain Processing
```javascript
import { lowerCase } from "text-lower-case";
// Domain normalization
lowerCase("EXAMPLE.COM"); // "example.com"
lowerCase("API.GitHub.Com"); // "api.github.com"
lowerCase("SUB.DOMAIN.ORG"); // "sub.domain.org"
// Protocol normalization
lowerCase("HTTPS"); // "https"
lowerCase("HTTP"); // "http"
lowerCase("FTP"); // "ftp"
```
### Search and Filtering
```javascript
import { lowerCase } from "text-lower-case";
function searchItems(items, query) {
const normalizedQuery = lowerCase(query);
return items.filter(
(item) =>
lowerCase(item.name).includes(normalizedQuery) ||
lowerCase(item.description).includes(normalizedQuery),
);
}
const products = [
{ name: "iPhone 15", description: "Latest Apple smartphone" },
{ name: "Samsung Galaxy", description: "Android flagship device" },
{ name: "Google Pixel", description: "Pure Android experience" },
];
console.log(searchItems(products, "APPLE"));
// [{ name: "iPhone 15", description: "Latest Apple smartphone" }]
```
### Form Data Processing
```javascript
import { lowerCase } from "text-lower-case";
function normalizeFormData(formData) {
const normalized = {};
for (const [key, value] of Object.entries(formData)) {
if (typeof value === "string") {
// Normalize email fields
if (key.toLowerCase().includes("email")) {
normalized[key] = lowerCase(value.trim());
}
// Normalize username fields
else if (key.toLowerCase().includes("username")) {
normalized[key] = lowerCase(value.trim());
} else {
normalized[key] = value;
}
} else {
normalized[key] = value;
}
}
return normalized;
}
const formData = {
email: " USER@EXAMPLE.COM ",
username: "JOHNDOE123",
firstName: "John",
lastName: "Doe",
};
console.log(normalizeFormData(formData));
// {
// email: "user@example.com",
// username: "johndoe123",
// firstName: "John",
// lastName: "Doe"
// }
```
### Database Query Normalization
```javascript
import { lowerCase } from "text-lower-case";
class UserRepository {
async findByEmail(email) {
const normalizedEmail = lowerCase(email.trim());
return await this.db.users.findOne({
email: normalizedEmail,
});
}
async findByUsername(username) {
const normalizedUsername = lowerCase(username.trim());
return await this.db.users.findOne({
username: normalizedUsername,
});
}
async searchUsers(query) {
const normalizedQuery = lowerCase(query);
return await this.db.users.find({
$or: [
{ email: { $regex: normalizedQuery, $options: "i" } },
{ username: { $regex: normalizedQuery, $options: "i" } },
],
});
}
}
```
### Configuration Management
```javascript
import { lowerCase } from "text-lower-case";
function parseEnvironmentVariables(env) {
const config = {};
for (const [key, value] of Object.entries(env)) {
// Normalize boolean values
if (typeof value === "string") {
const lowerValue = lowerCase(value);
if (lowerValue === "true" || lowerValue === "false") {
config[key] = lowerValue === "true";
}
// Normalize protocol values
else if (key.toLowerCase().includes("protocol")) {
config[key] = lowerValue;
}
// Normalize host values
else if (key.toLowerCase().includes("host")) {
config[key] = lowerValue;
} else {
config[key] = value;
}
} else {
config[key] = value;
}
}
return config;
}
const env = {
NODE_ENV: "PRODUCTION",
DATABASE_HOST: "LOCALHOST",
USE_SSL: "TRUE",
PROTOCOL: "HTTPS",
};
console.log(parseEnvironmentVariables(env));
// {
// NODE_ENV: "PRODUCTION",
// DATABASE_HOST: "localhost",
// USE_SSL: true,
// PROTOCOL: "https"
// }
```
### Text Processing Pipeline
```javascript
import { lowerCase } from "text-lower-case";
class TextNormalizer {
constructor() {
this.processors = [];
}
addTrim() {
this.processors.push((text) => text.trim());
return this;
}
addLowerCase() {
this.processors.push(lowerCase);
return this;
}
addRemoveExtraSpaces() {
this.processors.push((text) => text.replace(/\s+/g, " "));
return this;
}
process(text) {
return this.processors.reduce(
(result, processor) => processor(result),
text,
);
}
}
const normalizer = new TextNormalizer()
.addTrim()
.addLowerCase()
.addRemoveExtraSpaces();
console.log(normalizer.process(" HELLO WORLD ")); // "hello world"
```
### API Response Processing
```javascript
import { lowerCase } from "text-lower-case";
function normalizeApiResponse(response) {
if (response.data && Array.isArray(response.data)) {
response.data = response.data.map((item) => {
if (item.email) {
item.email = lowerCase(item.email);
}
if (item.username) {
item.username = lowerCase(item.username);
}
if (item.status) {
item.status = lowerCase(item.status);
}
return item;
});
}
return response;
}
const apiResponse = {
success: true,
data: [
{ id: 1, email: "USER@EXAMPLE.COM", username: "JOHNDOE", status: "ACTIVE" },
{
id: 2,
email: "ADMIN@COMPANY.ORG",
username: "ADMIN",
status: "INACTIVE",
},
],
};
console.log(normalizeApiResponse(apiResponse));
// {
// success: true,
// data: [
// { id: 1, email: "user@example.com", username: "johndoe", status: "active" },
// { id: 2, email: "admin@company.org", username: "admin", status: "inactive" }
// ]
// }
```
## π API Reference
### `lowerCase(input)`
Converts a string to lowercase format.
#### Parameters
- **`input`** (`string`): The string to transform
#### Returns
- **`string`**: The string converted to lowercase
## π Bundle Size
This package is optimized for minimal bundle size:
- **Minified**: ~200B
- **Gzipped**: ~150B
- **Tree-shakeable**: Yes
- **Side effects**: None
## π Browser Support
- **Modern browsers**: ES2015+ (Chrome 51+, Firefox 54+, Safari 10+)
- **Node.js**: 12+
- **TypeScript**: 4.0+
- **Bundle formats**: UMD, ESM, CommonJS
## π§ͺ Testing
```bash
# 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
```
## π Related Packages
- [`text-camel-case`](https://www.npmjs.com/package/text-camel-case) - Convert to camelCase
- [`text-capital-case`](https://www.npmjs.com/package/text-capital-case) - Convert to Capital Case
- [`text-constant-case`](https://www.npmjs.com/package/text-constant-case) - Convert to CONSTANT_CASE
- [`text-dot-case`](https://www.npmjs.com/package/text-dot-case) - Convert to dot.case
- [`text-header-case`](https://www.npmjs.com/package/text-header-case) - Convert to Header-Case
- [`text-case`](https://www.npmjs.com/package/text-case) - All case transformations in one package
## π License
[MIT](LICENSE) Β© [Dmitry Selikhov](https://github.com/idimetrix)
## π€ 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
- π§ **Email**: [selikhov.dmitrey@gmail.com](mailto:selikhov.dmitrey@gmail.com)
- π **Issues**: [GitHub Issues](https://github.com/idimetrix/text-case/issues)
- π¬ **Discussions**: [GitHub Discussions](https://github.com/idimetrix/text-case/discussions)
- π **Documentation**: [Full Documentation](https://github.com/idimetrix/text-case#readme)
---
**Made with β€οΈ by [Dmitry Selikhov](https://github.com/idimetrix)**
[npm-image]: https://img.shields.io/npm/v/text-lower-case.svg?style=flat
[npm-url]: https://npmjs.org/package/text-lower-case
[downloads-image]: https://img.shields.io/npm/dm/text-lower-case.svg?style=flat
[downloads-url]: https://npmjs.org/package/text-lower-case
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/text-lower-case.svg
[bundlephobia-url]: https://bundlephobia.com/result?p=text-lower-case