regexpu-core
Advanced tools
Comparing version 2.0.0 to 3.0.1
@@ -1,15 +0,17 @@ | ||
// Generated by `/scripts/character-class-escape-sets.js`. Do not edit. | ||
var regenerate = require('regenerate'); | ||
// Generated using `npm run build`. Do not edit. | ||
'use strict'; | ||
exports.REGULAR = { | ||
'd': regenerate() | ||
.addRange(0x30, 0x39), | ||
'D': regenerate() | ||
const regenerate = require('regenerate'); | ||
exports.REGULAR = new Map([ | ||
['d', regenerate() | ||
.addRange(0x30, 0x39)], | ||
['D', regenerate() | ||
.addRange(0x0, 0x2F) | ||
.addRange(0x3A, 0xFFFF), | ||
's': regenerate(0x20, 0xA0, 0x1680, 0x202F, 0x205F, 0x3000, 0xFEFF) | ||
.addRange(0x3A, 0xFFFF)], | ||
['s', regenerate(0x20, 0xA0, 0x1680, 0x202F, 0x205F, 0x3000, 0xFEFF) | ||
.addRange(0x9, 0xD) | ||
.addRange(0x2000, 0x200A) | ||
.addRange(0x2028, 0x2029), | ||
'S': regenerate() | ||
.addRange(0x2028, 0x2029)], | ||
['S', regenerate() | ||
.addRange(0x0, 0x8) | ||
@@ -25,25 +27,25 @@ .addRange(0xE, 0x1F) | ||
.addRange(0x3001, 0xFEFE) | ||
.addRange(0xFF00, 0xFFFF), | ||
'w': regenerate(0x5F) | ||
.addRange(0xFF00, 0xFFFF)], | ||
['w', regenerate(0x5F) | ||
.addRange(0x30, 0x39) | ||
.addRange(0x41, 0x5A) | ||
.addRange(0x61, 0x7A), | ||
'W': regenerate(0x60) | ||
.addRange(0x61, 0x7A)], | ||
['W', regenerate(0x60) | ||
.addRange(0x0, 0x2F) | ||
.addRange(0x3A, 0x40) | ||
.addRange(0x5B, 0x5E) | ||
.addRange(0x7B, 0xFFFF) | ||
}; | ||
.addRange(0x7B, 0xFFFF)] | ||
]); | ||
exports.UNICODE = { | ||
'd': regenerate() | ||
.addRange(0x30, 0x39), | ||
'D': regenerate() | ||
exports.UNICODE = new Map([ | ||
['d', regenerate() | ||
.addRange(0x30, 0x39)], | ||
['D', regenerate() | ||
.addRange(0x0, 0x2F) | ||
.addRange(0x3A, 0x10FFFF), | ||
's': regenerate(0x20, 0xA0, 0x1680, 0x202F, 0x205F, 0x3000, 0xFEFF) | ||
.addRange(0x3A, 0x10FFFF)], | ||
['s', regenerate(0x20, 0xA0, 0x1680, 0x202F, 0x205F, 0x3000, 0xFEFF) | ||
.addRange(0x9, 0xD) | ||
.addRange(0x2000, 0x200A) | ||
.addRange(0x2028, 0x2029), | ||
'S': regenerate() | ||
.addRange(0x2028, 0x2029)], | ||
['S', regenerate() | ||
.addRange(0x0, 0x8) | ||
@@ -59,25 +61,25 @@ .addRange(0xE, 0x1F) | ||
.addRange(0x3001, 0xFEFE) | ||
.addRange(0xFF00, 0x10FFFF), | ||
'w': regenerate(0x5F) | ||
.addRange(0xFF00, 0x10FFFF)], | ||
['w', regenerate(0x5F) | ||
.addRange(0x30, 0x39) | ||
.addRange(0x41, 0x5A) | ||
.addRange(0x61, 0x7A), | ||
'W': regenerate(0x60) | ||
.addRange(0x61, 0x7A)], | ||
['W', regenerate(0x60) | ||
.addRange(0x0, 0x2F) | ||
.addRange(0x3A, 0x40) | ||
.addRange(0x5B, 0x5E) | ||
.addRange(0x7B, 0x10FFFF) | ||
}; | ||
.addRange(0x7B, 0x10FFFF)] | ||
]); | ||
exports.UNICODE_IGNORE_CASE = { | ||
'd': regenerate() | ||
.addRange(0x30, 0x39), | ||
'D': regenerate() | ||
exports.UNICODE_IGNORE_CASE = new Map([ | ||
['d', regenerate() | ||
.addRange(0x30, 0x39)], | ||
['D', regenerate() | ||
.addRange(0x0, 0x2F) | ||
.addRange(0x3A, 0x10FFFF), | ||
's': regenerate(0x20, 0xA0, 0x1680, 0x202F, 0x205F, 0x3000, 0xFEFF) | ||
.addRange(0x3A, 0x10FFFF)], | ||
['s', regenerate(0x20, 0xA0, 0x1680, 0x202F, 0x205F, 0x3000, 0xFEFF) | ||
.addRange(0x9, 0xD) | ||
.addRange(0x2000, 0x200A) | ||
.addRange(0x2028, 0x2029), | ||
'S': regenerate() | ||
.addRange(0x2028, 0x2029)], | ||
['S', regenerate() | ||
.addRange(0x0, 0x8) | ||
@@ -93,12 +95,12 @@ .addRange(0xE, 0x1F) | ||
.addRange(0x3001, 0xFEFE) | ||
.addRange(0xFF00, 0x10FFFF), | ||
'w': regenerate(0x5F, 0x17F, 0x212A) | ||
.addRange(0xFF00, 0x10FFFF)], | ||
['w', regenerate(0x5F, 0x17F, 0x212A) | ||
.addRange(0x30, 0x39) | ||
.addRange(0x41, 0x5A) | ||
.addRange(0x61, 0x7A), | ||
'W': regenerate(0x4B, 0x53, 0x60) | ||
.addRange(0x61, 0x7A)], | ||
['W', regenerate(0x4B, 0x53, 0x60) | ||
.addRange(0x0, 0x2F) | ||
.addRange(0x3A, 0x40) | ||
.addRange(0x5B, 0x5E) | ||
.addRange(0x7B, 0x10FFFF) | ||
}; | ||
.addRange(0x7B, 0x10FFFF)] | ||
]); |
123
package.json
{ | ||
"name": "regexpu-core", | ||
"version": "2.0.0", | ||
"description": "regexpu’s core functionality (i.e. `rewritePattern(pattern, flag)`), capable of translating ES6 Unicode regular expressions to ES5.", | ||
"homepage": "https://mths.be/regexpu", | ||
"main": "rewrite-pattern.js", | ||
"keywords": [ | ||
"codegen", | ||
"desugaring", | ||
"ecmascript", | ||
"es5", | ||
"es6", | ||
"harmony", | ||
"javascript", | ||
"refactoring", | ||
"regex", | ||
"regexp", | ||
"regular expressions", | ||
"rewriting", | ||
"syntax", | ||
"transformation", | ||
"transpile", | ||
"transpiler", | ||
"unicode" | ||
], | ||
"license": "MIT", | ||
"author": { | ||
"name": "Mathias Bynens", | ||
"url": "https://mathiasbynens.be/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mathiasbynens/regexpu-core.git" | ||
}, | ||
"bugs": "https://github.com/mathiasbynens/regexpu-core/issues", | ||
"files": [ | ||
"LICENSE-MIT.txt", | ||
"rewrite-pattern.js", | ||
"data/character-class-escape-sets.js", | ||
"data/iu-mappings.json" | ||
], | ||
"scripts": { | ||
"build": "node scripts/iu-mappings.js && node scripts/character-class-escape-sets.js", | ||
"test": "mocha tests", | ||
"coverage": "istanbul cover --report html node_modules/.bin/_mocha tests/tests.js -- -u exports -R spec" | ||
}, | ||
"dependencies": { | ||
"regenerate": "^1.2.1", | ||
"regjsgen": "^0.2.0", | ||
"regjsparser": "^0.1.4" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.4.0", | ||
"jsesc": "^0.5.0", | ||
"lodash": "^3.6.0", | ||
"mocha": "^2.2.1", | ||
"regexpu-fixtures": "^2.0.0", | ||
"unicode-8.0.0": "^0.1.5" | ||
} | ||
"name": "regexpu-core", | ||
"version": "3.0.1", | ||
"description": "regexpu’s core functionality (i.e. `rewritePattern(pattern, flag)`), capable of translating ES6 Unicode regular expressions to ES5.", | ||
"homepage": "https://mths.be/regexpu", | ||
"main": "rewrite-pattern.js", | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"keywords": [ | ||
"codegen", | ||
"desugaring", | ||
"ecmascript", | ||
"es5", | ||
"es6", | ||
"harmony", | ||
"javascript", | ||
"refactoring", | ||
"regex", | ||
"regexp", | ||
"regular expressions", | ||
"rewriting", | ||
"syntax", | ||
"transformation", | ||
"transpile", | ||
"transpiler", | ||
"unicode" | ||
], | ||
"license": "MIT", | ||
"author": { | ||
"name": "Mathias Bynens", | ||
"url": "https://mathiasbynens.be/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mathiasbynens/regexpu-core.git" | ||
}, | ||
"bugs": "https://github.com/mathiasbynens/regexpu-core/issues", | ||
"files": [ | ||
"LICENSE-MIT.txt", | ||
"rewrite-pattern.js", | ||
"data/character-class-escape-sets.js", | ||
"data/iu-mappings.js" | ||
], | ||
"scripts": { | ||
"build": "node scripts/iu-mappings.js && node scripts/character-class-escape-sets.js", | ||
"test": "mocha tests", | ||
"cover": "istanbul cover --report html node_modules/.bin/_mocha tests -- -u exports -R spec" | ||
}, | ||
"dependencies": { | ||
"regenerate": "^1.3.1", | ||
"regenerate-unicode-properties": "^1.0.0", | ||
"regjsgen": "^0.3.0", | ||
"regjsparser": "^0.2.0", | ||
"unicode-loose-match": "^2.0.7" | ||
}, | ||
"devDependencies": { | ||
"codecov": "^1.0.1", | ||
"istanbul": "^0.4.0", | ||
"jsesc": "^2.1.0", | ||
"lodash": "^4.13.1", | ||
"mocha": "^2.2.1", | ||
"regexpu-fixtures": "^2.0.0", | ||
"unicode-8.0.0": "^0.6.0" | ||
} | ||
} |
@@ -1,33 +0,73 @@ | ||
var generate = require('regjsgen').generate; | ||
var parse = require('regjsparser').parse; | ||
var regenerate = require('regenerate'); | ||
var iuMappings = require('./data/iu-mappings.json'); | ||
var ESCAPE_SETS = require('./data/character-class-escape-sets.js'); | ||
'use strict'; | ||
function getCharacterClassEscapeSet(character) { | ||
if (unicode) { | ||
if (ignoreCase) { | ||
return ESCAPE_SETS.UNICODE_IGNORE_CASE[character]; | ||
const generate = require('regjsgen').generate; | ||
const parse = require('regjsparser').parse; | ||
const regenerate = require('regenerate'); | ||
const looseMatch = require('unicode-loose-match'); | ||
const knownUnicodeProperties = new Set( | ||
require('regenerate-unicode-properties') | ||
); | ||
const iuMappings = require('./data/iu-mappings.js'); | ||
const ESCAPE_SETS = require('./data/character-class-escape-sets.js'); | ||
const getCharacterClassEscapeSet = function(character) { | ||
if (config.unicode) { | ||
if (config.ignoreCase) { | ||
return ESCAPE_SETS.UNICODE_IGNORE_CASE.get(character); | ||
} | ||
return ESCAPE_SETS.UNICODE[character]; | ||
return ESCAPE_SETS.UNICODE.get(character); | ||
} | ||
return ESCAPE_SETS.REGULAR[character]; | ||
} | ||
return ESCAPE_SETS.REGULAR.get(character); | ||
}; | ||
var object = {}; | ||
var hasOwnProperty = object.hasOwnProperty; | ||
function has(object, property) { | ||
return hasOwnProperty.call(object, property); | ||
} | ||
const getUnicodePropertyValueSet = function(property, value) { | ||
const path = knownUnicodeProperties.has(property) ? | ||
`${ property }/${ value }` : | ||
`Binary_Property/${ property }`; | ||
try { | ||
return require(`regenerate-unicode-properties/${ path }.js`); | ||
} catch (exception) { | ||
throw new Error( | ||
`Failed to recognize value \`${ value }\` for property ` + | ||
`\`${ property }\`.` | ||
); | ||
} | ||
}; | ||
const getUnicodePropertyEscapeSet = function(value, isNegative) { | ||
const parts = value.split('='); | ||
let canonical; | ||
if (parts.length == 1) { | ||
const firstPart = parts[0]; | ||
// It could be a `General_Category` value or a binary property. | ||
canonical = looseMatch('General_Category', firstPart); | ||
if (!canonical.value) { | ||
// It’s not a `General_Category` value, so check if it’s a binary | ||
// property. Note: `looseMatch` throws on invalid properties. | ||
canonical = looseMatch(firstPart); | ||
} | ||
} else { | ||
// The pattern consists of two parts, i.e. `Property=Value`. | ||
canonical = looseMatch(parts[0], parts[1]); | ||
} | ||
const set = getUnicodePropertyValueSet( | ||
canonical.property, | ||
canonical.value | ||
).clone(); | ||
if (isNegative) { | ||
return UNICODE_SET.clone().remove(set); | ||
} | ||
return set; | ||
}; | ||
// Prepare a Regenerate set containing all code points, used for negative | ||
// character classes (if any). | ||
var UNICODE_SET = regenerate().addRange(0x0, 0x10FFFF); | ||
const UNICODE_SET = regenerate().addRange(0x0, 0x10FFFF); | ||
// Without the `u` flag, the range stops at 0xFFFF. | ||
// https://mths.be/es6#sec-pattern-semantics | ||
var BMP_SET = regenerate().addRange(0x0, 0xFFFF); | ||
const BMP_SET = regenerate().addRange(0x0, 0xFFFF); | ||
// Prepare a Regenerate set containing all code points that are supposed to be | ||
// matched by `/./u`. https://mths.be/es6#sec-atom | ||
var DOT_SET_UNICODE = UNICODE_SET.clone() // all Unicode code points | ||
const DOT_SET_UNICODE = UNICODE_SET.clone() // all Unicode code points | ||
.remove( | ||
@@ -42,3 +82,3 @@ // minus `LineTerminator`s (https://mths.be/es6#sec-line-terminators): | ||
// matched by `/./` (only BMP code points). | ||
var DOT_SET = DOT_SET_UNICODE.clone() | ||
const DOT_SET = DOT_SET_UNICODE.clone() | ||
.intersection(BMP_SET); | ||
@@ -49,5 +89,5 @@ | ||
regenerate.prototype.iuAddRange = function(min, max) { | ||
var $this = this; | ||
const $this = this; | ||
do { | ||
var folded = caseFold(min); | ||
const folded = caseFold(min); | ||
if (folded) { | ||
@@ -60,15 +100,4 @@ $this.add(folded); | ||
function assign(target, source) { | ||
for (var key in source) { | ||
// Note: `hasOwnProperty` is not needed here. | ||
target[key] = source[key]; | ||
} | ||
} | ||
function update(item, pattern) { | ||
// TODO: Test if memoizing `pattern` here is worth the effort. | ||
if (!pattern) { | ||
return; | ||
} | ||
var tree = parse(pattern, ''); | ||
const update = function(item, pattern) { | ||
let tree = parse(pattern, config.useUnicodeFlag ? 'u' : ''); | ||
switch (tree.type) { | ||
@@ -84,6 +113,6 @@ case 'characterClass': | ||
} | ||
assign(item, tree); | ||
} | ||
Object.assign(item, tree); | ||
}; | ||
function wrap(tree, pattern) { | ||
const wrap = function(tree, pattern) { | ||
// Wrap the pattern in a non-capturing group. | ||
@@ -94,20 +123,18 @@ return { | ||
'body': [tree], | ||
'raw': '(?:' + pattern + ')' | ||
'raw': `(?:${ pattern })` | ||
}; | ||
} | ||
}; | ||
function caseFold(codePoint) { | ||
return has(iuMappings, codePoint) ? iuMappings[codePoint] : false; | ||
} | ||
const caseFold = function(codePoint) { | ||
return iuMappings.get(codePoint) || false; | ||
}; | ||
var ignoreCase = false; | ||
var unicode = false; | ||
function processCharacterClass(characterClassItem) { | ||
var set = regenerate(); | ||
var body = characterClassItem.body.forEach(function(item) { | ||
const processCharacterClass = function(characterClassItem, regenerateOptions) { | ||
let set = regenerate(); | ||
const body = characterClassItem.body.forEach(function(item) { | ||
switch (item.type) { | ||
case 'value': | ||
set.add(item.codePoint); | ||
if (ignoreCase && unicode) { | ||
var folded = caseFold(item.codePoint); | ||
if (config.ignoreCase && config.unicode) { | ||
const folded = caseFold(item.codePoint); | ||
if (folded) { | ||
@@ -119,6 +146,6 @@ set.add(folded); | ||
case 'characterClassRange': | ||
var min = item.min.codePoint; | ||
var max = item.max.codePoint; | ||
const min = item.min.codePoint; | ||
const max = item.max.codePoint; | ||
set.addRange(min, max); | ||
if (ignoreCase && unicode) { | ||
if (config.ignoreCase && config.unicode) { | ||
set.iuAddRange(min, max); | ||
@@ -130,2 +157,5 @@ } | ||
break; | ||
case 'unicodePropertyEscape': | ||
set.add(getUnicodePropertyEscapeSet(item.value, item.negative)); | ||
break; | ||
// The `default` clause is only here as a safeguard; it should never be | ||
@@ -135,13 +165,13 @@ // reached. Code coverage tools should ignore it. | ||
default: | ||
throw Error('Unknown term type: ' + item.type); | ||
throw new Error(`Unknown term type: ${ item.type }`); | ||
} | ||
}); | ||
if (characterClassItem.negative) { | ||
set = (unicode ? UNICODE_SET : BMP_SET).clone().remove(set); | ||
set = (config.unicode ? UNICODE_SET : BMP_SET).clone().remove(set); | ||
} | ||
update(characterClassItem, set.toString()); | ||
update(characterClassItem, set.toString(regenerateOptions)); | ||
return characterClassItem; | ||
} | ||
}; | ||
function processTerm(item) { | ||
const processTerm = function(item, regenerateOptions) { | ||
switch (item.type) { | ||
@@ -151,12 +181,20 @@ case 'dot': | ||
item, | ||
(unicode ? DOT_SET_UNICODE : DOT_SET).toString() | ||
(config.unicode ? DOT_SET_UNICODE : DOT_SET) | ||
.toString(regenerateOptions) | ||
); | ||
break; | ||
case 'characterClass': | ||
item = processCharacterClass(item); | ||
item = processCharacterClass(item, regenerateOptions); | ||
break; | ||
case 'unicodePropertyEscape': | ||
update( | ||
item, | ||
getUnicodePropertyEscapeSet(item.value, item.negative) | ||
.toString(regenerateOptions) | ||
); | ||
break; | ||
case 'characterClassEscape': | ||
update( | ||
item, | ||
getCharacterClassEscapeSet(item.value).toString() | ||
getCharacterClassEscapeSet(item.value).toString(regenerateOptions) | ||
); | ||
@@ -171,6 +209,6 @@ break; | ||
case 'value': | ||
var codePoint = item.codePoint; | ||
var set = regenerate(codePoint); | ||
if (ignoreCase && unicode) { | ||
var folded = caseFold(codePoint); | ||
const codePoint = item.codePoint; | ||
const set = regenerate(codePoint); | ||
if (config.ignoreCase && config.unicode) { | ||
const folded = caseFold(codePoint); | ||
if (folded) { | ||
@@ -180,3 +218,3 @@ set.add(folded); | ||
} | ||
update(item, set.toString()); | ||
update(item, set.toString(regenerateOptions)); | ||
break; | ||
@@ -193,3 +231,3 @@ case 'anchor': | ||
default: | ||
throw Error('Unknown term type: ' + item.type); | ||
throw new Error(`Unknown term type: ${ item.type }`); | ||
} | ||
@@ -199,8 +237,22 @@ return item; | ||
module.exports = function(pattern, flags) { | ||
var tree = parse(pattern, flags); | ||
ignoreCase = flags ? flags.indexOf('i') > -1 : false; | ||
unicode = flags ? flags.indexOf('u') > -1 : false; | ||
assign(tree, processTerm(tree)); | ||
const config = { | ||
'ignoreCase': false, | ||
'unicode': false, | ||
'useUnicodeFlag': false | ||
}; | ||
const rewritePattern = function(pattern, flags, options) { | ||
const regjsparserFeatures = { | ||
'unicodePropertyEscape': options && options.unicodePropertyEscape | ||
}; | ||
config.useUnicodeFlag = options && options.useUnicodeFlag; | ||
const regenerateOptions = { | ||
'hasUnicodeFlag': config.useUnicodeFlag | ||
}; | ||
const tree = parse(pattern, flags, regjsparserFeatures); | ||
config.ignoreCase = flags && flags.includes('i'); | ||
config.unicode = flags && flags.includes('u'); | ||
Object.assign(tree, processTerm(tree, regenerateOptions)); | ||
return generate(tree); | ||
}; | ||
module.exports = rewritePattern; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18357
626
5
5
1
0
1
1
+ Addedunicode-loose-match@^2.0.7
+ Addedregenerate-unicode-properties@1.0.0(transitive)
+ Addedregjsgen@0.3.0(transitive)
+ Addedregjsparser@0.2.1(transitive)
+ Addedunicode-loose-match@2.7.0(transitive)
- Removedregjsgen@0.2.0(transitive)
- Removedregjsparser@0.1.5(transitive)
Updatedregenerate@^1.3.1
Updatedregjsgen@^0.3.0
Updatedregjsparser@^0.2.0