A utility function library developed using TDD (Test-Driven Development). Provides various features including mathematical operations, data validation, and formatting.
npm install @skybaer0804/tddor
yarn add @skybaer0804/tdd// 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>'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'); // 3000Subtracts 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'); // 2Power 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'); // 8Division 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'); // 5Calculates 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...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); // falseChecks 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); // falseChecks 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'); // falseChecks 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>'); // falseConverts a value to a number.
value(*): Value to convert- Returns: (number) Converted number
toNum('123'); // 123
toNum(' 12,340 '); // 12340
toNum('abc'); // NaNConverts 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'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>'npm testThis 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
- Added format category (toNum, toStringComma, toRenderXssSafe)
- Extended validate category (isUrl, isInputXssSafe)
- Extended math category (percent)
- Improved non-number input handling in math functions
- Support for automatic string number conversion
- Support for string numbers with spaces and commas
- Initial version
- Basic math functions (add, subtract, pow, divide)
- Basic validation functions (isNum, isPositiveNum)
MIT