Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

regex-mapper

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

regex-mapper

A small utility library that provides functions for mapping and matching strings against a set of regular expressions.

  • 0.0.2
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

regex-mapper

A small utility library that provides functions for mapping and matching strings against a set of regular expressions.

Installation

First, ensure you have Node.js installed. Then, you can install this library using npm (or yarn):

npm install regex-mapper

or import by cdn https://cdn.jsdelivr.net/npm/regex-mapper/dist/index.umd.js

Certainly! Below is an example of how to use the regex-mapper library by importing it via CDN with the UMD (Universal Module Definition) format in an HTML file:

Functions

regexMapper

Creates a function that maps a given string to a key, based on the regular expressions or strings provided in a map.

Usage
import { regexMapper } from './regex-utils';

const map = {
  greeting: /^hello/,
  farewell: /bye$/,
};

const match = regexMapper(map);

console.log(match("hello world")); // Output: 'greeting'
console.log(match("goodbye")); // Output: 'farewell'
console.log(match("unknown")); // Output: undefined

regexMapperStrict

Similar to regexMapper, but throws an error when no match is found.

Usage
import { regexMapperStrict } from './regex-utils';

const map = {
  greeting: /^hello/,
  farewell: /bye$/,
};

const matchStrict = regexMapperStrict(map);

console.log(matchStrict("hello world")); // Output: 'greeting'
console.log(matchStrict("goodbye")); // Output: 'farewell'
console.log(matchStrict("unknown")); // Throws an error

regexMatcher

Creates a function that returns all keys whose corresponding regular expression matches the given string.

Usage
import { regexMatcher } from './regex-utils';

const map = {
  greeting: /^hello/,
  farewell: /bye$/,
  casual: /yo/,
};

const matchAll = regexMatcher(map);

console.log(matchAll("hello yo")); // Output: ['greeting', 'casual']
console.log(matchAll("farewell bye")); // Output: ['farewell']
console.log(matchAll("unknown")); // Output: []

Using in UMD

Example: Using regex-mapper with UMD in HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Regex Mapper Example</title>
</head>
<body>
    <h1>Regex Mapper Example</h1>
    Check console
    <script src="https://cdn.jsdelivr.net/npm/regex-mapper/dist/index.umd.js"></script>
    <script>
        const { mapStrings, matchStrings } = RegexMapper;

        const map = {
          greeting: /^hello/,
          farewell: /bye$/,
        };

        const match = regexMapper(map);

        console.log(match("hello world")); // Output: 'greeting'
        console.log(match("goodbye")); // Output: 'farewell'
        console.log(match("unknown")); // Output: undefined

        // Example strings to match
        const strings = ["hello123", "world", "123", "hello world", "test"];

        // Use the mapStrings function to map each string to labels based on the regular expressions
        const mappedStrings = mapStrings(strings, regexes);

        // Use the matchStrings function to find matches for a single string
        const singleMatches = matchStrings("hello123", regexes);

        console.log("Mapped Strings:", mappedStrings);
        console.log("Single Matches for 'hello123':", singleMatches);
    </script>
</body>
</html>

Development

To contribute or modify the library, follow these steps:

  1. Clone the repository:
    git clone https://github.com/snomiao/regex-mapper.git
    
  2. Navigate to the project directory:
    cd regex-mapper
    
  3. Install dependencies:
    npm install
    
  4. Run tests:
    npm test
    

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Feel free to open issues or submit pull requests. Contributions are welcome!

Acknowledgements

This utility was built using the error handling library DIE.


Happy coding! 🎉

2024 snomiao snomiao@gmail.com

FAQs

Package last updated on 23 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc