function inside functions in coffeescript - coffeescript

How do I write this in coffee script?
function setUpDialogForms() {
setUpListForm();
setUpUpdateForm();
}
I have tried
setUpDialogForms -> setUpListForm(); setUpUpdateForm()

Provided the setUpListForm and setUpUpdateForm functions are defined somewhere, you can use:
setUpDialogForms = ->
setUpListForm()
setUpUpdateForm()
setUpDialogForms()

Found it out.
setUpDialogForms = () ->
setUpListForm()
setUpUpdateForm()
return
Which returns
var setUpDialogForms;
setUpDialogForms = function() {
setUpListForm();
setUpUpdateForm();
};

Related

Tell babel to transpile down to NodeJS

I use typescript and I'm building a lambda layer but lambda layer seem to be strict with module.exports.myHelperFunction rather than exports.myHelperFunction, the later doesn't work when you try to import it, it will fail.
So What I want is if I have the following code:
export function myHelperUtility () {
let a = {};
return { ...a };
}
it should transpile to:
"use strict";
function myHelperUtility () {
let a = {};
return { ...a };
}
module.exports.myHelperUtility = myHelperUtility;
Rather than:
"use strict";
exports.__esModule = true;
exports.test = test;
function test() {
let a = {};
return { ...a };
}
Found a way to make it work, instead of using #babel/preset-env, I completely removed it and used #babel/plugin-transform-modules-commonjs with the following options:
{
strict: true,
loose: true,
importInterop: 'node'
}
which did not produce what I wanted to achieve BUT since it removes the __esmodule and _interoprequiredefault, it will work!

Cannot call method 'id' of undefined

My world.js looks like this:
var protractor = require('protractor');
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().usingServer('xxxxx').
withCapabilities(webdriver.Capabilities.firefox()).build();
driver.manage().timeouts().setScriptTimeout(100000);
module.exports.World = function World(callback) {
this.browser = protractor.wrapDriver(driver);
this.by = protractor.by;
callback();
};
then in steps.js:
{
element(by.id('username')).sendKeys("admin");
}
When I ran it using cucumber.js, the error is:
TypeError: Cannot call method 'id' of undefined
but if I remove world.js and run it using protractor, it works.
How can I fix this?
It looks like you're not exporting by globally. I'm not sure why you're able to use the element function at all - but in any case, you should probably be doing something like:
module.exports.World = function World(callback) {
global.browser = protractor.wrapDriver(driver);
global.by = protractor.by;
};

How to globally add a custom locator to Protractor?

I wrote a custom locator for Protractor that finds anchor elements by their ui-sref value. In my specs I just used by.addLocator to add the custom locator, but I figured this might be a cool thing to publish and have other people use it.
The goal is to add this custom locator to the global Protractor object so it can be used in any of your specs.
My initial approach was to add this functionality in the onPrepare block of the Protractor config. Something like the pseudocode below:
onPrepare: function () {
require('ui-sref-locator')(protractor); // The protractor object is available here.
}
That require statement would just execute this function:
function (ptorInstance) {
ptorInstance.by.addLocator('uiSref', function (toState, opt_parentElement) {
var using = opt_parentElement || document;
var possibleAnchors = using.querySelectorAll('a[ui-sref="' + toState +'"]');
var result = undefined;
if (possibleAnchors.length === 0) {
result = null;
} else if (possibleAnchors.length === 1) {
result = possibleAnchors[0];
} else {
result = possibleAnchors;
}
return result;
});
};
The problem is that by is not defined on the protractor object available in the onPrepare block. This means that I cannot use the .addLocator method.
Try the following:
function () {
by.addLocator('uiSref', function (toState, opt_parentElement) {
...
By should be in the global scope.
The protractor object passed to the onPrepare block has a By property. That By property has an inherited enumerable property named addLocator. My understanding of JavaScript is pretty shallow so it really threw me off that when I console.log'ed the protractor.By it returned {}, but if I did for (var propName in protractor.By) it would show me all the "hidden" properties. I'm still struggling to understand that bit.
Working code:
onPrepare: function () {
require('ui-sref-locator')(protractor); // The protractor object is available here.
}
The require would execute the function below:
function (ptor) {
ptor.By.addLocator('linkUiSref', function (toState, opt_parentElement) {
var using = opt_parentElement || document;
var possibleAnchors = using.querySelectorAll('a[ui-sref="' + toState +'"]');
var result = undefined;
if (possibleAnchors.length === 0) {
result = null;
} else if (possibleAnchors.length === 1) {
result = possibleAnchors[0];
} else {
result = possibleAnchors;
}
return result;
});
};

Coffeescript best way to var

I was poking around trying to figure a good way to do var, and here's one.. is this intentionally in cs, because it's not documented?
me=;
fetchUser()
.then((_me) =>
me = _me
fetchFriend()
)
.then((_you) =>
me.friend(_you)
)
.then(done)
.otherwise(=>
console.log ':('
)
compiles correctly to
var me;
me = fetchUser().then((function(_this) {
return function(_me) {
me = _me;
return fetchFriend();
};
})(this)).then((function(_this) {
return function(_you) {
return me.friend(_you);
};
})(this)).then(done).otherwise((function(_this) {
return function() {
return console.log(':(');
};
})(this));
Edit: it's not compiling correctly.
I wouldn't expect me = fetchUser() either, but I didn't see that before
https://github.com/jashkenas/coffee-script/issues/3098
I think it's just a quirk of the parser. The normal way variables are declared in order to establish scoping is just by defining them with some default value (e.g. null).

Meteor 0.6.4.1 changes confuses coffeescript?

After upgrading from Meteor 0.5.4 to Meteor 0.6.4.1 I modified my coffeescript code accordingly to reflect changes of the variable scope. For some reason I think the changes confused the coffeescript to javascript interpretation?
Current code:
#liveObjects = {}
test = () ->
if liveObjects.intervalID?
donothing;
liveObjects = {} --Maybe this is what caused the confusion? Mistaken as a local variable declaration?
From the Chrome tool I noticed that the javascript code as
(function() { var test;
this.liveObjects = {};
test = function() {
var liveObjects;
if (liveObjects.intervalID != null) { --ReferenceError: liveObjects is not defined
donothing;
}
liveOjects = {};
You have to set it using this/# again.
#liveObjects = {}
test = () ->
if liveObjects.intervalID?
donothing;
#liveObjects = {}