This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+35
View File
@@ -0,0 +1,35 @@
'use strict';
const escapeStringRegexp = require('escape-string-regexp');
const mapping = require('./lib/mappings');
const hasFlags = (regexFlags, replaceFlags) => {
if (!replaceFlags) {
return true;
}
// Check if every flag in the replace flags is part of the original regex flags
return replaceFlags.split('').every(flag => regexFlags.includes(flag));
};
module.exports = (regexp, flags) => {
flags = flags || '';
if (typeof regexp !== 'string') {
throw new TypeError(`Expected regexp to be of type \`string\`, got \`${typeof regexp}\``);
}
if (typeof flags !== 'string') {
throw new TypeError(`Expected flags to be of type \`string\`, got \`${typeof flags}\``);
}
for (const replace of mapping) {
const key = replace[0];
const replacement = replace[1];
if (hasFlags(flags, replacement.flags)) {
regexp = regexp.replace(new RegExp(escapeStringRegexp(key), 'g'), replacement.value);
}
}
return regexp;
};
+143
View File
@@ -0,0 +1,143 @@
'use strict';
module.exports = new Map([
['[0-9]', {value: '\\d'}],
['[^0-9]', {value: '\\D'}],
// Word
['[a-zA-Z0-9_]', {value: '\\w'}],
['[a-zA-Z_0-9]', {value: '\\w'}],
['[a-z0-9A-Z_]', {value: '\\w'}],
['[a-z0-9_A-Z]', {value: '\\w'}],
['[a-z_A-Z0-9]', {value: '\\w'}],
['[a-z_0-9A-Z]', {value: '\\w'}],
['[A-Za-z0-9_]', {value: '\\w'}],
['[A-Za-z_0-9]', {value: '\\w'}],
['[A-Z0-9a-z_]', {value: '\\w'}],
['[A-Z0-9_a-z]', {value: '\\w'}],
['[A-Z_a-z0-9]', {value: '\\w'}],
['[A-Z_0-9a-z]', {value: '\\w'}],
['[0-9a-zA-Z_]', {value: '\\w'}],
['[0-9a-z_A-Z]', {value: '\\w'}],
['[0-9A-Za-z_]', {value: '\\w'}],
['[0-9A-Z_a-z]', {value: '\\w'}],
['[0-9_a-zA-Z]', {value: '\\w'}],
['[0-9_A-Za-z]', {value: '\\w'}],
['[_a-zA-Z0-9]', {value: '\\w'}],
['[_a-z0-9A-Z]', {value: '\\w'}],
['[_A-Za-z0-9]', {value: '\\w'}],
['[_A-Z0-9a-z]', {value: '\\w'}],
['[_0-9a-zA-Z]', {value: '\\w'}],
['[_0-9A-Za-z]', {value: '\\w'}],
// Word with digit
['[a-zA-Z\\d_]', {value: '\\w'}],
['[a-zA-Z_\\d]', {value: '\\w'}],
['[a-z\\dA-Z_]', {value: '\\w'}],
['[a-z\\d_A-Z]', {value: '\\w'}],
['[a-z_A-Z\\d]', {value: '\\w'}],
['[a-z_\\dA-Z]', {value: '\\w'}],
['[A-Za-z\\d_]', {value: '\\w'}],
['[A-Za-z_\\d]', {value: '\\w'}],
['[A-Z\\da-z_]', {value: '\\w'}],
['[A-Z\\d_a-z]', {value: '\\w'}],
['[A-Z_a-z\\d]', {value: '\\w'}],
['[A-Z_\\da-z]', {value: '\\w'}],
['[\\da-zA-Z_]', {value: '\\w'}],
['[\\da-z_A-Z]', {value: '\\w'}],
['[\\dA-Za-z_]', {value: '\\w'}],
['[\\dA-Z_a-z]', {value: '\\w'}],
['[\\d_a-zA-Z]', {value: '\\w'}],
['[\\d_A-Za-z]', {value: '\\w'}],
['[_a-zA-Z\\d]', {value: '\\w'}],
['[_a-z\\dA-Z]', {value: '\\w'}],
['[_A-Za-z\\d]', {value: '\\w'}],
['[_A-Z\\da-z]', {value: '\\w'}],
['[_\\da-zA-Z]', {value: '\\w'}],
['[_\\dA-Za-z]', {value: '\\w'}],
// Non-word
['[^a-zA-Z0-9_]', {value: '\\W'}],
['[^a-zA-Z_0-9]', {value: '\\W'}],
['[^a-z0-9A-Z_]', {value: '\\W'}],
['[^a-z0-9_A-Z]', {value: '\\W'}],
['[^a-z_A-Z0-9]', {value: '\\W'}],
['[^a-z_0-9A-Z]', {value: '\\W'}],
['[^A-Za-z0-9_]', {value: '\\W'}],
['[^A-Za-z_0-9]', {value: '\\W'}],
['[^A-Z0-9a-z_]', {value: '\\W'}],
['[^A-Z0-9_a-z]', {value: '\\W'}],
['[^A-Z_a-z0-9]', {value: '\\W'}],
['[^A-Z_0-9a-z]', {value: '\\W'}],
['[^0-9a-zA-Z_]', {value: '\\W'}],
['[^0-9a-z_A-Z]', {value: '\\W'}],
['[^0-9A-Za-z_]', {value: '\\W'}],
['[^0-9A-Z_a-z]', {value: '\\W'}],
['[^0-9_a-zA-Z]', {value: '\\W'}],
['[^0-9_A-Za-z]', {value: '\\W'}],
['[^_a-zA-Z0-9]', {value: '\\W'}],
['[^_a-z0-9A-Z]', {value: '\\W'}],
['[^_A-Za-z0-9]', {value: '\\W'}],
['[^_A-Z0-9a-z]', {value: '\\W'}],
['[^_0-9a-zA-Z]', {value: '\\W'}],
['[^_0-9A-Za-z]', {value: '\\W'}],
// Non-word with digit
['[^a-zA-Z\\d_]', {value: '\\W'}],
['[^a-zA-Z_\\d]', {value: '\\W'}],
['[^a-z\\dA-Z_]', {value: '\\W'}],
['[^a-z\\d_A-Z]', {value: '\\W'}],
['[^a-z_A-Z\\d]', {value: '\\W'}],
['[^a-z_\\dA-Z]', {value: '\\W'}],
['[^A-Za-z\\d_]', {value: '\\W'}],
['[^A-Za-z_\\d]', {value: '\\W'}],
['[^A-Z\\da-z_]', {value: '\\W'}],
['[^A-Z\\d_a-z]', {value: '\\W'}],
['[^A-Z_a-z\\d]', {value: '\\W'}],
['[^A-Z_\\da-z]', {value: '\\W'}],
['[^\\da-zA-Z_]', {value: '\\W'}],
['[^\\da-z_A-Z]', {value: '\\W'}],
['[^\\dA-Za-z_]', {value: '\\W'}],
['[^\\dA-Z_a-z]', {value: '\\W'}],
['[^\\d_a-zA-Z]', {value: '\\W'}],
['[^\\d_A-Za-z]', {value: '\\W'}],
['[^_a-zA-Z\\d]', {value: '\\W'}],
['[^_a-z\\dA-Z]', {value: '\\W'}],
['[^_A-Za-z\\d]', {value: '\\W'}],
['[^_A-Z\\da-z]', {value: '\\W'}],
['[^_\\da-zA-Z]', {value: '\\W'}],
['[^_\\dA-Za-z]', {value: '\\W'}],
// Word with case insensitivity
['[a-z0-9_]', {value: '\\w', flags: 'i'}],
['[a-z_0-9]', {value: '\\w', flags: 'i'}],
['[0-9a-z_]', {value: '\\w', flags: 'i'}],
['[0-9_a-z]', {value: '\\w', flags: 'i'}],
['[_a-z0-9]', {value: '\\w', flags: 'i'}],
['[_0-9a-z]', {value: '\\w', flags: 'i'}],
['[^a-z0-9_]', {value: '\\W', flags: 'i'}],
// Word with case insensitivity and digit
['[a-z\\d_]', {value: '\\w', flags: 'i'}],
['[a-z_\\d]', {value: '\\w', flags: 'i'}],
['[\\da-z_]', {value: '\\w', flags: 'i'}],
['[\\d_a-z]', {value: '\\w', flags: 'i'}],
['[_a-z\\d]', {value: '\\w', flags: 'i'}],
['[_\\da-z]', {value: '\\w', flags: 'i'}],
// Non-word with case insensitivity
['[^a-z0-9_]', {value: '\\W', flags: 'i'}],
['[^a-z_0-9]', {value: '\\W', flags: 'i'}],
['[^0-9a-z_]', {value: '\\W', flags: 'i'}],
['[^0-9_a-z]', {value: '\\W', flags: 'i'}],
['[^_a-z0-9]', {value: '\\W', flags: 'i'}],
['[^_0-9a-z]', {value: '\\W', flags: 'i'}],
// Non-word with case insensitivity and digit
['[^a-z\\d_]', {value: '\\W', flags: 'i'}],
['[^a-z_\\d]', {value: '\\W', flags: 'i'}],
['[^\\da-z_]', {value: '\\W', flags: 'i'}],
['[^\\d_a-z]', {value: '\\W', flags: 'i'}],
['[^_a-z\\d]', {value: '\\W', flags: 'i'}],
['[^_\\da-z]', {value: '\\W', flags: 'i'}]
]);
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sam Verschueren <sam.verschueren@gmail.com> (github.com/SamVerschueren)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+75
View File
@@ -0,0 +1,75 @@
{
"_args": [
[
"clean-regexp@1.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_development": true,
"_from": "clean-regexp@1.0.0",
"_id": "clean-regexp@1.0.0",
"_inBundle": false,
"_integrity": "sha1-jffHquUf02h06PjQW5GAvBGj/tc=",
"_location": "/clean-regexp",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "clean-regexp@1.0.0",
"name": "clean-regexp",
"escapedName": "clean-regexp",
"rawSpec": "1.0.0",
"saveSpec": null,
"fetchSpec": "1.0.0"
},
"_requiredBy": [
"/eslint-plugin-unicorn"
],
"_resolved": "https://registry.npmjs.org/clean-regexp/-/clean-regexp-1.0.0.tgz",
"_spec": "1.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"author": {
"name": "Sam Verschueren",
"email": "sam.verschueren@gmail.com",
"url": "github.com/SamVerschueren"
},
"bugs": {
"url": "https://github.com/SamVerschueren/clean-regexp/issues"
},
"dependencies": {
"escape-string-regexp": "^1.0.5"
},
"description": "Clean up regular expressions",
"devDependencies": {
"ava": "*",
"xo": "*"
},
"engines": {
"node": ">=4"
},
"files": [
"index.js",
"lib"
],
"homepage": "https://github.com/SamVerschueren/clean-regexp#readme",
"keywords": [
"regex",
"regexp",
"regular",
"expression",
"clean",
"cleanup",
"digit",
"word"
],
"license": "MIT",
"name": "clean-regexp",
"repository": {
"type": "git",
"url": "git+https://github.com/SamVerschueren/clean-regexp.git"
},
"scripts": {
"test": "xo && ava"
},
"version": "1.0.0"
}
+67
View File
@@ -0,0 +1,67 @@
# clean-regexp [![Build Status](https://travis-ci.org/SamVerschueren/clean-regexp.svg?branch=master)](https://travis-ci.org/SamVerschueren/clean-regexp)
> Clean up regular expressions
## Install
```
$ npm install clean-regexp
```
## Usage
```js
const cleanRegexp = require('clean-regexp');
cleanRegexp('[0-9]');
//=> '\\d'
cleanRegexp('[^0-9]');
//=> '\\D'
cleanRegexp('[a-zA-Z0-9_]');
//=> '\\w'
cleanRegexp('[a-z0-9_]', 'i');
//=> '\\w'
cleanRegexp('[^a-zA-Z0-9_]');
//=> '\\W'
cleanRegexp('[^a-z0-9_]', 'i');
//=> '\\W'
cleanRegexp('[a-zA-Z\\d_]');
//=> '\\w'
cleanRegexp('[^a-zA-Z\\d_]');
//=> '\\W'
cleanRegexp('[0-9]+\\.[a-zA-Z0-9_]?');
//=> '\\d+\\.\\w'
```
## API
### cleanRegexp(regexp, [flags])
#### regexp
Type: `string`
Text of the regular expression.
#### flags
Type: `string`<br>
Default: `''`
Flags of the regular expression.
## License
MIT © [Sam Verschueren](https://github.com/SamVerschueren)