Common Utilities
@enmovil/common-utils is a package of small, shared functions that check, clean, and handle values.
The same rules run in a React screen, a Node API, and a Python (Flask) service, so a value gets the same answer in every layer.
This section describes version 1.1.0 of the package.
How Common Utilities works
The package has four main functions:
| Function | What it does | Reference |
|---|---|---|
validators(value, name, options) | Checks a value against a named rule, such as pan, mobile, or amount | validators() |
formatters(value, name, options) | Cleans a value while the user types, or prints it for display or for the API | formatters() |
helpers(value, name, options) | Runs one of 27 utilities for dropdown options, nested values, lists, numbers, text, and dates | helpers() |
request(url, method, options) | Calls an API with a token, a timeout, and retries, and returns one result shape | request() |
validators, formatters, and helpers all take a value, a name, and options.
You decide when to call them, on which field, and what to do with the answer.
import { validators, formatters, helpers } from "@enmovil/common-utils";
validators("56001", "pincode", { required: true });
// { valid: false, code: "pincode", message: "Enter a valid 6-digit PIN code" }
formatters("abcde 1234f", "pan"); // "ABCDE1234F"
helpers(rows, "toOptions", { label: "name", value: "id" }); // [{ label: "Acme", value: 7 }, ...]
The rules are written once, as data. The JavaScript package and the Python package read the same rules and pass the same test cases. A field that the browser accepts is never rejected by the server for a different reason.
Results and errors
The package separates a wrong value from a wrong call:
- A wrong value comes back as a result.
validatorsreturns{ valid: true }or{ valid: false, code, message }, andformattersalways returns a string.requestreturns a result for HTTP errors too. - A wrong call throws an error immediately. This includes a misspelled rule, helper, or option name, a missing required option, and a bad pattern, time zone, or date bound. A typo fails at once instead of quietly checking nothing.
In TypeScript, rule, formatter, and helper names are types, so your editor autocompletes them and flags a misspelled name.
When to use Common Utilities
Use the package whenever a service checks, cleans, or converts a value that another layer also handles:
- Form fields that the browser checks and the API checks again
- Dropdown lists built from API rows
- Dates that are stored in UTC and shown in IST
- Calls from a screen or a service to an Enmovil API
When the package has no validator or formatter for your field, build one from a base rule or base formatter and its options. For how, see Build your own rule.