iterating over method names in coffeescript [duplicate] - coffeescript

This question already has answers here:
How to iterate over the keys and values in an object in CoffeeScript?
(4 answers)
Closed 8 years ago.
In JS, I can do something like this:
for(i in MyClass.prototype) {
console.log(i);
}
And it will show me the method names. That's fine.
Now, if I do this with coffeescript:
for i in MyClass.prototype
console.log i
It will be compiled to:
var i, _i, _len, _ref;
_ref = MyClass.prototype;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
console.log(i);
}
But prototype doesn't have a length property, so, it breaks.
How can I make it with coffeescript?

The 'secret' is to use the of command when using objects:
console.log i for i of MyClass.prototype

Related

Stuck on [object Promise] [duplicate]

This question already has answers here:
How can I access the value of a promise?
(14 answers)
Async function returning promise, instead of value
(3 answers)
How to return many Promises and wait for them all before doing other stuff
(6 answers)
Closed 3 months ago.
I have already resolve promises in the cardcounter function but still gives me the same [object Promise] error. I do not know which promise i should be resolving and im stuck please help me
function paginator (mainembed, max, mentioned, author) {
async function cardcounter (cardcode, user) {
const results = await printcounter.findOne({code: cardcode, printowner: user})
let finalresult = results.amounts
if (!results) {
finalresult = 0
} else {
finalresult = results.amounts
}
console.log(`Number should be: ${finalresult}`)
return Promise.resolve(finalresult).then(function(val) {
console.log(val);
});
}
var last = max
const embeds = []
for (var i = 0 ; i < mainembed.length ; i+= max) {
const sliced = mainembed.slice(i, last)
console.log(sliced)
var index = i
last += max
const cardline = sliced.map(card => ` **${card.role}** \`${card.code}\` ${card.rarity} | **${card.group}** ${card.name} (${card.theme}) - ${cardcounter(card.code, mentioned)} `).join("\n")
console.log(cardline) // will define how the line of each card will look like in the embed.
const embed = new EmbedBuilder()
.setAuthor({name: `${author.tag} - Card Book`})
.setDescription(`**Featured Member**
> <#${mentioned}>
${cardline}`)
embeds.push(embed)
}
return embeds;
} //this function will generate the enough embed needed for the cards.
I want to get a value from a document and put it in slice.map section using the cardcounter function i made. I already resolve promises in that function

How to replace deprecated List [duplicate]

This question already has answers here:
The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'
(4 answers)
Flutter: List is deprecated? [duplicate]
(4 answers)
Closed 1 year ago.
List has been deprecated. How do I re-write the following code?
RosterToView.fromJson(Map<String, dynamic> json) {
if (json['value'] != null) {
rvRows = new List<RVRows>();
json['value'].forEach((v) {
rvRows.add(new RVRows.fromJson(v));
});
}
}
According to the official documentation:
#Deprecated("Use a list literal, [], or the List.filled constructor instead")
NOTICE: This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use [] for a growable list or List.empty for a fixed length list (or where growability is determined at run-time).
You can do this instead:
RosterToView.fromJson(Map<String, dynamic> json) {
if (json['value'] != null) {
rvRows = <RVRows>[];
json['value'].forEach((v) {
rvRows.add(new RVRows.fromJson(v));
});
}
}
Another option is:
List<RVRows> rvRows = [];
Instead of:
rvRows = new List();
Write:
rvRows = [];
The error message tells you what to do. When I run dart analyze, I get:
info • 'List' is deprecated and shouldn't be used. Use a list literal, [],
or the List.filled constructor instead at ... • (deprecated_member_use)
Try replacing the use of the deprecated member with the replacement.
error • The default 'List' constructor isn't available when null safety is
enabled at ... • (default_list_constructor)
Try using a list literal, 'List.filled' or 'List.generate'.
The documentation for the zero-argument List constructor also states:
This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use [] for a growable list or List.empty for a fixed length list (or where growability is determined at run-time).
Examples:
var emptyList = [];
var filledList = List<int>.filled(3, 0); // 3 elements all initialized to 0.
filledList[0] = 0;
filledList[1] = 1;
filledList[2] = 2;
var filledListWithNulls = List<int?>.filled(3, null);
var generatedList = List<int>.generate(3, (index) => index);
You also could use collection-for for both cases:
var filledList = [for (var i = 0; i < 3; i += 1) 0];
var filledListWithNulls = <int?>[for (var i = 0; i < 3; i += 1) null];
var generatedList = [for (var i = 0; i < 3; i += 1) i];

How to create a class in a js file in a meteor-based website? [duplicate]

This question already has answers here:
How can I access constants in the lib/constants.js file in Meteor?
(2 answers)
Closed 7 years ago.
Why does this code does not work?
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo = getAppleInfo;
}
If I try to call Apple, I get the error message
Uncaught ReferenceError: Apple is not defined(…)
(anonymous function) # VM1844:2
InjectedScript._evaluateOn # VM1757:875
InjectedScript._evaluateAndWrap # VM1757:808
InjectedScript.evaluate # VM1757:664
If you want something global in Meteor, don't prefix with var.
So, this should work!
Apple = function (type) {
this.type = type;
this.color = "red";
this.getInfo = getAppleInfo;
}

CoffeeScript alternative syntax for a while loop

I have this function written in CoffeeScript that I feel should be better written:
addCSS = (files) ->
i = files.length - 1
while i >= 0
$("<link/>",
rel: "stylesheet"
href: files[i]
).appendTo $("head")
i--
return
The files arg is just an array of file paths.
Is there a more succinct/cleaner way to write this in CoffeeScript?
A simple
addCSS = (files) ->
for file in files
$("<link/>",
rel: "stylesheet"
href: file
).appendTo $("head")
(no need for return either, although it does prevent coffee from returning a bunch of stuff)
In case you are wondering, because of the horrible behavior of JS with for ... in loop, it is compiled as
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
// ...
}
Edit
I realize just now that you are reversing the array, so it would be
for file in files by -1
(supported since 1.5.0)
compiles as for (_i = files.length - 1; _i >= 0; _i += -1) {

is there any way to use .indexOf to search a javascript array in mirth?

I am trying to find a string in a javascript array in the transformer of a mirth channel. Mirth throws an error when I try to use indexOf function. My understanding is that indexOf is something that browsers add in, rather than a native part of the javascript language itself. ( How do I check if an array includes an object in JavaScript? )
So is array.indexOf just not supported in Mirth? Is there any way to use .indexOf in Mirth? Maybe an alternate syntax? Or do I need to just loop thru the array to search?
This is how I search arrays in a Mirth js transformer:
var Yak = [];
Yak.push('test');
if(Yak.indexOf('test') != -1)
{
// do something
}
Does this give you error?
Mirth uses the Rhino engine for Javascript, and on some earlier versions of the JVM, indexOf appeared to not be supported on arrays. Since upgrading our JVM to 1.6.23 (or higher), indexOf has started working. However, we still have legacy code that, when searching arrays of strings, I just use a loop each time:
var compareString = "blah";
var index = -1;
for (var i = 0; i < myArray.length; ++i)
{
if (myArray[i] == compareString)
{
index = i;
break;
}
}
If you need to do this frequently, you should be able to use a code template to manually add the indexOf function to Array.
Set the code template to global access, and try out something like this (untested code):
Array.prototype.indexOf = function(var compareObject)
{
for (var i = 0; i < myArray.length; ++i)
{
// I don't think this is actually the right way to compare
if (myArray[i] == compareObject)
{
return i;
}
}
return -1;
}
var arr = ['john',1,'Peter'];
if(arr.indexOf('john') > -1)
{
//match. what to do?
console.log("found");
}
else
{
console.log("not found");//not found .. do something
}
var i = ['a', 'b', 'c']
if(i.indexOf('a') > -1)
{
///do this, if it finds something in the array that matches what inside the indexOf()
}
else
{
//do something else if it theres no match in array
}