kr-corekit
A comprehensive collection of TypeScript utility functions for modern web development.
Features
- 🛠️ Comprehensive: String, object, cookie, number, validation, format, search query, device, type and common utilities
- 📦 Tree-shakable: Import only what you need
- 🔒 Type-safe: Full TypeScript support with type definitions
- ⚡ Lightweight: Minimal dependencies and optimized for performance
- 🧪 Well-tested: Extensive test coverage with comprehensive test cases
Installation
npm install kr-corekit
# or
pnpm add kr-corekit
# or
yarn add kr-corekit
Usage
Full Import (All utilities)
import {
stringUtil,
objectUtil,
cookieUtil,
numberUtil,
validationUtil,
commonUtil,
formatUtil,
searchQueryUtil,
typeUtil,
deviceUtil,
} from "kr-corekit";
// String utilities
const escaped = stringUtil.escapeHtml("<div>Hello</div>");
const unescaped = stringUtil.unescapeHtml("<div>Hello</div>");
// Object utilities
const cleaned = objectUtil.clearNullProperties({ a: 1, b: null, c: 3 });
const frozen = objectUtil.deepFreeze({ a: { b: 1 } });
const withoutKey = objectUtil.removeKey("b", { a: 1, b: 2, c: 3 }); // { a: 1, c: 3 }
// Number utilities
const total = numberUtil.sum(1, 2, 3, 4, 5); // 15
const difference = numberUtil.subtract(10, 3); // 7
const product = numberUtil.multiply(2, 3, 4); // 24
// Validation utilities
const isValid = validationUtil.checkEmail("user@example.com"); // true
const isHttpUrl = validationUtil.checkHttpUrl("https://example.com"); // true
const isDomain = validationUtil.checkDomain("example.com"); // true
const isBase64 = validationUtil.checkBase64("U29tZSB2YWxpZCBiYXNlNjQgc3RyaW5n"); // true
const isPasswordValid = validationUtil.checkPassword("Abc123!@#", {
minLength: 8,
requireUppercase: true,
requireLowercase: true,
requireNumber: true,
requireSpecialChar: true,
}); // true
// Common utilities
const empty = commonUtil.isEmpty(""); // true
const notEmpty = commonUtil.isEmpty("hello"); // false
const nullCheck = commonUtil.isNull(null); // true
const notNull = commonUtil.isNull("hello"); // false
await commonUtil.sleep(1000); // Pauses execution for 1 second
const copied = await commonUtil.copyToClipboard("Hello, World!"); // true if successful
const encoded = commonUtil.encodeBase64("Hello 한글!"); // Base64 encoded string
const decoded = commonUtil.decodeBase64(encoded); // "Hello 한글!"
// Search Query utilities
const queryParams = searchQueryUtil.getAllQuery(); // { key: ["value1", "value2"], id: "123" }
// Type utilities
const isPlain = typeUtil.isPlainObject({}); // true
const isNotPlain = typeUtil.isPlainObject(new Date()); // false
// Device utilities
const device = deviceUtil.getDevice(); // { isMobile: false, isTablet: false, isDesktop: true, isIOS: false, isAndroid: false }
// Cookie utilities
cookieUtil.setCookie("theme", "dark");
const theme = cookieUtil.getCookie("theme");
// Format utilities
const formattedPhone = formatUtil.formatPhoneNumber("01012345678"); // "010-1234-5678"
Tree-Shaking Optimized Import (Recommended)
For optimal bundle size, import only the functions you need:
// Option 1: Import specific functions (best tree-shaking)
import { escapeHtml, unescapeHtml } from "kr-corekit";
import { sum, multiply } from "kr-corekit";
import { clearNullProperties, deepFreeze } from "kr-corekit";
// Option 2: Import from specific utility modules (good tree-shaking)
import { escapeHtml } from "kr-corekit/stringUtil";
import { sum } from "kr-corekit/numberUtil";
import { clearNullProperties } from "kr-corekit/objectUtil";
// Usage remains the same
const escaped = escapeHtml("<div>Hello</div>");
const total = sum(1, 2, 3, 4, 5);
const cleaned = clearNullProperties({ a: 1, b: null, c: 3 });
Bundle Size Comparison
- Full import: ~8.3KB (2.9KB gzipped)
- Tree-shaken import: Only includes functions you use
- Individual module import: Further optimized for specific utilities
API Reference
StringUtil
escapeHtml(str: string): string
- Escapes HTML special charactersunescapeHtml(str: string): string
- Unescapes HTML entities
ObjectUtil
clearNullProperties(obj: object): object
- Removes null/undefined propertiesdeepFreeze(obj: object): object
- Deep freezes an object recursivelyremoveKey(key: string, obj: Record<string, any>): object
- Returns a new object with the specified key removed
NumberUtil
sum(...numbers: number[]): number
- Calculates sum of numberssubtract(...numbers: number[]): number
- Calculates subtraction of numbersmultiply(...numbers: number[]): number
- Calculates multiplication of numbers
FormatUtil
formatPhoneNumber(phone: string): string
- Formats a phone number string to a standard format (e.g., "010-1234-5678")
ValidationUtil
checkEmail(email: string): boolean
- Validates email formatcheckHttpUrl(url: string): boolean
- Validates HTTP/HTTPS URL formatcheckDomain(domain: string): boolean
- Validates domain name formatcheckBase64(value: string): boolean
- Validates whether a string is a valid base64 encoded valuecheckPassword(password: string, options?: { minLength?: number; maxLength?: number; requireUppercase?: boolean; requireLowercase?: boolean; requireNumber?: boolean; requireSpecialChar?: boolean }): boolean
- Validates password strength and requirements
CommonUtil
isEmpty(value: unknown): boolean
- Checks if a value is empty (null, undefined, "", 0, [], {}, empty Set/Map, NaN, or invalid Date)isNull(value: unknown): value is null
- Type guard that checks if a value is null and narrows the typesleep(ms: number): Promise<void>
- Pauses execution for a specified number of millisecondscopyToClipboard(text: string): Promise<boolean>
- Copies text to the user's clipboard. Uses modern Clipboard API with fallback to legacy execCommand method. Returns true if successful, false if failed.encodeBase64(str: string, options?: { convertSpecialChars?: boolean }): string
- Encodes a string to Base64 format with optional special character handlingdecodeBase64(str: string, options?: { convertSpecialChars?: boolean }): string
- Decodes a Base64 string back to original text with optional special character handling
SearchQueryUtil
getAllQuery(): Record<string, string | string[]>
- Parses the current URL's query string and returns an object with key-value pairs. Values appear as arrays when the same key is used multiple times.
TypeUtil
isPlainObject(value: unknown): boolean
- Checks if a value is a plain object (created by Object literal or Object.create(null)), excluding arrays, dates, and other built-in objects.
DeviceUtil
getDevice(): DeviceInfo
- Detects the user's device environment. Returns information about device type (mobile/tablet/desktop) and operating system (iOS/Android). Uses navigator.userAgent for detection and provides safe fallback for SSR environments.
CookieUtil
setCookie(name: string, value: string, options?: object): void
- Sets a cookiegetCookie(name: string): string | null
- Gets a cookie value
Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.
License
MIT License - see LICENSE file for details.