I have a specific block of code (the forEach loop) that I use all over the place in my code base, whenever I have an "item".
How can I refactor this into a module or a global function that I can call whenever I need to.
Here's the context:
Item.findById(itemid).populate('wants').exec(function(err, item){
item.wants.forEach(function(person){
person.avatar = gravatar.url(person.email, {size: 80, default: 'mm' });
});
//do some more stuff with item
});
I want to re-use this forEach loop in other places where I have an "item" object. I'm thinking I pass in the item object and return it again.
item.wants.forEach(function(person){
person.avatar = gravatar.url(person.email, {size: 80, default: 'mm' });
});
Something like:
var myLib = require('./lib/myLib');
...
item = myLib.doStuff( item );
Your ./lib/myLib.js file would look like this:
module.exports.doStuff = function(item) {
item.wants.forEach(function(person){
person.avatar = gravatar.url(person.email, {size: 80, default: 'mm' });
});
return item;
}
You can then require and use like you outlined already:
var myLib = require('./lib/myLib');
item = myLib.doStuff( item );
Related
I'm trying to write an eslint rule that enforces making sure the name property is defined on any classes that extend from other Error/Exception named classes (and fixes them).
As far as I can tell, it works in the astexplorer.net individually, but when I'm running it alongside other rules, it ends up getting ran multiple times, so the name property ends up being repeated multiple times in the resulting "fixed" file.
Is there anything in particular I can do to prevent it being run multiple times? I'm assuming what's happening is that it's inserting my name = 'ClassName';, then prettier is needing to reformat the code, which it does, but then maybe it's re-running my rule? I'm not sure.
Rule/fix code shown below. I've tried things like using *fix and yield, but that doesn't seem to help either (see commented code below, based on information in the eslint documentation)
module.exports = {
meta: {
hasSuggestions: true,
type: 'suggestion',
docs: {},
fixable: 'code',
schema: [], // no options,
},
create: function (context) {
return {
ClassDeclaration: function (node) {
const regex = /.*(Error|Exception)$/;
// If the parent/superClass is has "Error" or "Exception" in the name
if (node.superClass && regex.test(node.superClass.name)) {
let name = null;
const className = node.id.name;
// Test class object name
if (!regex.test(className)) {
context.report({
node: node,
message: 'Error extensions must end with "Error" or "Exception".',
});
}
// Find name ClassProperty
node.body.body.some(function (a) {
if (a.type === 'ClassProperty' && a.key.name === 'name') {
name = a.value.value;
return true;
}
});
// Name property is required
if (!name) {
context.report({
node: node,
message: 'Error extensions should have a descriptive name',
fix(fixer) {
return fixer.replaceTextRange(
[node.body.range[0]+1, node.body.range[0]+1],
`name = '${className}';`
);
},
// *fix(fixer) {
// name = className;
// yield fixer.replaceTextRange(
// [node.body.range[0]+1, node.body.range[0]+1],
// `name = '${className}';`
// );
//
// // extend range of the fix to the range of `node.parent`
// yield fixer.insertTextBefore(node.body, '');
// yield fixer.insertTextAfter(node.body, '');
// },
});
}
}
},
};
},
};
Turns out I had the AST Explorer set to the wrong parser, so it was showing me the wrong string name for the ClassProperty node. I should have been using PropertyDefinition instead.
I got list of videos from API, it has list of urls fo thumbnail and i would like to combine thumbnails of each video to gif. When i loop through videos and don't generate gifs it goes through 5 times as expected, but if i include function that should generate gifs it only goes through once, without any errors. I have no idea what is happening
I'm using node.js, discord.js, get pixels and gif-encoder modules to generate thumbnails.
for(i=0;i<5;i++){
generateThumbnail(data[i].video.video_id,data[i].video.thumbs,function(){
var tags = '';
for(t=0;t<data[i].video.tags.length;t++){
tags = tags + data[i].video.tags[t].tag_name+', ';
}
fields = [
{name:data[i].video.title,
value:value},
{name:'Tags',
value:tags}
]
msg.channel.send({embed: {
color: 3447003,
thumbnail: {
"url": ""
},
fields: fields,
}});
});
}
function generateThumbnail(id,images,fn){
var pics = [];
console.log(id)
var file = require('fs').createWriteStream(id+'.gif');
var gif = new GifEncoder(images[0].width, images[0].height);
gif.pipe(file);
gif.setQuality(20);
gif.setDelay(1000);
gif.setRepeat(0)
gif.writeHeader();
for(i=0;i<images.length;i++){
pics.push(images[i].src)
}
console.log(pics)
addToGif(pics,gif);
fn()
}
var addToGif = function(images,gif, counter = 0) {
getPixels(images[counter], function(err, pixels) {
gif.addFrame(pixels.data);
gif.read();
if (counter === images.length - 1) {
gif.finish();
} else {
addToGif(images,gif, ++counter);
}
})
}
if i dont use GenerateThumbnail function it goes through 5 times as expected and everything works fine, but if i use it it goes through only once, and generated only 1 gif
Use var to declare for vars. Ie for(var i=0....
If you declare vars without var keyword, they are in the global scope. ..... and you are using another i var inside the function but now it is the same var from the outer for loop.
I'd like to store initial device title, which is accessible via GUI in browser as page title, and reuse it later in the code. E.g. I want to change name of device if it is set to some particular name only, and to store an old device name in browser console log. Code might look like this:
var some_name = 'Some Name';
var title = browser.getTitle();
console.log(title);
if (title.equals('Some Name')){
some_name = 'Some Other Name';
}
setDeviceName(some_name);
unfortunately, to that protractor responds with
TypeError: title.equals is not a function
How is it possible to extract browser title as string?
Thanks.
UPDATE:
Thanks everyone, the solution due to igniteram1 is
var some_name = 'Some Name';
some_name = browser.getTitle().then(function(webpagetitle){
if (webpagetitle === 'Some Name'){
return 'Some Other Name';
}else{
return 'Some Name'
}
});
expect(some_name).toEqual('Some Other Name');
In addition to #Sudharshan Selvraj's answer,its good practice to use strict equality === instead of == and to return the value.
var some_name = 'Some Name';
some_name = browser.getTitle().then(function(webpagetitle){
if (webpagetitle === 'Some Name'){
return 'Some Other Name';
}else{
return 'Some Name';
}
});
expect(some_name).toEqual('Some Other Name');
If you aware of the new asyc/await you may use:
expect(await browser.getTitle()).toEqual("myCustomTitle");
In protractor whatever information you need from browser will be a promise and not a string. in order to fetch the value you need to resolve the promise. look at the below example.
var title = browser.getTitle();
title.then(function(webpagetitle){
if (webpagetitle == 'Some Name'){
some_name = 'Some Other Name';
}
})
.equals() is not a function in JavaScript, but it's a valid function in Java. In Java Script, we have '==', '===' as an alternative for performing equal operations.
var x=10
== compare only values of operands, not type of operands
example:
x==10 => true
x=="10" => true
=== compare both values and type of operands
example:
x===10 => true
x==="10" => false(type of x in int and "10" is string)
var someName;
browser.getTitle().then(function (webPageTitle) {
if (webPageTitle == 'Some Name'){
someName= 'Some Other Name';
}
});
I've got a simple function in protractor to get the title when in the promise it shows the title properly, but when I do console.log it gives :
Title Got is ManagedPromise::123 {[[PromiseStatus]]: "pending"}
describe('Protractor Demo App', function() {
it('should have a title', function(one) {
browser.waitForAngularEnabled(false);
browser.get('https://tasyah.com');
titleGot = browser.getTitle().then(function(promisedResult){
console.log("Title is : " + promisedResult);
return promisedResult;
})
console.log("Title Got is " + titleGot);
expect(titleGot).toEqual('Tasyah: Online Shopping For Fashion And Costume Jewellery');
console.log("Title Got is " + titleGot);
});
});
Any explanation on this is appreciated...
Thanks
Naveen
Page Object .po file
getTableRows() {
return element.all(by.css('[data-qa="qa-table"] tr'));
}
.e2e.spec file
<PROMISE-APPROCH>
obj.getTableRows().then((list)=> {
list.count().then((totalRows)=>{
expect(totalRows).toBe(10); // example
})
});
<ASYNC-AWAIT-APPROCH>
const rowsCount = await obj.getTableRows().count();
expect(rowsCount).toBe(10); // example
I know that it is possible to pass a model to a view in express by doing something like this:
exports.locations = function(req, res){
Location.find(function(err, results) {
res.render('locations', { title: 'Locations', locations: results });
});
};
But is it possible to pass a model to my layout?
Assuming you have all (relevant) routes inside a single .js file, you could add a function like this:
function applyGlobals(pageModel) {
pageModel.myGlobalThing = "I'm always available";
pageModel.anotherGlobalThing = 8675309;
return(pageModel);
}
exports.locations = function(req, res){
Location.find(function(err, results) {
res.render('locations', applyGlobals({ title: 'Locations', locations: results }));
});
};
You could also create a more generalizable solution:
function Globalizer(baseContent) {
var theFunc = function(specificContent) {
var keys = Object.keys(baseContent);
for (var i = 0; i < keys.length; i++)
{
// the lets the page content override global content by not
// overwriting it if it exists;
if(!specificContent.hasOwnProperty(keys[i])){
specificContent[keys[i]] = baseContent[keys[i]];
}
}
return specificContent;
};
return theFunc;
};
// And use it like so.
var applyGlobals = new Globalizer({global1: 12, global2: 'otherthing'});
var pageVars = applyGlobals({item1: 'fifteen', 'item2': 15, global2: 'override'});
console.log(require('util').inspect(pageVars));
Which would emit:
{ item1: 'fifteen',
item2: 15,
global2: 'override',
global1: 12 }
Similarly, you could use one of the various mixin, extend assign or similar functions of various libraries like lodash, underscore, etc. See the doc for lodash.assign() which illustrates accomplishing the same sort of thing.
UPDATE One more way of doing it.
You might want to check out Express' app.locals documentation as well - it might work well for you.
If I want a module that is instantiable, let say, a module that handles storing preferences in a subcookies, and i want the main cookie to be configurable, but i don't want it to be a widget... what patterns should i use with YUI?
the end code should be something:
Y.use('my-pref-manager', function(Y){
var A = Y.my-pref-manager.prefStore('A"),
B = Y.my-pref-manager.prefStore('B");
// A and B are now loaded with the contents of cookies A and B, if they exist
A.set('xy', 123 );
});
So far i either found patterns that create widgets within my-module or i have to use methods directly in my-method which will be globals and lack initializers, etc.
There is a bunch of ways of doing this. You could do it using Y.Base.create, like below. The code might not be production ready, or even working properly, but hopefully it answers how you can create a module without it being a Widget.
The code below creates a module that extends Y.Base. This let us use Attributes and other cool things. Check the doc for Y.Base.
YUI.add('my-pref-manager', function (Y) {
var PrefManager = Y.Base.create('myPrefManager', Y.Base, [], {
initializer: function () {
this.after('prefsChange', this.changePref);
},
changePref: function (e) {
Y.Cookie.setSub(this.get('prefStore'), e.subAttrName, this.get(e.subAttrName));
},
setPref: function (name, val) {
this.set('prefs.'+name, val);
},
getPref: function (name) {
return this.get('prefs.'+name);
}
}, {
ATTRS: {
prefStore: {
value: null,
setter: function (val) {
return Y.Cookie.set(val, val);
}
},
prefs: {
value: {}
}
}
});
Y.namespace('My').PrefManager = PrefManager;
}, '0.0.1', {
requires: ['base', 'cookie']
});
YUI().use('my-pref-manager', function (Y) {
var A = new Y.My.PrefManager({prefStore: 'userPrefs'}),
B = new Y.My.PrefManager({prefStore: 'displayPrefs'});
A.setPref('x', 3);
A.setPref('y', 54);
B.setPref('tty', 7);
console.log(A.getPref('x')); // 3
});
Try it out: http://jsfiddle.net/B62nu/