Skip to content

Instantly share code, notes, and snippets.

@daylik
Last active August 31, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daylik/1466450e1f8a067a40810c2d012c5674 to your computer and use it in GitHub Desktop.
Save daylik/1466450e1f8a067a40810c2d012c5674 to your computer and use it in GitHub Desktop.
global.timer_start = {}; //if browser replace all to window.timer_start
const jr = { //if browser js replace 'const' to 'var'
timer: {
start: function(timer_name) {
global.timer_start[timer_name] = new Date().getTime();
},
stop: function(timer_name) {
var timer_stop = new Date().getTime();
var out = timer_stop - global.timer_start[timer_name];
return 'timer ' + timer_name + ': ' + out + 'ms';
},
stop_log: function(timer_name) {
var timer_stop = new Date().getTime();
var out = timer_stop - global.timer_start[timer_name];
console.log('timer ' + timer_name + ': ' + out + 'ms');
}
},
isset: function(v_var) {
if (typeof(v_var) == 'number') { if (isNaN(v_var)) { return false; } }
if (typeof(v_var) == 'undefined' || v_var === null) { return false; }
return true;
},
fix_00: function(x) {
return ((x < 10 ? '0' : '') + x);
},
to_number: function(v) {
return parseInt(v, 10);
},
get_time: function() {
var d = new Date();
return '' + jr.fix_00(d.getHours()) + ':' + jr.fix_00(d.getMinutes());
},
get_date: function() {
var d = new Date();
return '' + jr.fix_00(d.getDate()) + '.' + jr.fix_00(d.getMonth() + 1) + '.' + d.getFullYear();
},
date_plus: function(time_1, time_2) {
time_1 = time_1.split(':');
time_2 = time_2.split(':');
time_1[0] = jr.to_number(time_1[0]);
time_1[1] = jr.to_number(time_1[1]);
time_2[0] = jr.to_number(time_2[0]);
time_2[1] = jr.to_number(time_2[1]);
var hour_plus = 0;
var minutes = jr.fix_00(time_1[1] + time_2[1]);
if (minutes >= 60) {
minutes = jr.fix_00(minutes - 60);
hour_plus = 1;
}
var hour = jr.fix_00(time_1[0] + time_2[0] + hour_plus);
if (hour >= 24) {
hour = jr.fix_00(hour - 24);
}
return hour + ':' + minutes;
},
regex_compile: function(v, name, regexp_flags) {
// use: jr.regex_compile(array_strings, object_name_in_array, (regexp flags, to return finalregexp object)? );
var regexp_str = '';
for (var i = 0; i < v.length; i++) {
if (i === 0) {
regexp_str += v[i][name];
} else {
regexp_str += '|' + v[i][name];
}
}
if (jr.isset(regexp_flags)) {
var regexp_str = new RegExp(regexp_str, regexp_flags);
}
return regexp_str;
},
hr: function() {
console.log('------------------');
}
};
module.exports = jr; //if brwser javascript, comment this
//include in nodejs
//const jr = require('./nodejs__jr_functions');
//####### SAMPLES #######
//console.log(jr.date_plus('01:30', '22:31'));
// const v_regexp = [
// {num: '1', regexp_str: 'дорог(ой|ая|ие)'},
// {num: '2', regexp_str: 'дешев(ый|ая|ие|ле)'},
// {num: '3', regexp_str: 'хорош(ий|ая|ие|ее)'}
// ];
// var regexp_match = jr.regex_compile(v_regexp, 'regexp_str', 'ig');
// var v_str = 'хороший';
// if( regexp_match.test(v_str) ){
// true
// }
//console.log(jr.regex_compile(v_regexp, 'num'));
//console.log(jr.regex_compile(v_regexp, 'num', 'ig'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment