This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/regexp-tree/dist/compat-transpiler/index.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-08-11 18:48:02 +00:00
/**
* 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;
}
};