Sweet.js prefix a match in a template - sweet.js

Given this macro
macro type {
case {_ $attr } =>
{
return #{
var a = obj.some$attr
}
}
}
type Attr
I'm trying to get this output:
var a = obj.someAttr;
But what I get is
var a = obj.some$attr;
Adding any non alphanumeric character between some and $attr correctly outputs the bound value.

Ok just figured out how to do this
enter code here
macro type {
case { $mName $attr } =>
{
var wrapperName = makeIdent('some' + unwrapSyntax(#{$attr}), #{$mName});
letstx $x = [wrapperName];
return #{
var a = obj.$x
}
}
}

Related

Symfony get className object from String class declaration

I have this code that returns column names of the className declared in the 2nd line :
public function listColumns(EntityManagerInterface $em ) {
$class = $em->getClassMetadata(Assure::class);
$fields = [];
if (!empty($class->discriminatorColumn)) {
$fields[] = $class->discriminatorColumn['name'];
}
$fields = array_merge($class->getColumnNames(), $fields);
foreach ($fields as $index => $field) {
if ($class->isInheritedField($field)) {
unset($fields[$index]);
}
}
foreach ($class->getAssociationMappings() as $name => $relation) {
if (!$class->isInheritedAssociation($name)){
foreach ($relation['joinColumns'] as $joinColumn) {
$fields[] = $joinColumn['name'];
}
}
}
return $fields;
}
I am trying to make this function parametrable so I can give it every time which table/className I am trying to get its columns
This is a possible solution to do what I want (extracting table column names ) differently :
public function listColumns2(EntityManagerInterface $em ) {
$conn = $this->getEntityManager()->getConnection();
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =
N'Assure' ";
$stmt = $conn->prepare($sql);
$stmt->execute();
return $stmt->fetchAllAssociative();
}

How to add dynamic values to field injections list with custom trigger to camunda properties panel?

I have two questions here
Is it possible to add dynamic lists values to field injection list input ?
Can I create a trigger for this so this can be initiated from any other input selection say a class selection will populate all fields
I was just looking into FieldInjection.js whether that can be extented for the same
Can someone please provide a hint or direction for this ?
Thanks.
For anyone interested in the answer, I was able to achieve the above goal by changing the set function of the Java Class select input as folllowing
few imports
var extensionElementsHelper = require('../../../../helper/ExtensionElementsHelper'),
elementHelper = require('../../../../helper/ElementHelper')
var CAMUNDA_FIELD_EXTENSION_ELEMENT = 'camunda:Field';
function getExtensionFields(bo) {
return bo && extensionElementsHelper.getExtensionElements(bo, CAMUNDA_FIELD_EXTENSION_ELEMENT) || [];
}
then changing the set function to create extension element and push the field values as :
set: function(element, values, node) {
var bo = getBusinessObject(element);
var type = getImplementationType(element);
var attr = getAttribute(type);
var prop = {}
var commands = [];
prop[attr] = values.delegate || '';
var extensionElements = getExtensionFields(bo);
//remove any extension elements existing before
extensionElements.forEach(function(ele){
commands.push(extensionElementsHelper.removeEntry(getBusinessObject(element), element, ele));
});
if(prop[attr] !== ""){
var extensionElements = elementHelper.createElement('bpmn:ExtensionElements', { values: [] }, bo, bpmnFactory);
commands.push(cmdHelper.updateBusinessObject(element, bo, { extensionElements: extensionElements }));
var arrProperties = ["private org.camunda.bpm.engine.delegate.Expression com.cfe.extensions.SampleJavaDelegate.varOne","private org.camunda.bpm.engine.delegate.Expression com.cfe.extensions.SampleJavaDelegate.varTwo"]
var newFieldElem = "";
arrProperties.forEach(function(prop){
var eachProp = {
name:"",
string:"",
expression:""
}
var type = prop.split(" ")[1].split(".").reverse()[0];
var val = prop.split(" ")[2].split(".").reverse()[0];
eachProp.name = val;
if( type == "String"){
eachProp.string = "${" + val +" }"
}else if( type == "Expression"){
eachProp.expression = "${" + val +" }"
}
newFieldElem = elementHelper.createElement(CAMUNDA_FIELD_EXTENSION_ELEMENT, eachProp, extensionElements, bpmnFactory);
commands.push(cmdHelper.addElementsTolist(element, extensionElements, 'values', [ newFieldElem ]));
});
}
commands.push(cmdHelper.updateBusinessObject(element, bo, prop));
return commands;
}
Cheers !.

Swift 2 NSRegularExpression

Eventually I want to be able to input a string like "\mycard{front1}{back1} \mycard{front2}{back2} \mycard{front3}{back3}" and return the front and back of each card.
I found this website on NSRegularExpression, but I'm having a hard time adjusting it to my problem.
Here is what I have so far.
import Foundation
func rangeFromNSRange(nsRange: NSRange, forString str: String) -> Range<String.Index>? {
let fromUTF16 = str.utf16.startIndex.advancedBy(nsRange.location, limit: str.utf16.endIndex)
let toUTF16 = fromUTF16.advancedBy(nsRange.length, limit: str.utf16.endIndex)
if let from = String.Index(fromUTF16, within: str), let to = String.Index(toUTF16, within: str) {
return from ..< to
}
return nil
}
do {
// let input = "My name is Taylor Swift"
// let regex = try NSRegularExpression(pattern: "My name is (.*)", options: NSRegularExpressionOptions.CaseInsensitive)
let input = "mycard{front}{back}"
let regex = try NSRegularExpression(pattern: "mycard{(.*)}{(.*)}", options: NSRegularExpressionOptions.CaseInsensitive)
let matches = regex.matchesInString(input, options: [], range: NSMakeRange(0, input.characters.count))
if let match = matches.first {
let range = match.rangeAtIndex(1)
if let swiftRange = rangeFromNSRange(range, forString: input) {
let name = input.substringWithRange(swiftRange)
}
}
} catch {
// regex was bad!
}
As stated in my comment you need to escape the { and }. That results in the following regex: mycard\\{(.*)\\}\\{(.*)\\}.
You then might want to change your match logic a little bit to output the expected results:
if let match = matches.first {
for i in 1..<match.numberOfRanges {
let range = match.rangeAtIndex(i)
if let swiftRange = rangeFromNSRange(range, forString: input) {
let name = input.substringWithRange(swiftRange)
print(name)
}
}
}
Which outputs
front
back
If you want to match multiple cards use the following regex:
mycard\\{([^{]*)\\}\\{([^{]*)\\}
Then iterate over the matches
for match in matches {
for i in 1..<match.numberOfRanges {
let range = match.rangeAtIndex(i)
if let swiftRange = rangeFromNSRange(range, forString: input) {
let name = input.substringWithRange(swiftRange)
print(name)
}
}
}
For the input mycard{front}{back} mycard{front1}{back1} the output correctly is
front
back
front1
back1
I gave up on regex. I just don't think it will do the trick here. I came up with another solution.
import Foundation
extension String {
subscript (r: Int) -> Character? {
var cur = 0
for char in self.characters {
if cur == r {
return char
}
cur += 1
}
return nil
}
subscript (r: Range<Int>) -> String {
return substringWithRange(Range(start: startIndex.advancedBy(r.startIndex), end: startIndex.advancedBy(r.endIndex)))
}
func parseBrackets () -> [String]? {
var list: [String] = []
var level = 0
var start = 0
for var i=0; i < self.characters.count - 1; i++ {
if self[i] == "{" {
level += 1
if level == 1 {
start = i + 1
}
} else if self[i] == "}" {
if level == 1 {
list.append(self[start..<i])
}
level -= 1
}
}
if list.count > 0 {
return list
} else {
return nil
}
}
}
let testString = "mycard{f{}ront}{termins{x}{n}} mycard{front1}{back1} mycard{front2}{back2}"
let list = testString.parseBrackets()
for a in list! {
print(a)
}
Which gives the desired output
f{}ront
termins{x}{n}
front1
back1
front2

Find out if a Regex could match

Lets say P is a regex pattern defined as "AB" where A,B are subpatterns. There is a string T which is tested against pattern P. I want to find out if T is a partial match of P, this means T machtes against A but not B since it is not long enough to match B. T could be a match if it was combined with a string U which is matched by B.
var A = "[a-z]{3}";
var B = "[0-9]{2}"
var P = A + B;
var T = "abc";
var U = "20";
var macthes_T = regex(P, T); // false
var matches_U = regex(P, U); // false
var matches_TU = regex(P, T + U); // true
var couldMatch_T = magic(P, T); // true
var couldMatch_U = magic(P, U); // false
var couldMatch_TU = magic(P, T + U); // true
Now I want to doe this recursive on A since its the "start" of P and "implement" the magic function for input T, which would look like this:
var A_1 = "[a-z]";
var A_2 = "[a-z]";
var A_3 = "[a-z]";
var A = A_1 + A_2 + A_3;
// T="" would return false
// T="a" would return true
var hasChar(T) {
return length(T) > 0;
}
// T="ab" would return a and T is "b"
var getChar(T) {
var c = T[0];
T = substring(T, 1);
return c;
}
// Let x element of [a-z]
// would return true for "", "x", "xx", "xxx"
// would return false for any other input T
var magic_A(T) {
if(!hasChar(T)) {
return true;
}
if(!regex(A_1, getChar(T))) {
return false;
}
if(!hasChar(T)) {
return true;
}
if(!regex(A_2, getChar(T))) {
return false;
}
if(!hasChar(T)) {
return true;
}
if(!regex(A_3, getChar(T))) {
return false;
}
return true;
}
umm something like this?
\b(a|ab)[a-z]*
this one check if there's a or ab, then any char between a-z following behind and wrap the whole word as matching
http://regexr.com/3aa5n
just noticed that your code won't work with regex
here's some reference http://www.dotnetperls.com/regex-match

Replacing multiple URLs in a string by a links with regex

I'm using a filter with AngularJS to replace URLs in strings by window.open() function and email by mailto :
app.filter('parseUrl', function() {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/gim;
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
//Change email addresses to mailto:: links.
var replacePattern3 = /(\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
return function(text, target, otherProp) {
var originText = text;
if(text == undefined || text == "") {
return "";
} else {
angular.forEach(text.match(replacePattern1), function(url) {
text = text.replace(replacePattern1, "<span onclick=\"window.open('$1', '_system');\" class='link_url'>$1</span>");
});
angular.forEach(text.match(replacePattern2), function(url) {
text = text.replace(replacePattern2, "<span onclick=\"window.open('http://$2', '_system');\" class='link_url'>$1$2</span>");
});
angular.forEach(text.match(replacePattern3), function(url) {
text = text.replace(replacePattern3, "$1");
});
return text;
}
};
});
It works well with one URL or one email, but when I have two or more URLs it's not working because it replace a multiple times the URLs by a span with window.open() function. As you can see in this JSFiddle : http://jsfiddle.net/wps94/1/
Do you have an idea to avoid this ? Maybe by changing the regex ?
Thank you
Try using a single regex and a replacement function:
var replacePattern = /\b((http:\/\/|https:\/\/|ftp:\/\/|mailto:|news:)|www\.|ftp\.|[^ \,\;\:\!\)\(\""\'\<\>\f\n\r\t\v]+#)([^ \,\;\:\!\)\(\""\'\<\>\f\n\r\t\v]+)\b/gim;
return function(text, target, otherProp) {
var originText = text;
if(text == undefined || text == "") {
return "";
} else {
return text.replace(replacePattern, function($0, $1) {
var match = $0;
var protocol = $1;
if ((/^www\./i).test(match))
{
return "<span onclick=\"window.open('http://" + match + "', '_system');\" class='link_url'>" + match + "</span>";
}
if ((/^ftp\./i).test(match))
{
return "<span onclick=\"window.open('ftp://" + match + "', '_system');\" class='link_url'>" + match + "</span>";
}
if (protocol && protocol.charAt(0) === '#')
{
return "" + match + "";
}
return "<span onclick=\"window.open('" + match + "', '_system');\" class='link_url'>" + match + "</span>";
});
}
};
http://jsfiddle.net/wps94/2/
In the forEachs, change text = to newText = and then return that instead…like this:
app.filter('parseUrl', function() {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/gim;
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
//Change email addresses to mailto:: links.
var replacePattern3 = /(\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
return function(text, target, otherProp) {
var originText = text;
if(text == undefined || text == "") {
return "";
} else {
var newText;
angular.forEach(text.match(replacePattern1), function(url) {
newText = text.replace(replacePattern1, "<span onclick=\"window.open('$1', '_system');\" class='link_url'>$1</span>");
});
angular.forEach(text.match(replacePattern2), function(url) {
newText = text.replace(replacePattern2, "<span onclick=\"window.open('http://$2', '_system');\" class='link_url'>$1$2</span>");
});
angular.forEach(text.match(replacePattern3), function(url) {
newText = text.replace(replacePattern3, "$1");
});
return newText;
}
};
});
Each pass through the forEach was looking at the value of text with the replacement already made for the first URL rather than the initial value.