Is there a way to use jade mixin inside javascript code block?
I have next use case:
mixin YYYYMMDD(date)
= date.getFullYear() + '-'
= ('0' + (date.getMonth() + 1)).slice(-2) + '-'
= ('0' + date.getDate()).slice(-2)
So I need to use this mixin inside
input(
value=YYYYMMDD(date)
)
Update:
I had to remake those mixins to js functions, so it looks similar to next:
- function YYYYMMDD(date)
- var flDate = date.getFullYear() + '-';
- flDate += ('0' + (date.getMonth() + 1)).slice(-2) + '-';
- flDate += ('0' + date.getDate()).slice(-2);
- return flDate;
the easiest way is, that you write this code on your server, then you can also use some other syntax, for instance coffeescript or even better in this case, some libraries like moment
When you render your template, you just add a helper object to your locals:
var moment = require('moment');
var YYYYMMDD = function(date) {
return moment(date).format('YYYYMMDD');
}
// express code
app.get('/test', function(req, res, next) {
var locals = getLocalsForTest();
locals.helpers = {
YYYYMMDD: YYYYMMDD
};
res.render('template.jade', {locals: locals});
});
and in your template you can just call this
input(value=helpers.YYYYMMDD(date))
Related
I have a for loop in my nodejs code
const saveDocument = co.wrap(function *(documentData, user, locale) {
var now = moment();
var creationDateLongString = now.format("YYYYMMDDHHmmss");
var creationDateShortString = now.format("YYYYMMDD");
var outputChildFolder = documentData.code + '_' + creationDateLongString + '_' + documentCounter;
var outputFolder = config.files.incomingDocumentsDir + '/' + outputChildFolder;
++documentCounter;
yield fs.mkdir(outputFolder)
var xmlFileName = documentData.code + "-" + creationDateLongString + ".xml";
var pdfFileName = documentData.code + "-" + creationDateLongString + ".pdf";
const pages = [];
for(var index=0;index < documentData.pages.length; ++index) {
const page = documentData.pages[index];
var data = new Buffer(page, "base64");
var dataEncoding = imageType(data).mime === "image/png" ? "png" : "jpg";
var fileName = "page" + index + "." + dataEncoding;
var targetFilePath = outputFolder + "/" + fileName
yield fs.writeFile(targetFilePath,data);
pages.push(fileName);
}
...
}
What I don't understand is why in the above code page only gets assigned once, on the first iteration, and holds that same value during the other iterations. So if I have 5 pages I end up 5 times with the data of the first page in that variable.
I am running node 4 without any special arguments or postprocessor. Simply npm run ... which maps to a node src/main/myApp.js in my package.json
I am probably missing something simple here but I've never seen this before when doing client side ES6 code. The big difference of course being that the client side code goes through Babel + Webpack and the server side code is ran directly through node.
Small addendum: if you are wondering why the "old school" for syntax and not something along the lines of pages.forEach(...., it's because this is existing code where I just did a few minor modifications.
This will work as you are expecting in strict mode. Try adding...
"use strict";
You will only see this behavior in environments (like Node) that actually respect and enforce the keyword. Babel simply converts all let and const to var right now to provide ES5 compatibility. To demonstrate, take a look at this Babel example. You can see in the output that const has been changed to var
I am trying to define a function, which can be used in all parts of my program. The program consists of several files with code, and I define the function in the first part like this:
file 1:
var debug_log = fs.createWriteStream('./' + '/debug.log', {flags : 'w'});
var debug = function(d){
debug_log.write(util.format(d) + '\n');
};
var part2 = require('./somefile.js');
part2.part2(function(result){
//some work is done with the result
});
file 2:
function part2(callback){
//some work is done with initial data
debug('done'); //here is were I am trying to use the function
callback(data);
}
exports.part2 = part2;
The important part is the function "debug" which I am using to log my results. I was using console.log before with some small changes like this:
var console_log = fs.createWriteStream('./' + '/console.log', {flags : 'w'});
var log_stdout = process.stdout;
console.log = function(d){
console_log.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
and it worked fine in every part of the program, so why doesnt the other (similar) function work? Is it because console.log was already defined before (by default)?
Yes.
debug is not defined anywhere in file 2, so it does not work in file 2.
You can use events, this is a example from cordova code:
//file_events.js
module.exports = new (require('events').EventEmitter)();
//main.js
var events = require('./file_events.js');
var debug_log = fs.createWriteStream('./' + '/debug.log', {flags : 'w'});
var debug = function(d){
debug_log.write(util.format(d) + '\n');
};
var part2 = require('./somefile.js');
part2.part2(function(result){
//some work is done with the result
});
events.on("log", debug)
//somefile.js
var events = require('./file_events.js');
function part2(callback){
//some work is done with initial data
events.emit('log', 'done'); //emit a event catched for the main file on
callback(data);
}
exports.part2 = part2;
PD: The code is not tested but must work, with little fixs. The main strategy es call the function by the event library.
I think you'll want to take a look at:
http://nodejs.org/api/modules.html
To access the debug function from other files, you'll want to expose your debug function in module.exports
In file1 (lets call it debug.js):
var debug_log = fs.createWriteStream('./' + '/debug.log', {flags : 'w'});
var debug = function(d){
debug_log.write(util.format(d) + '\n');
};
var part2 = require('./somefile.js');
part2.part2(function(result){
//some work is done with the result
});
module.exports = {
debug: debug
}
Then in file2:
var debug = require('./debug').debug; //assuming debug.js is in the same directory
function part2(callback){
//some work is done with initial data
debug('done'); //here is were I am trying to use the function
callback(data);
}
exports.part2 = part2;
I need the current system datetime in the format "yyyy-mm-dd:hh:mm:ss".
https://stackoverflow.com/a/19079030/2663388 helped a lot.
new Date().toJSON() is showing "2014-07-14T13:41:23.521Z"
Can someone help me to extract "yyyy-mm-dd:hh:mm:ss" from "2014-07-14T13:41:23.521Z"?
Seems that there is no good way to do it with original code unless using Regex. There are some modules such as Moment.js though.
If you are using npm:
npm install moment --save
Then in your code:
var moment = require('moment');
moment().format('yyyy-mm-dd:hh:mm:ss');
That may be much easier to understand.
What about:
new Date().toString().replace(/T/, ':').replace(/\.\w*/, '');
Returns for me:
2014-07-14:13:41:23
But the more safe way is using Date class methods which works in javascript (browser) and node.js:
var date = new Date();
function getDateStringCustom(oDate) {
var sDate;
if (oDate instanceof Date) {
sDate = oDate.getYear() + 1900
+ ':'
+ ((oDate.getMonth() + 1 < 10) ? '0' + (oDate.getMonth() + 1) : oDate.getMonth() + 1)
+ ':' + oDate.getDate()
+ ':' + oDate.getHours()
+ ':' + ((oDate.getMinutes() < 10) ? '0' + (oDate.getMinutes()) : oDate.getMinutes())
+ ':' + ((oDate.getSeconds() < 10) ? '0' + (oDate.getSeconds()) : oDate.getSeconds());
} else {
throw new Error("oDate is not an instance of Date");
}
return sDate;
}
alert(getDateStringCustom(date));
Returns in node.js:
/usr/local/bin/node date.js 2014:07:14:16:13:10
And in Firebug:
2014:07:14:16:14:31
Install moment using
npm install moment --save
And in your code import the moment like this.
var moment = require('moment')
var created = moment().format('YYYY-MM-DD hh:mm:ss')
Though other answers are helpful I found the following code is working for me.
var d = new Date();
console.log(d.toJSON().slice(0,19).replace('T',':'));
The output on console is: 2014-07-15:06:10:16.
I am using Node.js Express on Ubuntu.
Cleaner version of #Bernhard code using padStart and without deprecated getYear
function getCurrentDateTimeString() {
const date = new Date();
return date.getFullYear() + '-' +
(date.getMonth() + 1).toString().padStart(2, '0') + '-' +
date.getDate().toString().padStart(2, '0') + ':' +
date.getHours().toString().padStart(2, '0') + ':' +
date.getMinutes().toString().padStart(2, '0') + ':' +
date.getSeconds().toString().padStart(2, '0');
}
console.log(getCurrentDateTimeString());
Explicity call each part of the Date:
function formatDate(date) {
return [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-') + ':' +
[date.getHours(), date.getMinutes(), date.getSeconds()].join(':');
}
function formatDatePart(part) {
return part < 10 ? ("0" + part) : part;
}
You can see an example on this fiddle.
Correct way is this
var moment = require('moment');
moment().format('Y-M-D H:m:s');
You can do it using toLocaleString() method in node.js
let date = new Date('2014-07-14T13:41:23.521Z');
console.log(date.toLocaleString('en-US'));
//Converting current date time without timezone
date = new Date()
console.log(date.toLocaleString('en-US'));
I have a directory tmp that has 3 test node.js modules [mod0.js, mod1.js, mod2.js].
I am attempting to write a function in order to import these three modules into an array and then return said array. I can drop to REPL and can import each file using var x = require("./tmp/mod0"); etc without any issue.
When I attempt to run the following function though to automate this, I receive the error [Error: Cannot fine module './tmp/mod0'].
var _importFiles = function(path, files){
var moduleList = []
, trimmedName;
files.forEach(function (element, index, array){
if (_fs.lstatSync(path + "/" + element).isFile()){
trimmedName = element.substring(0, (element.length - 3));
moduleList.push(require("./" + path + "/" + trimmedName));
}
});
return moduleList;
};
I am passing in 'tmp' for the path parameter and the output of fs.readdirSync(path) for the files parameter.
If I check process.cwd(); within the if block, it matches that of the REPL console.
I'm trying to figure out why it works when I manually do it in REPL but not automated.
I modified the code slightly to this:
var _fs = require('fs');
var path = process.cwd() + '/tmp'
var _importFiles = function(path, files){
var moduleList = [], trimmedName;
files.forEach(function (element, index, array){
if (_fs.lstatSync(path + "/" + element).isFile()){
trimmedName = element.substring(0, (element.length - 3));
moduleList.push(require("./" + path + "/" + trimmedName));
}
});
return moduleList;
};
var imports = _importFiles('./tmp', _fs.readdirSync(path));
console.log(imports);
Which gives me:
$ node import.js
[ 'imported mod0 automatically', 'imported mod1 automatically' ]
The mod files are simple module.exports = "imported mod(x) automatically";
So now my return list has an array. Also; Make sure your directory has read permissions (which im sure it does)
i am using https://github.com/gf3/sandbox#readme but i am not getting that how can i pass some data from my code to javascript code through this sandbox. for example
var s = new sandBox();
s.run("(function(name) { return 'Hi there, ' + name + '!'; })('Fabio')", function(output) {
console.log("Example 2: " + output.result + "\n")
})
now i want to pass some data to this function how can i do that?
It does not have any clean way to pass arguments. However, since you are passing code as a string anyway, you can simply add the arguments directly to the string:
var arg = 'Hello';
var code = "(function(name) { return 'Hi there, ' + name + '!'; })("+ JSON.stringify(arg) +")"
s.run(code, ...);
I'm using JSON.stringify to ensure that the string is a valid JS expression.