Using CoffeeScript string interpolation inside a for loop - coffeescript

I am filling an html <select> list with <option> elements based on a blob of JSON data. I would like to tidy up my code by using string interpolation but I cannot get the values to substitute correctly.
Here is the code that works (no interpolation):
$list
.empty()
.append('<option value="' + item.Id + '">' + item.Name + '</option>' for item in data)
Here is how I would like to do things (does not work):
$list
.empty()
.append('<option value="#{item.Id}">#{item.Name}</option>' for item in data)
Here is an example of the JSON I am using:
[
{"Id":"1","Name":"Client-1"},
{"Id":"2","Name":"Client-2"}
]
The substitutions do not happen, instead I just get a list filled with the correct number of #{item.Name} strings.
Is it possible to use CoffeeScript string interpolation inside a for loop like this?
Thanks.

String interpolation only works with double-quoted strings, not apostrophe-quoted strings.
http://coffeescript.org/#strings
This should work:
$list
.empty()
.append("<option value=\"#{item.Id}\">#{item.Name}</option>" for item in data)

Related

Split a string based on "|" character in PowerShell

I have a string variable in PowerShell which contains the value:
NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
I am attempting to get the beginning portion of that string into it's own variable by identifying the index of the "|" character and using a substring function to extract the first portion of the string, in this case "NFP". I am not sure how to escape the "|" so I can use it properly. It doesn't seem to recognize it at all. My latest attempt is as follows:
$PolicyManual = $Item["PolicyManual"]
write-host $PolicyManual #Displays NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
if ($PolicyManual.Contains([regex]::escape("|"))) {
$PolcyManual = $PolicyManual.Substring(0, $PolicyManual.IndexOf([regex]::escape("|")))
}
I'm sure this is simple, but I can't figure out how to make it work. Can anyone offer assistance to a PowerShell novice?
Thanks.
The problem is that .contains method doesn't know about regex and you are never entering the if condition because of this. When you do [regex]::escape("|"), the method is looking for a literal \|.
Try this instead:
$PolicyManual = "NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8"
if ($PolicyManual.Contains('|')) {
$element0, $element1 = $PolicyManual.Split('|')
$element0 #=> NFP
$element1 #=> 8dc3b47a-48eb-4696-abe2-48729beb63c8
}

Wix: Populate repeater with external API call

I'm referring to the following video How to Create A Web App With External API Access using Wix Code & wanted to know how I would populate a repeater rather than populating a paragraph tag as shown in the youtube video mentioned above.
Basically here is the pseudocode of what I would like to achieve:
If search box is equal to null or empty
Display all crypto currencie(s)
else
Display single crypto currency
Putting the info in a repeater isn't too different than what the example already shows. Actually, when the search box is empty, the API returns an array that just needs a little playing with to get it to work with a repeater.
So, assuming you added a repeater with the ID repeater1 that contains a text element with the id result, you can make the following minor changes to the page code. You don't need to touch the backend code at all.
First, in the button1_click event handler we'll remove the code that populates the text element with the data returned from the API. Instead, we'll add an _id property to each currency object (required for the repeater) and then feed that data to the repeater.
export function button1_click(event) {
getCryptoCurrencyInfo($w("#currencyInput").value)
.then(currencyInfo => {
// add an _id property to each currency object
currencyInfo.forEach(item => item._id = item.id);
// feed the data to the repeater
$w('#repeater1').data = currencyInfo;
} );
}
Then, we can take the code for populating the text element and stick it in the repeater1_itemReady event handler. This function will run once for each currency item in the array fed to the repeater's data property. Make sure you use the properties panel to wire the function to the matching repeater event.
export function repeater1_itemReady($item, itemData, index) {
$item("#result").text = "Name: " + itemData.name + "\n"
+ "Symbol: " + itemData.symbol + "\n"
+ "Rank: " + itemData.rank + "\n"
+ "Price (USD): " + itemData.price_usd + "\n"
+ "Market Capitalization (USD): " + itemData.market_cap_usd + "\n"
+ "Percent Change 1h: " + itemData.percent_change_1h + "\n"
+ "Percent Change 24h: " + itemData.percent_change_24h + "\n"
+ "Percent Change 7d: " + itemData.percent_change_7d;
}
Notice two subtle changes to the code. First, we use $item instead of $w to select the text element. This selects the specific instance of the text element in the current repeater element. Second, we use itemData instead of currencyInfo[0]. This gives us the specific data that is associated with the current repeater element.

How to use HTML style in a string?

suppose i have a string like this:
string =
"<style>li { list-style: none; }</style>
<li><b>Source:</b> $Source</li>
<li><b>Security:</b> $Security</li>
"
I still get bullet points
i cant use it like this because the string is already wrapped in double quotes due to $variables like $Source
<li style="list-style: none;">
I get Unexpected token 'list-style:' in expression or statement. because i cant use double quotes
so my only choice is using it like this:
<style>li { list-style: none; }</style>
but it doesnt get applied...why is that?
UPDATE: To clarify, i want to utilize the listing mechanism WITHOUT bullet points showing up
If you set string variables with single quotes, those values can contain literal double quotes. The problem with this is that everything within the single quotes will be treated as a literal string, which means $Source and $Security would not get expanded. If you are going to use double quotes and variables within the same string, I suggest escaping the inner double quotes with a backtick.
$string =
"<ul style=`"list-style-type:none; padding-left:0`">
<li><b>Source:</b> $Source</li>
<li><b>Security:</b> $Security</li>
</ul>
"
The list-style-type:none property sets the list item marker to none. 'padding-left:0 removes the left indentation of the list.
To test, just output the contents to a file (s.html) and open the file from a browser.
$string | Set-Content s.html
If you are testing this in an email client like Outlook, results may vary. Outlook does not support list-style-type: none.
If you just need a list with bolded properties for purposes of reading in an email client, you can simplify this. Then use Send-Mailmessage with the -BodyAsHtml switch with the following string as the body.
$string = "
<b>Sources:</b> $source<br>
<b>Security:</b> $security<br>
"

Can't unescape escaped string with ABAP

I want to escape this string in SAPUI5 like this.
var escapedLongText = escape(unescapedLongText);
String (UTF-8 quote, space, Unicode quote)
" “
Escaped string
%22%20%u201C
I want to unescape it with this method, but it returns empty. Any ideas?
DATA: LV_STRING TYPE STRING.
LV_STRING = '%22%20%u201C'.
CALL METHOD CL_HTTP_UTILITY=>UNESCAPE_URL
EXPORTING
ESCAPED = LV_STRING
RECEIVING
UNESCAPED = LV_STRING.
I changed the code in SAPUI5 to the following:
var escapedLongText = encodeURI(unescapedLongText);
This results in: (like andreas mentioned)
%22%20%e2%80%9c
If I want to decode it later in SAPUI5, it can be done like this:
var unescapedLongText = unescape(decodeURI(escapedLongText));
The unescape needs to be done, because commas (for example) don't seem to be decoded automatically.

JAVASCRIPT: Replace a single character into a <p>

I've got a word in an HTML paragraph. For example 'string':
<p id="myWord">string</p>
I want to change only a character of that word. For example 'strong':
So I add an html button that calls a function:
<button onclick="myFunction">replace 'i'</button>
the function in JS is this but it can't access to the fourth character of myWord:
function myFunction{
document.getElementById("myWord[3]").innerHTML = 'o';
}
Here is the unWorking fiddle: https://jsfiddle.net/su3Lrezr/4/
How can I access to a index of a word in a html paragraph? I already tried to convert myWord into an array with split() method but it doesn't work.
Thanks everybody
Array method:
You'll want to split the string at value 'i' Then rejoin them together with an 'o' in the middle. Then update your innerHTML. You will need the split() function. This will work:
function myFunction(){
var word = document.getElementById('myWord').innerHTML;
var result = word.split('i');
var newword = result[0] + "o" + result[1];
document.getElementById('myWord').innerHTML = newword;
}
Also, as a commenter has mentioned your syntax for calling functions is wrong. You must call functions like this: myFunction() and declare them like this: function myFunction(){ }