utilitify-core
v1.0.0
Published
Comprehensive validation & utility SDK for JavaScript/TypeScript - User data validation, country utilities, string helpers, security, and more.
Maintainers
Readme
Utilitify
Comprehensive validation & utility SDK for JavaScript/TypeScript applications
Features
- Security - Password strength, hashing, secure tokens
- Validators - Email, phone, username, URL, credit card, IBAN, VAT, IP
- Country Data - Countries, timezones, currencies, dialing codes
- String Utils - Case conversion, slugs, masking, truncation
- Date Utils - Age calculation, formatting, validation
- Transform - Deep merge, flatten, sanitization
- Form Validation - Field validators, conditional rules
- Tree-Shakable - Import only what you need
- Zero Dependencies - Lightweight core
- TypeScript Native - Full type safety
Installation
npm install utilitify-coreOptional (for bcrypt hashing):
npm install bcryptjsQuick Start
import { validateEmail } from 'utilitify-core/validators';
import { checkPasswordStrength } from 'utilitify-core/security';
import { slugify } from 'utilitify-core/string';
// Email validation
const email = validateEmail('[email protected]');
console.log(email.isValid); // true
// Password strength
const password = checkPasswordStrength('MyP@ssw0rd123');
console.log(password.score); // 0-4
console.log(password.crackTime); // "Centuries"
// String utilities
const slug = slugify('Hello World!'); // "hello-world"API Reference
Validators (utilitify-core/validators)
// Email
validateEmail(email, options?)
isValidEmail(email)
isDisposableEmail(email)
normalizeEmail(email)
// Phone
validatePhone(phone, country?)
formatPhone(phone, country?)
// Username
validateUsername(username, options?)
suggestUsernames(base)
// URL
validateUrl(url, options?)
isDangerousUrl(url)
// Credit Card
validateCreditCard(number)
detectCardType(number)
// Financial
validateIBAN(iban)
validateVAT(vat, country)
// IP Address
validateIp(ip, options?)
isPrivateIp(ip)Security (utilitify-core/security)
// Password
checkPasswordStrength(password, options?)
isStrongPassword(password, minScore?)
getPasswordFeedback(password)
// Hashing
hashSHA256(data)
hashSHA512(data)
hashBcrypt(password, rounds?)
verifyBcrypt(password, hash)
hmacSHA256(data, key)
// Tokens
generateUUID()
generateUUIDv7()
generateToken(length)
generateSecureToken(length)
generateNanoId(length?)String (utilitify-core/string)
// Case conversion
toCamelCase(str)
toPascalCase(str)
toSnakeCase(str)
toKebabCase(str)
toTitleCase(str)
// Slugs
slugify(str, options?)
uniqueSlug(str, existing)
// Masking
maskEmail(email)
maskPhone(phone)
maskCreditCard(number)
// Random
generatePassword(options?)
generateOTP(length?)
// Truncation
truncate(str, length, options?)
truncateWords(str, words)Country (utilitify-core/country)
// Country lookup
getCountry(code)
getAllCountries()
searchCountries(query)
// Timezone
getTimezonesByCountry(country)
getCurrentTimeInCountry(country)
// Currency
getCurrencyByCountry(country)
formatCurrency(amount, currency)
// Flags & Dialing
getFlagEmoji(country)
getDialingCode(country)Date (utilitify-core/date)
// Age
calculateAge(birthDate)
isAdult(birthDate, minAge?)
getAgeBracket(age)
// Formatting
formatRelativeTime(date)
formatDate(date, format)
// Validation
isValidDate(date)
isInPast(date)
isWeekend(date)
// Time difference
getTimeDiff(date1, date2)
addTime(date, amount, unit)Transform (utilitify-core/transform)
// Objects
deepMerge(obj1, obj2)
cloneDeep(obj)
flattenObject(obj)
pick(obj, keys)
omit(obj, keys)
// Sanitization
sanitizeString(str)
stripHtml(html)
escapeHtml(str)Form (utilitify-core/form)
// Validators
required(value)
minLength(value, min)
maxLength(value, max)
matchesPattern(value, pattern)
// Conditional
requiredIf(value, condition)
mustMatch(value, other)Schema (utilitify-core/schema)
import { z } from 'utilitify-core/schema';
const schema = z.object({
email: z.string().email(),
age: z.number().min(18).max(120),
name: z.string().min(2)
});
const result = schema.safeParse(data);
if (result.success) {
console.log(result.data);
}Usage Examples
Form Validation
import { validateEmail, validatePhone, checkPasswordStrength } from 'utilitify-core';
function validateForm(data) {
const errors = {};
const email = validateEmail(data.email, { checkDisposable: true });
if (!email.isValid) {
errors.email = email.errors[0].message;
}
const phone = validatePhone(data.phone, 'US');
if (!phone.isValid) {
errors.phone = 'Invalid phone number';
}
const password = checkPasswordStrength(data.password);
if (password.score < 3) {
errors.password = password.suggestions.join(', ');
}
return { isValid: Object.keys(errors).length === 0, errors };
}Secure Token Generation
import { generateUUID, generateSecureToken } from 'utilitify-core/security';
const userId = generateUUID();
const apiKey = generateSecureToken(32);
const sessionToken = generateSecureToken(64);International Phone Formatting
import { formatPhone, getDialingCode } from 'utilitify-core/country';
const formatted = formatPhone('2025551234', 'US');
// "+1 (202) 555-1234"
const code = getDialingCode('GB');
// "+44"String Manipulation
import { slugify, maskEmail, truncate } from 'utilitify-core/string';
slugify('Hello World! 123'); // "hello-world-123"
maskEmail('[email protected]'); // "u***@example.com"
truncate('Long text...', 10); // "Long te..."Tree-Shaking
Import from specific paths to reduce bundle size:
// Recommended - Only imports what you need
import { validateEmail } from 'utilitify-core/validators';
import { generateUUID } from 'utilitify-core/security';
// Avoid - Imports entire package
import { validateEmail, generateUUID } from 'utilitify-core';TypeScript Support
Full TypeScript support with type definitions:
import type {
EmailValidationResult,
PasswordStrength,
Country
} from 'utilitify-core';
const result: EmailValidationResult = validateEmail('[email protected]');Browser & Node.js Support
- Node.js: >= 18.0.0
- Browsers: All modern browsers (ES2022+)
- TypeScript: >= 5.0
Bundle Size
- Core validators: ~5 KB (gzipped)
- Full package: ~10 KB (gzipped)
- Tree-shakable modules
License
MIT © Fahad
