npm.io
0.0.96 • Published 4d ago

regexpert

Licence
Version
0.0.96
Deps
2
Size
77 kB
Vulns
0
Weekly
2

Installation and use

npm install regexpert

What is needed for work

1. Register plugin
// vite.config.ts

import { regExpertVitePlugin } from 'regexpert';

export default defineConfig(() => {
  return {
    plugins: [regExpertVitePlugin()],
  };
});
2. Start a project
npm run dev
Type parsing occurs only when the project is running!
3. Write your RegExp
// your-file.ts

import { makeNamedRegExp } from 'regexpert';

const { regExp: myFavouriteRegExp, transform: transformMyFavouriteRegExp } = makeNamedRegExp(
  `/(1)\\s?text between\\s(?<groupName>named group)( )?\\k<groupName>?/`,
);

// There will be a conversion to such RegExp - this will work in browsers in IOS
console.info(regExp); // /(1)\s?text between\s(named group)?( )?\2/
What is the main task?
// Let's find a suitable line:

const matches = '1text between named group'.match(myFavouriteRegExp);
// [
//   '1text between named group ',
//   '1',
//   'named group',
//   ' '
// ]

// Let's pass the result to the transform function
const result = transformMyFavouriteRegExp(matches);

console.info(result);
// {
//   '$0': '1text between named group ',
//   '$1': '1',
//   groupName: 'named group',
//   '$3': ' '
// }

type Result = typeof result;
// Result is {
//   $0: `1${string | ''}text between${string}named group${' ' | ''}${'named group' | ''}`,
//   $1: '1',
//   groupName: 'named group',
//   $3?: ' ',
// }
What do we get?
  1. Convenient use of the transformed object
  2. We get the expected types for each field
  3. We know for sure which field will be optional

Every time you make a change, files with your types will be automatically generated, which you can always view and inspect.

Keywords