Skip to content

Instantly share code, notes, and snippets.

@olostan
Created April 14, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olostan/d621ccc0a8d45784d1be2bddc9b15afd to your computer and use it in GitHub Desktop.
Save olostan/d621ccc0a8d45784d1be2bddc9b15afd to your computer and use it in GitHub Desktop.
Converts from Directive Definition to Directive
<html>
<body>
<textarea id="a" cols=80 rows=20>@directiveDefinition("viewControlPanel", [])
export class ControlPanelView implements ng.IDirective {
restrict = 'E';
template = controlPanelViewTemplate;
bindToController = { activity: '=' };
controller = controlPanelViewController;
controllerAs = "ctrl";
require = { payload: "^uifDataPayload" };
scope = true;
}
</textarea>
<button id="c" >Convert</button>
<textarea id="b" cols=80 rows=20></textarea>
<script>
function converter(m,dir,param) {
param = param.replace(/;/g,',');
param = param.replace(/\s+=\s+/g,' : ');
param = param.replace(/\s*controller\s*:\s*\w+\s*,/g,'');
console.log(param);
return `@directive(${dir},${param})`;
}
document.getElementById('c').addEventListener('click', function() {
let text = document.getElementById('a').value;
text = text.replace(/@directiveDefinition\((.*?)(?:,\s*\[\])?\)[\s\S]*export class \w+ implements (?:angular|ng)\.IDirective([\s\S]*?})[\s]*$/mg,converter);
document.getElementById('b').value = text;
});
document.getElementById('c').click();
</script>
<body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment