@thecyberlocal/strling
v2.5.3
Published
A next-generation object-oriented interface for regular expressions.
Downloads
9
Maintainers
Readme
STRling
🗺️ Project Overview
STRling is a next-generation production-grade syntax designed as a user interface for writing powerful regular expressions (RegEx) with an object-oriented approach and instructional error handling. STRling recognizes the cryptic nature of raw regular expression is challenging and susceptible to errors, which is why STRling keeps it as far from you as possible while maintaining the same power and flexibility. String validation should be simple and enjoyable, not a hassle. Best of all, STRling syntax is built upon the RegEx engine, making it fully compatible with all libraries that accept RegEx along with the traditional built-in RegEx methods.
🗝️ Key Features
- Beginner Friendly: You need ZERO knowledge of RegEx jargon to create powerful and clear-to-read patterns.
- Reliable Logic: STRling exclusively utilizes built-in libraries, making it a reliable package.
- Instructional Error Handling: Errors inevitably occur by drafting invalid patterns, so STRling explains to the developer exactly what was done wrong and how to correct it.
- Consistent and Readable: Make your projects clear and consistent by using STRling rather than the individual library string validators that each have their own syntax (Sqlite3, Sequelize, Postgres, WTForms... etc.).
- Multilingual Availability: STRling is not just a package, but a concept brought to you through multiple widely used languages.
These key features collectively emphasize and fortify the claim that STRling truly is a next-generation production-grade syntax for pattern handling.
🎯 Project Mission
Our mission is to make RegEx a distant and outdated syntax by abstracting the complexities of RegEx into a clean, readable, and powerful interface. We strive to simplify string operations and ensure that developers can focus on building their applications without the steep learning curve of traditional RegEx. The previous alternative to RegEx was to learn the string validation syntax specific to each user input library, but STRling aims to unify the disparate syntaxes found across various libraries and frameworks into one simple library using only built-in packages.
💾 Installation
Install STRling via npm:
npm install @thecyberlocal/strling
✨ STRling in action!
📑 STRling Documentation on GitHub
import { simply as s } from "@thecyberlocal/strling";
// Let's make a phone number pattern for the formats below:
// - (123) 456-7890
// - 123-456-7890
// - 123 456 7890
// - 1234567890
// Separator: either space or hyphen
const separator = s.inChars(" -");
// Optional area code part: 123 even if in parenthesis like (123)
const areaCode = s.merge(
// notice we use merge since we don't want to name the group with parenthesis
s.may("("), // Optional opening parenthesis
s.group("area_code", s.digit(3)), // Exactly 3 digits and named for later reference
s.may(")"), // Optional closing parenthesis
);
// Central part: 456
const centralPart = s.group("central_part", s.digit(3)); // Exactly 3 digits and named for later reference
// Last part: 7890
const lastPart = s.group("last_part", s.digit(4)); // Exactly 4 digits and named for later reference
// Combine all parts into the final phone number pattern
// Notice we don't name the whole pattern since we can already reference it
const phoneNumberPattern = s.merge(
areaCode, // Area code part
s.may(separator), // Optional separator after area code
centralPart, // Central 3 digits
s.may(separator), // Optional separator after central part
lastPart, // Last part with hyphen and 4 digits
);
// Example usage
// Note: To make a pattern a RegEx string compatible with other engines use `toString(pattern)`.
const exampleText = "(123) 456-7890 and 123-456-7890";
const pattern = new RegExp(phoneNumberPattern.toString(), "g"); // Notice toString(pattern)
const matches = exampleText.matchAll(pattern);
for (const match of matches) {
console.log("Full Match:", match[0]);
console.log("Area Code:", match.groups.area_code);
console.log("Central Part:", match.groups.central_part);
console.log("Last Part:", match.groups.last_part);
console.log();
}
// Output:
// Full Match: (123) 456-7890
// Area Code: 123
// Central Part: 456
// Last Part: 7890
// Full Match: 123-456-7890
// Area Code: 123
// Central Part: 456
// Last Part: 7890
Simplify your string validation and matching tasks with STRling, the all-in-one solution for developers who need a powerful yet user-friendly tool for working with strings. No longer write RegEx using complex jargon or the various syntaxes string validation specific to independent libraries. Download and start using STRling today!
🌎 Locations
STRling for Python
STRling for JavaScript
🌐 Socials
💖 Support
If you find my content helpful or interesting, consider buying me a coffee. Every cup is greatly appreciated and fuels my work!