numsy

# _Numsy_ - _A light-weight number processor_ [![NPM Version](https://img.shields.io/npm/v/@numsy/numsy.svg?style=flat-square)](https://www.npmjs.com/package/@numsy/numsy) [![NPM Downloads](https://img.shields.io/npm/dm/@numsy/numsy.svg?style=flat-square)](https://www.npmjs.com/package/@numsy/numsy) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT) [![TypeScript](https://img.shields.io/badge/TypeScript-5.3-blue.svg?style=flat-square&logo=typescript)](https://www.typescriptlang.org/) [![Node Version](https://img.shields.io/node/v/@numsy/numsy.svg?style=flat-square)](https://nodejs.org) [![CI/CD Pipeline](https://github.com/shreesharma07/numsy/actions/workflows/main.yml/badge.svg)](https://github.com/shreesharma07/numsy/actions/workflows/main.yml) [![Security](https://img.shields.io/github/actions/workflow/status/shreesharma07/numsy/security.yml?style=flat-square&label=Security&logo=github)](https://github.com/shreesharma07/numsy/actions/workflows/security.yml) [![codecov](https://codecov.io/gh/shreesharma07/numsy/graph/badge.svg?token=A9H550XJVW)](https://codecov.io/gh/shreesharma07/numsy) [![Known Vulnerabilities](https://snyk.io/test/github/shreesharma07/numsy/badge.svg?style=flat-square)](https://snyk.io/test/github/shreesharma07/numsy) [![Maintained](https://img.shields.io/badge/Maintained%3F-yes-green.svg?style=flat-square)](https://github.com/shreesharma07/numsy/graphs/commit-activity) [![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) [![Package Manager](https://img.shields.io/badge/pnpm-8.15.0-F69220?style=flat-square&logo=pnpm)](https://pnpm.io/) [![GitHub stars](https://img.shields.io/github/stars/shreesharma07/numsy?style=social)](https://github.com/shreesharma07/numsy/stargazers) [![GitHub forks](https://img.shields.io/github/forks/shreesharma07/numsy?style=social)](https://github.com/shreesharma07/numsy/network/members)

Numsy is a light-weight TypeScript library for Indian phone number validation, sanitization, and CSV/Excel file processing. Built with class-based architecture, comprehensive error handling, and extensive logging capabilities.*

โœจ Features

๐Ÿ“ฆ Installation

npm install numsy
pnpm add numsy
yarn add numsy

๐Ÿš€ Quick Start

Basic Usage

import Numsy from 'numsy';

const numsy = new Numsy();

// Validate a phone number
const result = numsy.validate('9876543210');
console.log(result);
// { original: '9876543210', sanitized: '9876543210', isValid: true }

// Check if valid
console.log(numsy.isValid('9876543210')); // true

// Sanitize number
console.log(numsy.sanitize('+91-987-654-3210')); // '9876543210'

// Format with country code
console.log(numsy.format('9876543210', true)); // '+919876543210'

Using Parser

import { Parser } from 'numsy';
// or
import parser from 'numsy/parser';

const parser = new Parser();

// Parse CSV file
const result = await parser.parseFile('./contacts.csv');
console.log(result.data);
console.log(result.totalRows);

Process Files

import Numsy from 'numsy';

const numsy = new Numsy();

// Process file with validation
const result = await numsy.processFile('./contacts.csv', './output');
console.log(`Processed ${result.totalRecords} records`);
console.log(`Valid: ${result.validRecords}, Invalid: ${result.invalidRecords}`);

๐Ÿ–ฅ๏ธ CLI Usage

Numsy includes a built-in server with a web interface for processing phone numbers without writing code.

Starting the Server

After installing the package:

# Using npx (recommended - no installation needed)
npx @numsy/numsy-serve

# Or install globally first
npm install -g @numsy/numsy
numsy-serve

# With custom options
npx @numsy/numsy-serve --port 3000
npx @numsy/numsy-serve --page
npx @numsy/numsy-serve -p 8080 --page

# Display help
npx @numsy/numsy-serve --help

For local development (in this repository):

# Using pnpm scripts
pnpm run serve

# Or using npm scripts
npm run serve

# Or directly with ts-node
npx ts-node src/cli/server.ts

# With options (requires -- separator)
pnpm run serve -- --port 3000
pnpm run serve -- --page

CLI Options

Option Alias Description Default
--port <number> -p Specify port number (1024-65535) 3000
--page -s, --serve Serve the HTML utility page false
--help -h Display help message -

Environment Variables

You can also configure the server using environment variables:

# Set port via environment variable
PORT=3000 npx @numsy/numsy-serve

# Set environment mode
NODE_ENV=production npx @numsy/numsy-serve

Server Features

API Endpoints

Once the server is running, you can access:

Example Server Output

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ โœ… Server Started Successfully โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿš€ Server running on: http://localhost:3000
๐Ÿ“ก API endpoint: http://localhost:3000/api
๐Ÿ’š Health check: http://localhost:3000/api/health
๐ŸŒ Utility page: http://localhost:3000

๐Ÿ“ Environment: development
โšก Process ID: 12345

Press Ctrl+C to stop the server

๐Ÿ’ก API Examples

Phone Validation

import Numsy from 'numsy';

const numsy = new Numsy();

// Single validation
const result = numsy.validate('9876543210');

// Batch validation
const numbers = ['9876543210', '8123456789', '1234567890'];
const results = numsy.validateBatch(numbers);

// Extract multiple numbers from text
const text = 'Contact me at 9876543210 or 8123456789';
const extracted = numsy.extractMultiple(text);
console.log(extracted.validNumbers); // ['9876543210', '8123456789']

File Operations

import Numsy from 'numsy';

const numsy = new Numsy();

// Parse file
const parsed = await numsy.parseFile('./data.csv');

// Process with validation
const result = await numsy.processFile('./data.csv', './output');

// Write to CSV
await numsy.writeCsv(data, './output/clean-data.csv');

Using Individual Components

import { Parser, PhoneValidator, FileProcessor } from 'numsy';

// Use Parser separately
const parser = new Parser({
  normalizeColumns: true,
  detectPhoneColumn: true,
});

// Use PhoneValidator separately
const validator = new PhoneValidator({
  enableLogging: false,
});

// Use FileProcessor separately
const processor = new FileProcessor({
  outputDir: './output',
});

๐ŸŽฏ Configuration Options

import Numsy from 'numsy';

const numsy = new Numsy({
  enableLogging: true, // Enable console logging
  logLevel: 'debug', // Log level: 'log' | 'error' | 'warn' | 'debug' | 'verbose'
  throwOnError: false, // Throw errors vs return error objects
});

// Update options at runtime
numsy.setOptions({ enableLogging: false });

๐Ÿ“š API Reference

Numsy Class

Main class providing unified API:

Methods

Parser Class

File parsing operations:

PhoneValidator Class

Phone validation operations:

Helper Functions

Pure utility functions:

import {
  sanitizePhoneNumber,
  validatePhoneNumber,
  extractPhoneNumbers,
  normalizeDataRows,
  detectPhoneColumn,
  isNonEmptyString,
  isValidNumber,
  LoggerHelper,
  AppError,
} from 'numsy';

๐Ÿ—๏ธ Architecture

Numsy follows a modern, class-based architecture:

src/
โ”œโ”€โ”€ common/
โ”‚ โ”œโ”€โ”€ interfaces/ # TypeScript interfaces
โ”‚ โ”œโ”€โ”€ functions/ # Pure utility functions
โ”‚ โ””โ”€โ”€ helpers/ # Helper classes (Logger, Error, File, Validation)
โ”œโ”€โ”€ core/
โ”‚ โ”œโ”€โ”€ Numsy.ts # Main class
โ”‚ โ”œโ”€โ”€ Parser.ts # File parser
โ”‚ โ”œโ”€โ”€ PhoneValidator.ts # Phone validator
โ”‚ โ””โ”€โ”€ FileProcessor.ts # File processor
โ””โ”€โ”€ index.ts # Package entry point

๐Ÿ”ง Development

Setup

# Install dependencies
pnpm install

# Build package
pnpm run build

# Run tests
pnpm test

# Development with auto-reload
pnpm run dev

# Start server (for web interface)
pnpm run start:dev

Scripts

๐Ÿ“– Documentation

๐Ÿงช Testing

Numsy includes comprehensive test coverage:

# Run all tests
pnpm test

# Watch mode
pnpm test:watch

# Coverage report
pnpm test:cov

๐Ÿ›ก๏ธ Error Handling

Numsy provides comprehensive error handling:

import { Numsy, AppError } from 'numsy';

try {
  const numsy = new Numsy({ throwOnError: true });
  const result = await numsy.processFile('./data.csv');
} catch (error) {
  if (error instanceof AppError) {
    console.error(`Error [${error.code}]: ${error.message}`);
    console.error('Details:', error.details);
  }
}

๐Ÿ“ TypeScript Support

Full TypeScript support with complete type definitions:

import {
  Numsy,
  NumsyOptions,
  PhoneValidationResult,
  MultipleNumbersResult,
  ProcessingResult,
  FileParseResult,
  ParsedDataRow,
} from 'numsy';

const options: NumsyOptions = {
  enableLogging: true,
  logLevel: 'debug',
  throwOnError: false,
};

const numsy = new Numsy(options);
const result: PhoneValidationResult = numsy.validate('9876543210');

๐Ÿค Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ‘จโ€๐Ÿ’ป Author

Shri Kumar Sharma

๐ŸŒŸ Support

If you find this package helpful, please give it a star on GitHub!

๐Ÿ“Š Stats

๐Ÿš€ Optimizations


๐Ÿ‘ค Contributors


Made with โค๏ธ by Shri Kumar Sharma