@skybaer0804/tdd
v1.1.8
Published
TDD 방식으로 개발된 유틸리티 라이브러리
Maintainers
Readme
TDD Utility Library
A utility function library developed using TDD (Test-Driven Development). Provides various features including mathematical operations, data validation, and formatting.
Installation
npm install @skybaer0804/tddor
yarn add @skybaer0804/tddUsage
// Method 1: Import all functions at once
import { add, subtract, pow, divide, percent, isNum, isPositiveNum, isUrl, isInputXssSafe, toNum, toStringComma, toRenderXssSafe } from '@skybaer0804/tdd';
// Method 2: Import by category
import { add, subtract, pow, divide, percent } from '@skybaer0804/tdd/math';
import { isNum, isPositiveNum, isUrl, isInputXssSafe } from '@skybaer0804/tdd/validate';
import { toNum, toStringComma, toRenderXssSafe } from '@skybaer0804/tdd/format';
// Math functions
console.log(add(2, 3)); // 5
console.log(add('1', '2')); // 3
console.log(subtract(5, 3)); // 2
console.log(pow(2, 3)); // 8
console.log(divide(10, 2)); // 5
console.log(percent(50, 100)); // 50
// Validation functions
console.log(isNum(123)); // true
console.log(isUrl('https://example.com')); // true
console.log(isInputXssSafe('<script>alert(1)</script>')); // false
// Format functions
console.log(toNum('123')); // 123
console.log(toStringComma(1234)); // '1,234'
console.log(toRenderXssSafe('<script>alert(1)</script>')); // '<script>alert(1)</script>'API Documentation
Math Functions
add(a, b)
Adds two numbers. Automatically converts string numbers.
a(number|string): First numberb(number|string): Second number- Returns: (number) Sum of two numbers
add(2, 3); // 5
add('1', '2'); // 3
add('1,000', '2,000'); // 3000subtract(a, b)
Subtracts two numbers. Automatically converts string numbers.
a(number|string): First numberb(number|string): Second number- Returns: (number) Difference of two numbers
subtract(5, 3); // 2
subtract('5', '3'); // 2pow(base, exponent)
Power function. Automatically converts string numbers.
base(number|string): Baseexponent(number|string): Exponent- Returns: (number) Base raised to the power of exponent
pow(2, 3); // 8
pow('2', '3'); // 8divide(dividend, divisor)
Division function. Automatically converts string numbers.
dividend(number|string): Number to be divideddivisor(number|string): Number to divide by- Returns: (number) Result of division
divide(10, 2); // 5
divide('10', '2'); // 5percent(value, total)
Calculates percentage.
value(number|string): Value to calculatetotal(number|string): Total value- Returns: (number) Percentage
percent(50, 100); // 50
percent(25, 100); // 25
percent(1, 3); // 33.333...Validate Functions
isNum(value)
Checks if a value is a number.
value(*): Value to check- Returns: (boolean) true if number, false otherwise
isNum(123); // true
isNum('123'); // false
isNum(null); // falseisPositiveNum(value)
Checks if a value is a non-negative integer.
value(*): Value to check- Returns: (boolean) true if non-negative integer, false otherwise
isPositiveNum(0); // true
isPositiveNum(1); // true
isPositiveNum(-1); // falseisUrl(value)
Checks if a value is a valid URL format.
value(*): Value to check- Returns: (boolean) true if valid URL, false otherwise
isUrl('https://example.com'); // true
isUrl('not-a-url'); // falseisInputXssSafe(value)
Checks if input value is safe from XSS attacks.
value(*): Value to check- Returns: (boolean) true if safe, false if dangerous
isInputXssSafe('Safe text'); // true
isInputXssSafe('<script>alert("XSS")</script>'); // falseFormat Functions
toNum(value)
Converts a value to a number.
value(*): Value to convert- Returns: (number) Converted number
toNum('123'); // 123
toNum(' 12,340 '); // 12340
toNum('abc'); // NaNtoStringComma(value)
Converts a number to a string with thousand separators.
value(number|string): Number or string to convert- Returns: (string) String with commas
toStringComma(1234); // '1,234'
toStringComma(1234567); // '1,234,567'toRenderXssSafe(value)
Escapes HTML special characters to prevent XSS attacks.
value(*): Value to escape- Returns: (string) Escaped safe string
toRenderXssSafe('Safe text'); // 'Safe text'
toRenderXssSafe('<script>alert("XSS")</script>'); // '<script>alert("XSS")</script>'Running Tests
npm testDevelopment
This project follows the TDD Red-Green-Blue cycle:
- Red Phase: Write failing test code
- Green Phase: Write minimal code to pass tests
- Blue Phase: Refactor and clean up code
Changelog
1.1.0 (2024-11-10)
New Features
- Added format category (toNum, toStringComma, toRenderXssSafe)
- Extended validate category (isUrl, isInputXssSafe)
- Extended math category (percent)
Improvements
- Improved non-number input handling in math functions
- Support for automatic string number conversion
- Support for string numbers with spaces and commas
1.0.2
- Initial version
- Basic math functions (add, subtract, pow, divide)
- Basic validation functions (isNum, isPositiveNum)
License
MIT
