npm package: @skybaer0804/tdd
Description: TDD 방식으로 개발된 유틸리티 라이브러리
Author: Nodejs
Latest version: 1.1.8

# TDD Utility Library

A utility function library developed using TDD (Test-Driven Development). Provides various features including mathematical operations, data validation, and formatting.

## Installation

```bash
npm install @skybaer0804/tdd
```

or

```bash
yarn add @skybaer0804/tdd
```

## Usage

```javascript
// 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>')); // '&lt;script&gt;alert(1)&lt;/script&gt;'
```

## API Documentation

### Math Functions

#### add(a, b)

Adds two numbers. Automatically converts string numbers.

-   `a` (number|string): First number
-   `b` (number|string): Second number
-   Returns: (number) Sum of two numbers

```javascript
add(2, 3); // 5
add('1', '2'); // 3
add('1,000', '2,000'); // 3000
```

#### subtract(a, b)

Subtracts two numbers. Automatically converts string numbers.

-   `a` (number|string): First number
-   `b` (number|string): Second number
-   Returns: (number) Difference of two numbers

```javascript
subtract(5, 3); // 2
subtract('5', '3'); // 2
```

#### pow(base, exponent)

Power function. Automatically converts string numbers.

-   `base` (number|string): Base
-   `exponent` (number|string): Exponent
-   Returns: (number) Base raised to the power of exponent

```javascript
pow(2, 3); // 8
pow('2', '3'); // 8
```

#### divide(dividend, divisor)

Division function. Automatically converts string numbers.

-   `dividend` (number|string): Number to be divided
-   `divisor` (number|string): Number to divide by
-   Returns: (number) Result of division

```javascript
divide(10, 2); // 5
divide('10', '2'); // 5
```

#### percent(value, total)

Calculates percentage.

-   `value` (number|string): Value to calculate
-   `total` (number|string): Total value
-   Returns: (number) Percentage

```javascript
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

```javascript
isNum(123); // true
isNum('123'); // false
isNum(null); // false
```

#### isPositiveNum(value)

Checks if a value is a non-negative integer.

-   `value` (\*): Value to check
-   Returns: (boolean) true if non-negative integer, false otherwise

```javascript
isPositiveNum(0); // true
isPositiveNum(1); // true
isPositiveNum(-1); // false
```

#### isUrl(value)

Checks if a value is a valid URL format.

-   `value` (\*): Value to check
-   Returns: (boolean) true if valid URL, false otherwise

```javascript
isUrl('https://example.com'); // true
isUrl('not-a-url'); // false
```

#### isInputXssSafe(value)

Checks if input value is safe from XSS attacks.

-   `value` (\*): Value to check
-   Returns: (boolean) true if safe, false if dangerous

```javascript
isInputXssSafe('Safe text'); // true
isInputXssSafe('<script>alert("XSS")</script>'); // false
```

### Format Functions

#### toNum(value)

Converts a value to a number.

-   `value` (\*): Value to convert
-   Returns: (number) Converted number

```javascript
toNum('123'); // 123
toNum(' 12,340 '); // 12340
toNum('abc'); // NaN
```

#### toStringComma(value)

Converts a number to a string with thousand separators.

-   `value` (number|string): Number or string to convert
-   Returns: (string) String with commas

```javascript
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

```javascript
toRenderXssSafe('Safe text'); // 'Safe text'
toRenderXssSafe('<script>alert("XSS")</script>'); // '&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;'
```

## Running Tests

```bash
npm test
```

## Development

This project follows the TDD Red-Green-Blue cycle:

1. Red Phase: Write failing test code
2. Green Phase: Write minimal code to pass tests
3. 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