Is it possible to describe array of objects in one line without using braces? - coffeescript

I am learning CoffeeScript and got a question:
Is it possible to describe array of objects in one line?
So, I can do it in multiple lines without braces:
arr = [
name: "name",
age: 123
,
name: "anothername",
age: 321
]
But, is it possible to do this in one line?

Related

Why a "static" array doesn't work with GitHub contains workflow expression?

From the documentation, this works:
contains('Hello world', 'llo')
However, this will not:
contains(['6', '7', '8'], matrix.foo)
Unexpected symbol: '['. Located at position 10 within expression: contains(['6', '7', '8'], matrix.foo)
Is there any way to check the that matrix.foo is either 6, 7, or 8 using contains?
The contains function can't achieve the result you expect alone, you would also need to use the fromJson function with an array list.
In that case, your expression should instead look like this:
if: contains(fromJson('["6", "7", "8"]'), matrix.foo)
and the opposite like this:
if: ${{ !contains(fromJson('["6", "7", "8"]'), matrix.foo) }}
I tested it here if you want to have a look:
workflow file
workflow run

How to search MongoDB through the comand line with a wildcard

I'm trying to search MongoDB for some info using a wildcard. I'm trying to find all the "agents" near a given zip code using some type of wildcard. Here's what I have:
db.agents.find({company_address:"49085"},{_id:1,email:1,company_address:1}).pretty()
For the zip code, can I use something like: ...find({company_address:"490*"}...?
You could use a regex to find patterns in text/strings.
Asumming an address starts with a number:
...find({company_address:{ $regex: '^490' }})
This admits everything after 490 ...
Case you wanted to test a zip code, for example:
For example:
...find({company_address:{ $regex: '^490[0-9]+$' }})
That finds strings starting with 490 and continued by one or more digits.
...find({company_address:{ $regex: '^490[0-9]{1,5}$' }})
This other is for strings starting with 490 and continued by between 1 or 5 digits.
...find({company_address:{ $regex: '^490[0-9]{1,}$' }})
Goes for starting with 490 and having at least 1 more digit.
...find({company_address:{ $regex: '^490[0-9]{4}$' }})
Goes for starting with 490 and continued exactly by 4 digits.
The ^ pattern means start of string, and $ means end of string, that way it ensures it's always a number.
For more info on regex, look here: http://docs.mongodb.org/manual/reference/operator/query/regex/
And you can test some regex at regex101, see you should pick Java Script on the right as MongoDB works with Java Script
Try this "starts with" regex pattern:
db.agents.find(
{
company_address: /^490/
}, //SQL equivalent like '490%'
{_id:1, email: 1, company_address: 1}
)
given that the company_address has a string value.

Searching with multiple keys and "begins with"

What's the best way to perform the following type of search in a collection named "things":
mylist = ['lak', 'dodg', 'ang']
and the return could be:
["lake", "Lakers", "laky", "dodge", "Dodgers", "Angels", "angle"]
Would I need to perform a separate query for each?
To do this you want to use the mongodb command $in to search for all things that match with something in your array.
The command you would use would be:
db.things.find( {name: { $in: mylist }} )
But for this to work you want to be using regular expressions in your array, so you can either define them in the array, or if you want to maintain strings then the best thing to do it probably just create another array and loop through and create regex from the strings.
mylist = [/^lak/i, /^dodg/i, /^ang/i]
The ^ making it match only if it begins with the value, and the i at the end to make the search case insensitive.

Convert Ansible variable from Unicode to ASCII

I'm getting the output of a command on the remote system and storing it in a variable. It is then used to fill in a file template which gets placed on the system.
- name: Retrieve Initiator Name
command: /usr/sbin/iscsi-iname
register: iscsiname
- name: Setup InitiatorName File
template: src=initiatorname.iscsi.template dest=/etc/iscsi/initiatorname.iscsi
The initiatorname.iscsi.template file contains:
InitiatorName={{ iscsiname.stdout_lines }}
When I run it however, I get a file with the following:
InitiatorName=[u'iqn.2005-03.org.open-iscsi:2bb08ec8f94']
What I want:
InitiatorName=iqn.2005-03.org.open-iscsi:2bb08ec8f94
What am I doing wrong?
I realize I could write this to the file with an "echo "InitiatorName=$(/usr/sbin/iscsi-iname)" > /etc/iscsi/initiatorname.iscsi" but that seems like an un-Ansible way of doing it.
Thanks in advance.
FWIW, if you really do have an array:
[u'string1', u'string2', u'string3']
And you want your template/whatever result to be NOT:
ABC=[u'string1', u'string2', u'string3']
But you prefer:
ABC=["string1", "string2", "string3"]
Then, this will do the trick:
ABC=["{{ iscsiname.stdout_lines | list | join("\", \"") }}"]
(extra backslashes due to my code being in a string originally.)
Use a filter to avoid unicode strings:
InitiatorName = {{ iscsiname.stdout_lines | to_yaml }}
Ansible Playbook Filters
To avoid the 80 symbol limit of PyYAML, just use the to_json filter instead:
InitiatorName = {{ iscsiname.stdout_lines | to_yaml }}
In my case, I'd like to create a python array from a comma seperated list. So a,b,c should become ["a", "b", "c"]. But without the 'u' prefix because I need string comparisations (without special chars) from WebSpher. Since they seems not to have the same encoding, comparisation fails. For this reason, I can't simply use var.split(',').
Since the strings contains no special chars, I just use to_json in combination with map(trim). This fixes the problem that a, b would become "a", " b".
restartApps = {{ apps.split(',') | map('trim') | list | to_json }}
Since JSON also knows arrays, I get the same result than python would generate, but without the u prefix.

Regex to grab a number from within a string

I would like to grab a number from a string.
The string is: "responseID: 1 is the return value";
Sometimes the number could be 1, or 3, 300, 3000...
I am trying to do this in objective C for the iphone.
I've tried NSPredicate and NSRegularExpression, but I can not seem to get the right regex to start with.
I tried "*[0-9]+*";
Does this return the "1" , "300" or whatever number to me when I call the regex?
Thank you!
\d+ should match 1 or more digits.
Sorry, I was able to search again and found exactly what I was looking for: see this:
Objective C: How to grab a number after a sub string from string