License Plate
Utilities for working with Indonesian Vehicle License Plates (Tanda Nomor Kendaraan Bermotor - TNKB).
Overview
The Plate module provides utilities to validate Indonesian license plates, detect the region/city based on the prefix, and format plates for consistent display.
Features
- Validation: Check if a license plate follows the standard Indonesian format (1-2 letters prefix, 1-4 digits, and 1-3 letters suffix).
- Region Detection: Identify the province or city associated with the plate’s prefix (e.g., ‘B’ for Jakarta, ‘D’ for Bandung).
- Formatting: Normalize plate strings by removing extra spaces and ensuring the standard
[PREFIX] [NUMBER] [SUFFIX]format.
Installation
npm
npm install @indodev/toolkitQuick Start
import { validatePlate, getRegionFromPlate, formatPlate, parsePlate, maskPlate, cleanPlate, isPrivatePlate, isPublicPlate, isDiplomatPlate } from '@indodev/toolkit/plate';
// Validate Plate
validatePlate('B 1234 ABC'); // true
// Get Region
getRegionFromPlate('B 1234 ABC');
// 'DKI Jakarta, Tangerang, Bekasi, Depok'
getRegionFromPlate('D 1');
// 'Bandung, Cimahi'
// Format Plate
formatPlate('b1234abc');
// 'B 1234 ABC'
// Parse Plate
const info = parsePlate('B 1234 ABC');
console.log(info.prefix); // 'B'
console.log(info.number); // '1234'
console.log(info.suffix); // 'ABC'
console.log(info.type); // 'private'
// Mask Plate (for privacy)
maskPlate('B 1234 ABC'); // 'B****ABC'
// Clean Plate (remove non-alphanumeric)
cleanPlate('B 1234 ABC'); // 'B1234ABC'
// Check plate type
isPrivatePlate('B 1234 ABC'); // true
isPublicPlate('B 1234 U'); // false (single suffix treated as private)
isDiplomatPlate('CD 1234 AB'); // trueAPI Reference
validatePlate()
Validates if a string follows the Indonesian license plate format.
Type Signature:
function validatePlate(plate: string): boolean;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to validate |
Returns:
boolean - Returns true if valid, false otherwise.
getRegionFromPlate()
Returns the region name(s) associated with a license plate’s prefix.
Type Signature:
function getRegionFromPlate(plate: string): string | null;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate |
Returns:
string | null - Region description, or null if the prefix is not recognized.
formatPlate()
Formats a license plate string into standard [PREFIX] [NUMBER] [SUFFIX] format.
Type Signature:
function formatPlate(plate: string): string;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate text |
Returns:
string - Formatted license plate, or the original if invalid.
parsePlate()
Parses a license plate and extracts its component parts.
Type Signature:
function parsePlate(plate: string): PlateInfo | null;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to parse |
Returns:
PlateInfo | null - Parsed plate information or null if invalid.
Example:
const info = parsePlate('B 1234 ABC');
if (info) {
console.log(info.prefix); // 'B'
console.log(info.number); // '1234'
console.log(info.suffix); // 'ABC'
console.log(info.type); // 'private'
console.log(info.formatted); // 'B 1234 ABC'
console.log(info.isValid); // true
}
// Parse diplomat plate
const diplomat = parsePlate('CD 1234 AB');
console.log(diplomat?.type); // 'diplomat'maskPlate()
Masks a license plate number for privacy.
Type Signature:
function maskPlate(plate: string, options?: PlateMaskOptions): string;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to mask |
options | PlateMaskOptions | Optional masking configuration |
Returns:
string - Masked plate string (without spaces), or empty string if invalid.
Example:
maskPlate('B 1234 ABC'); // 'B****ABC'
maskPlate('B 1234 ABC', { visibleStart: 2 }); // 'B1***ABC'
maskPlate('B 1234 ABC', { maskChar: '#' }); // 'B####ABC'cleanPlate()
Cleans a license plate by removing all non-alphanumeric characters.
Type Signature:
function cleanPlate(plate: string): string;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to clean |
Returns:
string - Cleaned plate string (alphanumeric only, uppercase).
Example:
cleanPlate('B 1234 ABC'); // 'B1234ABC'
cleanPlate('da-1234-xyz'); // 'DA1234XYZ'isPrivatePlate()
Checks if a license plate is a private vehicle plate.
Type Signature:
function isPrivatePlate(plate: string): boolean;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to check |
Returns:
boolean - Returns true if valid private plate, false otherwise.
isPublicPlate()
Checks if a license plate is a public transportation plate.
Type Signature:
function isPublicPlate(plate: string): boolean;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to check |
Returns:
boolean - Returns true if valid public plate, false otherwise.
isDiplomatPlate()
Checks if a license plate is a diplomat plate.
Type Signature:
function isDiplomatPlate(plate: string): boolean;Parameters:
| Name | Type | Description |
|---|---|---|
plate | string | The license plate to check |
Returns:
boolean - Returns true if valid diplomat plate, false otherwise.
Constants
PLATE_REGIONS
A mapping of license plate prefixes to their respective regions.
import { PLATE_REGIONS } from '@indodev/toolkit/plate';
console.log(PLATE_REGIONS['B']); // 'DKI Jakarta, Tangerang, Bekasi, Depok'
console.log(PLATE_REGIONS['D']); // 'Bandung, Cimahi'PlateInfo
Information extracted from a valid license plate.
interface PlateInfo {
prefix: string; // Plate prefix (e.g., 'B')
number: string; // Plate number (e.g., '1234')
suffix: string; // Plate suffix (e.g., 'ABC')
type: 'private' | 'public' | 'diplomat' | null;
formatted: string; // Formatted plate (e.g., 'B 1234 ABC')
isValid: boolean; // Whether the plate passed validation
}PlateMaskOptions
Options for masking a license plate.
interface PlateMaskOptions {
visibleStart?: number; // Characters visible at start (default: 1)
visibleEnd?: number; // Characters visible at end (default: 3)
maskChar?: string; // Mask character (default: '*')
}InvalidPlateError
Error class for invalid plate with error code for programmatic handling.
class InvalidPlateError extends Error {
readonly code = 'INVALID_PLATE';
constructor(message?: string);
}Example:
import { InvalidPlateError, validatePlate } from '@indodev/toolkit/plate';
try {
if (!validatePlate(plate)) {
throw new InvalidPlateError('Invalid plate format');
}
} catch (error) {
if (error instanceof InvalidPlateError) {
console.log(error.code); // 'INVALID_PLATE'
}
}