parent
ab23b9b256
commit
c5b5276400
@ -0,0 +1,59 @@
|
||||
export class DateUtilities {
|
||||
|
||||
/**
|
||||
* add some days to a date, to compute intervals
|
||||
* @param days
|
||||
* @param date
|
||||
*/
|
||||
addDaysToDate(days: number, date: Date) {
|
||||
date = new Date(date.valueOf());
|
||||
date.setDate(date.getDate() + days);
|
||||
return date;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param d1
|
||||
* @param d2
|
||||
* @param interval
|
||||
*/
|
||||
getDatesInRange(d1: Date, d2: Date, interval: number) {
|
||||
d1 = new Date(d1);
|
||||
d2 = new Date(d2);
|
||||
const dates = [];
|
||||
while (+d1 < +d2) {
|
||||
dates.push({
|
||||
literal: this.formateDate(d1),
|
||||
date_object: d1
|
||||
});
|
||||
d1.setDate(d1.getDate() + interval)
|
||||
}
|
||||
return dates.slice(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the number of days between two dates
|
||||
* @param d1
|
||||
* @param d2
|
||||
*/
|
||||
dayDiff(d1: Date, d2: Date): Number {
|
||||
return Number(((d2.getTime()) - (d1.getTime()) / 31536000000));
|
||||
}
|
||||
|
||||
/**
|
||||
* format a date object to the date format used by the inputs of type date
|
||||
* YYYY-MM-DD
|
||||
* @param date
|
||||
*/
|
||||
formateDate(date) {
|
||||
return [
|
||||
date.getFullYear(),
|
||||
this.getDoubleDigits(date.getMonth() + 1),
|
||||
this.getDoubleDigits(date.getDate()),
|
||||
].join('-')
|
||||
}
|
||||
|
||||
getDoubleDigits(str) {
|
||||
return ("00" + str).slice(-2);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
import {HttpHeaders} from "@angular/common/http";
|
||||
import {PollConfig} from "./PollConfig";
|
||||
|
||||
export class PollUtilities {
|
||||
// utils functions
|
||||
/**
|
||||
* generate unique id to have a default url for future poll
|
||||
*/
|
||||
makeUuid() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* make a uniq slug for the current poll creation
|
||||
* @param str
|
||||
*/
|
||||
makeSlug(config: PollConfig) {
|
||||
let str = '';
|
||||
str = config.creationDate.getFullYear() + '_' + (config.creationDate.getMonth() + 1) + '_' + config.creationDate.getDate() + '_' + config.myName + '_' + config.title;
|
||||
str = str.replace(/^\s+|\s+$/g, ''); // trim
|
||||
str = str.toLowerCase();
|
||||
|
||||
// remove accents, swap ñ for n, etc
|
||||
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
|
||||
var to = "aaaaeeeeiiiioooouuuunc------";
|
||||
for (var i = 0, l = from.length; i < l; i++) {
|
||||
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
||||
}
|
||||
|
||||
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
|
||||
.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
||||
.replace(/-+/g, '-'); // collapse dashes
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare headers like the charset and json type for any call to the backend
|
||||
* @param bodyContent
|
||||
*/
|
||||
makeHeaders(bodyContent?: any) {
|
||||
|
||||
const headerDict = {
|
||||
'Charset': 'UTF-8',
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
headers: new HttpHeaders(headerDict),
|
||||
body: bodyContent
|
||||
};
|
||||
|
||||
return requestOptions;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue