Metadata-Version: 2.4
Name: slnic
Version: 0.2.0
Summary: Parse, validate, convert, and generate Sri Lankan NIC (National Identity Card) numbers
Author-email: Ravindu Pabasara Karunarathna <karurpabe@gmail.com>
Maintainer-email: Ravindu Pabasara Karunarathna <karurpabe@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/RavinduPabasara/slnic
Project-URL: Documentation, https://github.com/RavinduPabasara/slnic#readme
Project-URL: Repository, https://github.com/RavinduPabasara/slnic
Project-URL: Bug Tracker, https://github.com/RavinduPabasara/slnic/issues
Keywords: sri lanka,nic,national identity card,validation,parsing,kyc,identity
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: black>=24.0; extra == "dev"
Requires-Dist: flake8>=7.0; extra == "dev"
Requires-Dist: mypy>=1.10; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# slnic

Parse, validate, convert, and generate **Sri Lankan NIC (National Identity Card) numbers** in Python.

[![PyPI version](https://img.shields.io/pypi/v/slnic.svg)](https://pypi.org/project/slnic/)
[![Python versions](https://img.shields.io/pypi/pyversions/slnic.svg)](https://pypi.org/project/slnic/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

A Sri Lankan NIC number encodes the holder's **birth date** and **gender**. `slnic` understands both the old 10-character format (`853400123V`) and the new 12-digit format (`198534000123`), and gives you a clean, typed API to work with them — zero dependencies.

## Installation

```bash
pip install slnic
```

## Quick start

```python
import slnic

nic = slnic.parse("853400123V")

nic.birth_date      # datetime.date(1985, 12, 5)
nic.gender          # <Gender.MALE: 'male'>
nic.age()           # 40  (as of today)
nic.format          # <NICFormat.OLD: 'old'>
nic.voter_eligible  # True  ('V' suffix; 'X' → False; None for new format)
nic.serial          # '012'
nic.check_digit     # '3'

str(nic.to_new())   # '198534000123'
```

### Validate

```python
slnic.is_valid("853400123V")    # True
slnic.is_valid("858400123v")    # True  (case-insensitive, female)
slnic.is_valid("859990123V")    # False (invalid day-of-year segment)

# parse() raises InvalidNICError with a helpful message:
slnic.parse("870600012V")
# InvalidNICError: day of year 60 means 29 February, but 1987 is not a leap year
```

### Convert between formats

```python
old = slnic.parse("853400123V")
new = old.to_new()              # NIC('198534000123')

# New → old works when the birth year is 1900-1999 and the serial fits.
# The V/X letter was dropped in the new format, so supply it (default 'V'):
back = new.to_old(voter_eligible=True)   # NIC('853400123V')
```

### Generate test data

```python
from datetime import date
from slnic import generate

nic = generate(date(1992, 7, 14), gender="female", nic_format="new")
str(nic)   # e.g. '199269604823'
```

### Command line

```bash
slnic info 853400123V             # birth date, gender, age, serial, ...
slnic info --json 853400123V      # the same as JSON
slnic validate nics.txt           # one NIC per line (or stdin); lists invalid ones, exits 1 if any
slnic convert 853400123V          # 198534000123 (old <-> new)
slnic generate 1992-07-14 --gender female --format old -n 5
```

Spaces and hyphens between digit groups are accepted everywhere:
`parse("853 400 123V")` and `parse("1985-340-00123")` both work.

> **Note:** the NIC checksum algorithm has never been published, so generated
> numbers are *structurally* valid (great for test fixtures and demos) but may
> not correspond to any real, issued NIC. For the same reason, `parse()` and
> `is_valid()` do not verify the check digit — no library can.

## How NIC numbers work

| Segment | Old format (`853400123V`) | New format (`198534000123`) |
|---|---|---|
| Birth year | `85` → 1985 (always 19xx) | `1985` |
| Day of year | `340` | `340` |
| Serial | `012` (3 digits) | `0012` (4 digits) |
| Check digit | `3` | `3` |
| Voter letter | `V` eligible / `X` not | *(dropped)* |

- **Gender**: if the day-of-year segment is greater than 500, the holder is **female** — subtract 500 to get the actual day (e.g. `840` → day 340).
- **February always counts as 29 days** in the NIC calendar, regardless of leap year. So 1 March is *always* day 61, and day 60 (29 Feb) is only valid for leap-year births.
- **Old → new conversion**: prepend `19` to the year and left-pad the serial with `0`.

## API

| Function / attribute | Description |
|---|---|
| `slnic.parse(s) -> NIC` | Parse either format (spaces/hyphens allowed); raises `InvalidNICError` |
| `slnic.is_valid(s) -> bool` | Structural validity check |
| `slnic.generate(birth_date, ...) -> NIC` | Build a structurally valid NIC (test data) |
| `NIC.birth_date`, `.gender`, `.serial`, `.check_digit`, `.voter_eligible`, `.format`, `.number` | Parsed fields |
| `NIC.birth_year`, `.day_of_year`, `.age(on=None)` | Derived values |
| `NIC.to_new()`, `NIC.to_old(voter_eligible=True)` | Format conversion (raises `ConversionError` when impossible) |

`NIC` is a frozen (immutable, hashable) dataclass, and the package ships type hints (`py.typed`).

## Development

```bash
git clone https://github.com/RavinduPabasara/slnic
cd slnic
make install-dev
make test
```

## Privacy note

NIC numbers are personal data. This library only decodes what the number format itself encodes; take care when logging or storing NICs in your own applications.

## License

MIT — see [LICENSE](LICENSE).

## Author

**Ravindu Pabasara Karunarathna** — also the author of [sinhaladate](https://pypi.org/project/sinhaladate/).
