50 lines
No EOL
1.4 KiB
JavaScript
50 lines
No EOL
1.4 KiB
JavaScript
/**
|
|
* The MIT License (MIT)
|
|
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var compatTransforms = require('./transforms');
|
|
var _transform = require('../transform');
|
|
|
|
module.exports = {
|
|
/**
|
|
* Translates a regexp in new syntax to equivalent regexp in old syntax.
|
|
*
|
|
* @param string|RegExp|AST - regexp
|
|
* @param Array transformsWhitelist - names of the transforms to apply
|
|
*/
|
|
transform: function transform(regexp) {
|
|
var transformsWhitelist = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
|
|
var transformToApply = transformsWhitelist.length > 0 ? transformsWhitelist : Object.keys(compatTransforms);
|
|
|
|
var result = void 0;
|
|
|
|
// Collect extra data per transform.
|
|
var extra = {};
|
|
|
|
transformToApply.forEach(function (transformName) {
|
|
|
|
if (!compatTransforms.hasOwnProperty(transformName)) {
|
|
throw new Error('Unknown compat-transform: ' + transformName + '. ' + 'Available transforms are: ' + Object.keys(compatTransforms).join(', '));
|
|
}
|
|
|
|
var handler = compatTransforms[transformName];
|
|
|
|
result = _transform.transform(regexp, handler);
|
|
regexp = result.getAST();
|
|
|
|
// Collect `extra` transform result.
|
|
if (typeof handler.getExtra === 'function') {
|
|
extra[transformName] = handler.getExtra();
|
|
}
|
|
});
|
|
|
|
// Set the final extras for all transforms.
|
|
result.setExtra(extra);
|
|
|
|
return result;
|
|
}
|
|
}; |