Skip to content

Commit

Permalink
Server testing moment.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Vuthy committed Mar 24, 2019
1 parent d2675f5 commit a2f9184
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 199 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var moment = require('moment')
//var fn = require('fn');

var hbs = require('./lib/handlebar-helpers')

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('view engine', hbs.extname);
app.set('port', (process.env.PORT || 5000));

var Handlebars = require("handlebars");
var MomentHandler = require("handlebars.moment");
MomentHandler.registerHelpers(Handlebars);

var fake_account = require('./fake')
var routes = require('./routes/index');
Expand Down
75 changes: 38 additions & 37 deletions lib/handlebar-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ const moment = require('moment-timezone')

var hbs = exphbs.create({
defaultLayout: 'main',
extname: '.handlebars',
// Specify helpers which are only registered on this instance.
helpers: {
toFormalDate: function(date, form){
var newDate = moment.tz(date, "Asia/Bangkok");
var TODAY = moment();

if (newDate.isSame(TODAY, 'h')) {
return newDate.startOf('minute').fromNow();
console.log("hour the same");
} else if(newDate.isSame(TODAY, 'd')){
return newDate.startOf('hour').fromNow();
console.log("day the same");
} else {
return newDate.format("MMM Do YYYY, h:mm a");
return newDate.format("MMM Do YYYY, h:mm a");
console.log("else the same");
}
},
ifEvent: function(approve_info_obj, options){
/*
* approve_info_obj = ..rows[0].approve_info
* approve_info_obj = ..rows[0].approve_info
*/

//Validation
if(!approve_info_obj){
return;
}

var events = {approve: 0, disapprove:0}; //declear objects
var events = {approve: 0, disapprove:0}; //declear objects
var results = functions.eventCounter(approve_info_obj); //return number of events
results = Promise.resolve(results);
results.then(function(sub_result){
Expand All @@ -51,15 +52,15 @@ var hbs = exphbs.create({
var result = '';
var addO = function(number){
if(number < 10){
return "0" + String(number);
return "0" + String(number);
}
return(number);
}
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
var toDateMonth = function(dateZone){
var today = new Date(dateZone);
var date = today.getDate();
var mon = today.getMonth();
var mon = today.getMonth();

return String(month[mon]) + ", " + addO(date);
}
Expand All @@ -78,20 +79,20 @@ var hbs = exphbs.create({

sorten: function(dataCollection, options){
console.log(dataCollection);
var resultColletion = [];
var resultColletion = [];
var time = [];
var stc = new Array();

var username = 0;
var budget = 1;
var id = 2;
var date = 3;
var id = 2;
var date = 3;
var info = '';
function conDate(date) {
return new Date(date).getTime();
}

if(dataCollection){
if(dataCollection){

for (var constance = 0; constance < dataCollection.length; constance++) {
if(time == 0){
Expand All @@ -100,23 +101,23 @@ var hbs = exphbs.create({
var Isee = 0;
for(var i = 0; i < time.length; i++){
if(time[i] == conDate(dataCollection[constance][date])){
Isee = 1;
Isee = 1;
}
}
if(Isee == 0){
time.push(conDate(dataCollection[constance][date]));
}
}

for (var varies = 0; varies < dataCollection.length; varies++) {
for (var varies = 0; varies < dataCollection.length; varies++) {

if (dataCollection[constance][username] === dataCollection[varies][username]) {
if (resultColletion == 0) {
if (resultColletion == 0) {
resultColletion.push(dataCollection[varies]);
} else {
var found = 0;
for (var inner = 0; inner < resultColletion.length; inner++) {
if(resultColletion[inner][username] == dataCollection[varies][username] &&
if(resultColletion[inner][username] == dataCollection[varies][username] &&
conDate(resultColletion[inner][date]) == conDate(dataCollection[varies][date])){

if(resultColletion[inner][id] != dataCollection[varies][id]){
Expand All @@ -126,8 +127,8 @@ var hbs = exphbs.create({
dataCollection[varies][id] = resultColletion[inner][id]; // change the constance id
}

found = 1;
}
found = 1;
}
}
if(found == 0){
resultColletion.push(dataCollection[varies]);
Expand All @@ -143,9 +144,9 @@ var hbs = exphbs.create({
if(time[i] === conDate(resultColletion[k][date])){
stc[i].push(resultColletion[k]);
}
}
}
}
info += options.fn({day1: stc[0], day2: stc[1], day3: stc[2],
info += options.fn({day1: stc[0], day2: stc[1], day3: stc[2],
day4: stc[3], day5: stc[4], day6: stc[5], day7: stc[6]});
}else{
return info += options.fn({noData: "There's no Exchange!"});
Expand All @@ -156,7 +157,7 @@ var hbs = exphbs.create({
ifCond: function(v1, v2, options) {
if (v1 == v2) {
return options.fn(this);
}
}
return options.inverse(this);
},

Expand All @@ -165,20 +166,20 @@ var hbs = exphbs.create({
for(var i = 1; i < x; i++){
if (z[i] == y) {
return options.fn(this);
}
}
return options.inverse(this)
}
},

finding: function(masterObj, val, options){
/*
* masterObj = ..rows[0].approve_info
* masterObj = ..rows[0].approve_info
*/
var arr = [];

var keysVal = Object.keys(masterObj.body);

for(var z = 0; z < keysVal.length; z++){
for(var z = 0; z < keysVal.length; z++){
arr.push(`${masterObj.body[keysVal[z]].email}`);
}

Expand All @@ -187,7 +188,7 @@ var hbs = exphbs.create({
if(arr[i] == val){
return options.fn(this);
}
}
}
}
return options.inverse(this);
},
Expand Down Expand Up @@ -226,7 +227,7 @@ var hbs = exphbs.create({

return disapprove;
},

countApprove: function(masterObj){
var approve = 0;

Expand Down Expand Up @@ -270,7 +271,7 @@ var hbs = exphbs.create({
}
},

upperCase: function(text) {
upperCase: function(text) {
if (text == null) {
return text;
}
Expand All @@ -280,10 +281,10 @@ var hbs = exphbs.create({
} else {
return text;
}

},
lowerCase: function(text) {

lowerCase: function(text) {
if (text == null) {
return text;
}
Expand All @@ -293,14 +294,14 @@ var hbs = exphbs.create({
} else {
return text;
}

},

firstLetter: function(username) {
return username[0]
},

formatDate: function(date, format, zone) {
formatDate: function(date, format, zone) {
if (date == null) {
return date;
}
Expand All @@ -311,7 +312,7 @@ var hbs = exphbs.create({
newDate.setHours(hours - offset);

newDate = moment(newDate);

if(zone == true){
newDate = moment.tz(date, "Asia/Bangkok");
}
Expand All @@ -321,7 +322,7 @@ var hbs = exphbs.create({
if (newDate.isSame(TODAY, 'd')) {
return newDate.format("h:mm A")
} else {
return newDate.format("MMMM DD, YYYY");
return newDate.format("MMMM DD, YYYY");
}
} else if (format == 'standard') {
return newDate.format('MMM DD YYYY h:mm A');
Expand All @@ -330,12 +331,12 @@ var hbs = exphbs.create({
newDate = moment(newDate).add(7, 'hours');
return newDate.format('MMM DD YYYY h:mm A');
} else if (format == 'fromNow') {
return newDate.startOf('hour').fromNow();
return newDate.startOf('hour').fromNow();
} else {
return newDate.format(format);
}


return moment().format();
},

Expand Down Expand Up @@ -381,7 +382,7 @@ var hbs = exphbs.create({
if (interval >= 2) { //more than two days
return [pad(newDate.getDate()), monthNames[newDate.getMonth()]+',', newDate.getFullYear()].join(' '); //date format seperated by .
}


function plural(number, text) {
if (number == 1) {
Expand Down Expand Up @@ -411,4 +412,4 @@ var hbs = exphbs.create({
}
});

module.exports = hbs;
module.exports = hbs;
Loading

0 comments on commit a2f9184

Please sign in to comment.