2020-04-19 14:22:10 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
|
|
|
@Injectable({
|
2020-04-21 17:26:25 +02:00
|
|
|
providedIn: 'root',
|
2020-04-19 14:22:10 +02:00
|
|
|
})
|
|
|
|
export class PollUtilsService {
|
2020-04-21 17:26:25 +02:00
|
|
|
public makeUuid(): string {
|
|
|
|
// TODO: how to be sure the uuid generated in front is available in backend ?
|
|
|
|
// It could be a better way to generate uuids in backend.
|
|
|
|
return uuidv4();
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
2020-04-21 17:26:25 +02:00
|
|
|
/**
|
|
|
|
* make a uniq slug for the current poll creation
|
|
|
|
* @param str
|
|
|
|
*/
|
2020-05-01 19:10:17 +02:00
|
|
|
public makeSlug(config: any): string {
|
2020-04-21 17:26:25 +02:00
|
|
|
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();
|
2020-04-19 14:22:10 +02:00
|
|
|
|
2020-04-21 17:26:25 +02:00
|
|
|
// remove accents, swap ñ for n, etc
|
|
|
|
const from = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;';
|
|
|
|
const to = 'aaaaeeeeiiiioooouuuunc------';
|
|
|
|
for (let i = 0, l = from.length; i < l; i++) {
|
|
|
|
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
2020-04-21 17:26:25 +02:00
|
|
|
str = str
|
|
|
|
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
|
|
|
|
.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
|
|
|
.replace(/-+/g, '-'); // collapse dashes
|
2020-04-19 14:22:10 +02:00
|
|
|
|
2020-04-21 17:26:25 +02:00
|
|
|
return str;
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
}
|