How can we to extend element methods in JSDOM to all opened pages?
// load the module and few pages
const JSDOM = require("jsdom").JSDOM;
var dom1 = new JSDOM("<p>1st</p><p>2nd</p>");
var dom2 = new JSDOM("<p>1st</p><p>2nd</p>");
...
// add methods such as: map, filter, searchText...
var window = dom1.window;
window.NodeList.prototype.map =
function (cb) { return Array.from(this).map(cb) };
...
// Execution:
dom1.window.document.querySelectorAll('p').map(p=> p.textContent);
// returns: [ '1st', '2nd' ]
dom2.window.document.querySelectorAll('p').map(p=> p.textContent);
// throws an error
Is it possible to extend NodeList ones, without loading it for every new document?
Something more like:
JSDOM.installedInterfaces.NodeList.prototype.map = ...
Related
I would like to use updated (and only then) globals among all node modules. How to do that? Questions are in code.
app.js
var data = 'data';
var global = require('glob.js')(data);
// here we are require your globals variables and we corectly 'set them'
console.log(globals.glob1);
// we can use them here
glob.js
module.exports = function(data)
{
var globs = {
glob1 : data.toLowerCase(),
glob2 : data.toUpperCase()
}
return globs;
}
mod.js
var global = require('glob.js'); // I require globals but they are not set...
function funct(someOtherData, someMoreData)
{
var test = global.glob1;
console.log(test);
// why I can't use globals here ? How can I use corectly set globals (globals need to be updated first - app.js, then ALL other modules should be able to use correctly set globals)?
}
module.export = funct;
For the answer scroll down to the TLDR section below but do read on to understand why.
Part1 - the difference between a function and a function call
Your first mistake is that you are exporting a function, not an object:
module.exports = function(data) // <---- this is a function
{
var globs = {
glob1 : data.toLowerCase(),
glob2 : data.toUpperCase()
}
return globs;
}
and in app.js you do this:
console.log(globs.glob1); <--- globs is a function, not an object
when you should be doing this:
console.log(globs().glob1);
Why is this? OK, lets forget for a moment your module. Consider the following code:
var a = function(){ return 2 };
console.log(a); // do you expect this to print a function or 2?
console.log(a()); // what do you expect this to print?
This is a very basic rule about functions in all programming languages, not just javascript: to get the return value you need to call the function. So in your code:
function myExportedFunction (data) {
// some logic here...
return globs;
}
console.log(myExportedFunction); // prints a function
console.log(myExportedFunction()); // prints the globs object
console.log(myExportedFunction().glob1); // prints value of glob1
So it's simple really. There is no magic syntax going on. You've just forgotten to return the glob object and are using the function pointer instead. Obviously the function has no glob1 property so it's correct for it to be undefined.
Part2 - function local variables
OK. So let's say you made the changes I recommended above. There's an obvious problem with the way the function was written. What happens when you do this:
var glob = require('glob.js')();
console.log(glob.glob1); // <--- prints "undefined"
So the first problem is you're not checking if you're passing data or nothing. So every time you call the function you will overwrite the stored value.
There's another problem, you are always returning a different object every time you call the function. Let's look at how local variables work when returned:
function a () {
var data = {}
return data;
}
var x = a();
var y = a();
x.testing = 1;
y.testing = 2;
console.log(x.testing); // prints 1
console.log(y.testing); // prints 2
So, every time you call a function that creates a local variable you are returning a different object. Actually what's doing this is not really the variable but the object literal syntax:
var a = {};
// is basically the same as
var a = new Object();
If we change the above example to:
function a () {
return {};
}
it would still behave the same.
TLDR
So, how do we fix it? Simple, create the object outside of the function and check if we pass data to initialize:
var globs = {
glob1 : "",
glob2 : ""
}
module.exports = function(data)
{
globs.glob1 = data.toLowerCase();
globs.glob2 = data.toUpperCase();
return globs;
}
Now everything should work:
In app.js
var global = require('glob.js')(data);
In mod.js
var global = require('glob.js')();
Epologue - modules are singletons
It may or may not be obvious to you why the above should work. In case you already know why I'm writing this as reference to future readers.
In node.js modules are implemented as proper singletons. Therefore in node if you want a singleton all you need to do is write a module, you don't need to implement any special code for it.
What this means is that all module globals (module scoped variables) are shared amongst all requires. Here's a very simple module to share one variable amongst all modules:
shared.js
var x = "";
module.exports = {
set: function (val) {x=val},
get: function () {return x}
}
a.js
var shared = require('./shared');
shared.set("hello world");
b.js
var shared = require('./shared');
console.log(shared.get()); // prints "hello world"
We're using this feature to declare a shared glob variable in the code above.
You can use the global. variable identifier to set global variables in NodeJS, instead of var, example:
app.js
var data = 'data';
var glob = require('./glob.js');
glob(data);
// here we are require your globals variables and we corectly 'set them'
console.log(global.gl.glob1);
var mod = require('./mod.js');
mod();
// we can use them here
glob.js
module.exports = function(data)
{
console.log("setting globals");
global.gl = {
glob1 : '1' + data,
glob2 : '2' + data
}
// return global.gl; // can be removed
}
mod.js
function funct(someOtherData, someMoreData)
{
var test = global.gl.glob1;
console.log(test);
test = global.gl.glob2;
console.log(test);
// why I can't use globals here ? How can I use corectly set globals (globals need to be updated first - app.js, then ALL other modules should be able to use correctly set globals)?
}
module.exports = funct;
As you can see in glob.js, i switched to var globs = to global.gl = and then in mod.js used it as global.gl.
Running app.js outputs:
setting globals
1data // From app.js
1data // From mod.js imported in app.js
2data // From mod.js imported in app.js
There are 2 options:
Use nodejs global variable (not recommended)
Create shared module
You chose 2nd option, but did it a bit wrong way by exporting a function. When you import the package and call the function it always creates new globs object and fulfill it with your data. Instead you can export an object. Simple example
glob.js
Global object is defined here
module.exports = {
glob1: '1',
glob2: '2'
};
mod.js
You can change global object here, like
var globs = require('./glob');
module.exports.updateGlob1 = function(data) {
globs.glob1 = data;
};
app.js
Here if you access global variable you can see it updated
var globs = require('./glob');
var mod = require('./mod');
mod.updateGlob1('1 plus 2');
console.log(globs.glob1); // Output: '1 plus 2'
There can be more complex examples, as for module design pattern often IIFE is used.
UPDATE
Another example using IIFE.
glob.js
module.exports = (function() {
var glob1 = 'initial value';
return {
// Getter method
getGlob1() {
return glob1;
},
// Setter method
setGlob1(value) {
glob1 = value;
}
}
})();
mod.js
var shared = require('./shared');
module.exports.testFn = function() {
// Access global variable with getter method
console.log('In mod.js', shared.getGlob1());
};
app.js
var shared = require('./shared');
var mod = require('./mod');
// Print initial value
console.log('Initial', shared.getGlob1());
// Set new value to global variable
shared.setGlob1('new value');
// Print updated value
console.log('In app.js', shared.getGlob1());
// Use global variable in mod.js file
mod.testFn();
Ok so I have a class that contains
Object JS
var GameServer = require("./GameServer");
var gameServer = new GameServer();
GameServer() contains
GameServer JS
function GameServer() {
// Startup
this.run = true;
this.lastNodeId = 1;
this.lastPlayerId = 1;
this.clients = [];
this.largestClient; // Required for spectators
this.nodes = [];
this.nodesVirus = []; // Virus nodes
this.nodesEjected = []; // Ejected mass nodes
this.nodesPlayer = []; // Nodes controlled by players
}
Now, what im trying to acheive is getting gameServer from ObjectClass
In my class i've tried
new JS
var ObjectClass = require("./ObjectClass");
var gameServer = ObjectClass.gameServer;
But from this way, I won't be able to grab the class GameServer() properties. I'm new to node and im sorry I have to ask this question. I'm currently stuck right now
When I try to grab clients from GameServer
var ObjectClass = require("./ObjectClass");
var gameServer = ObjectClass.gameServer;
gameServer.clients.length;
I get error, clients is undefined. Any way around this?.
I cannot modify GameServer nor Object js.. Basicly im making a script attacthed to a script for extra functionalities.
You are missing the exports of your files so when doing require(file) you're getting and empty object {}..
For gameServer you should be doing something like:
'use strict';
function GameServer() {
// Startup
this.run = true;
this.lastNodeId = 1;
this.lastPlayerId = 1;
this.clients = [];
this.largestClient; // Required for spectators
this.nodes = [];
this.nodesVirus = []; // Virus nodes
this.nodesEjected = []; // Ejected mass nodes
this.nodesPlayer = []; // Nodes controlled by players
}
module.exports = exports = GameServer;
ObjectClass
'use strict';
var GameServer = require("./GameServer");
var gameServer = new GameServer();
exports.gameServer = gameServer;
You need to understand that require cache the value returned by the file, so you would be using a singleton of gameServer.
I am creating a factory method where i am returning an instance of a node module
var dal1 = require('../dal/dal1');
var dal2 = require('../dal/dal2');
exports.createDAL(role){
switch(role){
case "XYZ": return dal1;
case "ABC": return dal2
}
};
Using Mocha as the TDD frame work, When I get the object returned,I get the value of returnObject.constructor.name as object instead of dal1 or dal2. Any pointers?
So I went ahead with using the node-module 'util'. I was able to get the expected value instead of just 'Object'
I created a DAL object
//dal.js
function dal(){
}
dal.prototype.BLAH = function(){
//logic here
}
module.exports = dal
//dal1.js
var inherits = require('util').inherits;
function dal1(){
dal1.call(this);
}
inherits(dal1,dal);
module.exports = dal1;
//similarly dal2.js
//finally in factory
var dal1 = require('./dal1');
var dal2 = require('./dal2');
switch(role){
case 'XYZ': return new dal1();
case 'ABC' : return new dal2();
}
I want to import a dependency of a dependency. For example, I want to import jade-load directly into my app:
my-app
┗━jade
┗━jade-load
I could do require('jade/node_modules/jade-load'), but this won't work if the node_modules tree has been flattened or deduped.
I thought of using require.resolve() to find out where jade-load really is, but there doesn't seem to be a way to tell it the starting point for the resolution. I need to be able to say "require jade-load from wherever jade is".
NB. I do not want to install jade-load as a direct dependency of my app; the point is to import the same instance that jade uses, so I can monkeypatch it.
I guess you may want to use proxyquire for managing dependencies of required modules. You can set proxyquire to globally override methods of the submodule when it will be loaded.
main.js
var proxyquire = require('proxyquire');
// use default childModule properties and methods unless they are redefined here
var childModuleStub = {
/* redefine here some necessary methods */
'#global': true
};
// parent module itself does't require childModule
var parentModuleStubs = {
'./childModule': childModuleStub
};
var parentModule = proxyquire('./parentModule', parentModuleStubs);
var result;
result = parentModule.exec();
console.log(result);
childModuleStub.data.sentence = "Overridden property.";
result = parentModule.exec();
console.log(result);
childModuleStub._exec = function () {
return "Overridden function.";
};
result = parentModule.exec();
console.log(result);
parentModule.js
var intermediateLibrary = require('./intermediateLibrary');
module.exports = {
exec: function() {
return intermediateLibrary.exec();
}
};
intermediateLibrary.js
var childModule = require('./childModule');
module.exports = {
exec: function() {
return childModule._exec();
}
};
childModule.js
var lib = {};
lib.data = {};
lib.data.sentence = "Hello, World!";
lib._exec = function () {
return lib.data.sentence;
};
module.exports = lib;
Results:
Hello, World!
Overridden property.
Overridden function.
I have decided to use CoffeeScript and i have been trying to convert Node.js module of mine to CoffeeScript. So, here is the code in JS:
function Domain(obj){
var self = this;
for (var key in obj){
this[key] = obj[key]; //merge values
}
}
Domain.prototype.save = function(fn){
}
And my attempt to have the same in the CoffeeScript as following:
class Domain
consructor: (obj) ->
for own key, value of obj
#key = value
save: (fn) ->
module.exports = Domain
The following test fails:
require('coffee-script');
var should = require('should')
, Domain = require('../index');
should.exist(Domain);
var domain = new Domain({'attOne':1, 'attTwo':2});
domain.should.have.property('save');
domain.should.have.property('attOne');
domain.should.have.property('attTwo');
domain.save.should.be.an.instanceof(Function);
console.log('All tests passed');
The property 'attrOne' and 'attrTwo' is not binding to the Domain class. I have compiled the coffee code by 'coffee -c index.coffee' to see the js code:
(function() {
var Domain,
__hasProp = {}.hasOwnProperty;
Domain = (function() {
function Domain() {}
Domain.prototype.consructor = function(obj) {
var key, value, _results;
_results = [];
for (key in obj) {
if (!__hasProp.call(obj, key)) continue;
value = obj[key];
_results.push(this.key = value);
}
return _results;
};
Domain.prototype.save = function(fn) {};
return Domain;
})();
module.exports = Domain;
}).call(this);
From the compiled js, i see the '_result' array being generated and returned but never binded to 'this'. How would i bind the array into the class scope to pass the test?Thank You
Your line of CoffeeScript
#key = value
is not equivalent to the javascript you want
this[key] = obj[key];
The way # works is that it is simply replaced by either this or this.. Thus, #key compiles to this.key. You should instead use
#[key] = value
Additionally, you spelled constructor wrong (as consructor). In CoffeeScript, constructor is a special method that compiles differently from a normal method; if it is spelled wrong, CoffeeScript assumes you want an empty one.
consructor: (obj) ->
Domain.prototype.consructor
You are missing a t.
(I keep saying that)