Skip to main content

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:

FunctionWhat it doesReference
validators(value, name, options)Checks a value against a named rule, such as pan, mobile, or amountvalidators()
formatters(value, name, options)Cleans a value while the user types, or prints it for display or for the APIformatters()
helpers(value, name, options)Runs one of 27 utilities for dropdown options, nested values, lists, numbers, text, and dateshelpers()
request(url, method, options)Calls an API with a token, a timeout, and retries, and returns one result shaperequest()

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. validators returns { valid: true } or { valid: false, code, message }, and formatters always returns a string. request returns 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.