-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelfer.js
More file actions
145 lines (135 loc) · 5.79 KB
/
helfer.js
File metadata and controls
145 lines (135 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const noten = [null, 'sehr gut', 'gut', 'befriedigend', 'ausreichend', 'mangelhaft', 'ungenügend']
const enoten = { 'E1': 'mit besonderem Erfolg teilgenommen', 'E2': 'mit Erfolg teilgenommen', 'E3': 'teilgenommen' }
const punkte = {
15: '1+', 14: '1', 13: '1-',
12: '2+', 11: '2', 10: '2-',
9: '3+', 8: '3', 7: '3-',
6: '4+', 5: '4', 4: '4-',
3: '5+', 2: '5', 1: '5-',
0: '6' }
const zahlwort = { 1: "eins",2: "zwei",3: "drei",4: "vier",5: "fünf",6: "sechs",7: "sieben",8: "acht",9: "neun",0: "null" }
// Verzichten wir auf teure lodash-Funktionen:
export const groupBy = (arr, id) => arr.reduce(
(entryMap, f) => {
const fx = id.split('.').reduce((p,c)=>p&&p[c]||null, f)
return entryMap.set(fx, [...entryMap.get(fx)||[], f])
},
new Map()
)
export const chunk = (arr, size) => arr.reduce((chunks, el, i) => (i % size
? chunks[chunks.length - 1].push(el)
: chunks.push([el])) && chunks, [])
export const datum = (t) => {
// gibt ein Datum im deutschen Format zurück
try {
return new Date(t).toLocaleDateString('de', {day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'Europe/Berlin'})
} catch (e) {console.log(e); return 'undefined - Datumsfehler'}
}
export const bemerkungen = (hj) => hj.ZeugnisBem ? hj.ZeugnisBem.replace('\r\n', '<br/>') : 'keine'
export const volljaehrigBei = (s, datum) => {
// gibt an, ob der Schüler *s* zu einem Zeitpunkt *datum* volljährig war
try {
const g = new Date(s.Geburtsdatum)
const d = new Date(datum)
const volljaehrig = d.getFullYear() - g.getFullYear() - ((d.getMonth() > g.getMonth() || (d.getMonth() == g.getMonth() && d.getDay() >= g.getDay()) ? 0 : 1)) >= 18
return volljaehrig
} catch (e) {console.log(e); return}
}
export const note = (note) => noten[parseInt(note)] || enoten[note]
export const punkte2note = (p) => punkte[parseInt(p)]
export const noteInWorten = (n) => n.split('').map(n => n === ',' ? '/' : zahlwort[parseInt(n)]).join(' ')
export function slugify(text, separator) {
text = text.toString().trim();
const sets = [
{ to: "a", from: "[ÀÁÂÃÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]" },
{ to: "ae", from: "[Ä]" },
{ to: "c", from: "[ÇĆĈČ]" },
{ to: "d", from: "[ÐĎĐÞ]" },
{ to: "e", from: "[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]" },
{ to: "g", from: "[ĜĞĢǴ]" },
{ to: "h", from: "[ĤḦ]" },
{ to: "i", from: "[ÌÍÎÏĨĪĮİỈỊ]" },
{ to: "j", from: "[Ĵ]" },
{ to: "ij", from: "[IJ]" },
{ to: "k", from: "[Ķ]" },
{ to: "l", from: "[ĹĻĽŁ]" },
{ to: "m", from: "[Ḿ]" },
{ to: "n", from: "[ÑŃŅŇ]" },
{ to: "o", from: "[ÒÓÔÕØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]" },
{ to: "oe", from: "[΅]" },
{ to: "p", from: "[ṕ]" },
{ to: "r", from: "[ŔŖŘ]" },
{ to: "s", from: "[ŚŜŞŠ]" },
{ to: "ss", from: "[ß]" },
{ to: "t", from: "[ŢŤ]" },
{ to: "u", from: "[ÙÚÛŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]" },
{ to: "ue", from: "[Ü]" },
{ to: "w", from: "[ẂŴẀẄ]" },
{ to: "x", from: "[ẍ]" },
{ to: "y", from: "[ÝŶŸỲỴỶỸ]" },
{ to: "z", from: "[ŹŻŽ]" },
{ to: "-", from: "[·/_,:;']" },
];
sets.forEach((set) => {
text = text.replace(new RegExp(set.from, "gi"), set.to);
});
text = text
.toString()
// .toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-+/g, "") // Replace multiple - with single -
// .replace(/\--+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
if (typeof separator !== "undefined" && separator !== "-") {
text = text.replace(/-/g, separator);
}
return text;
}
function generateHash(str) {
let hash = 0;
if (str.length === 0)
return hash;
for (let i = 0; i < str.length; i++)
hash = (((hash << 5) - hash) + str.charCodeAt(i)) | 0;
return Math.abs(hash);
}
// im kommenden Jahr sollen die Passwörter mit Hilfe der Usernames ermittelt werden, die nun individuell sind + hash
import { names } from "./names";
export const updater = (schueler) => {
const set = new Set();
const hashset = new Set();
let counter = 0;
for (const s of schueler) {
if (s.Geburtsdatum === null) {
// console.log(s.Vorname, s.Name, "ohne Geburtsdatum");
s.Geburtsdatum = new Date();
}
if (s.Geburtsdatum.toString().length > 10)
s.Geburtsdatum = new Date(s.Geburtsdatum).toJSON().slice(0, 10);
s.username = `${slugify(s.Vorname).slice(0, 3)}${slugify(s.Name).slice(0,4)}`.toLowerCase();
s.Klasse = /^.*[0-9]{2,}.*?$/.test(s.Klasse) ? s.Klasse.slice(0, -1) : s.Klasse;
const o = names.get(s.GU_ID);
if (o) {
s.Vorname = o.name || s.Vorname;
s.Geschlecht = o.geschlecht || s.Geschlecht;
s.Klasse = o.klasse || s.Klasse;
s.username = o.username || `${slugify(s.Vorname).slice(0,3)}${slugify(s.Name).slice(0,4)}`.toLowerCase();
console.log(JSON.stringify(s));
counter++;
names.delete(s.GU_ID);
}
if (set.has(s.username))
throw new Error(`doppelter Username, muss ersetzt werden: <br>names.set("${s.GU_ID}", {username: "${slugify(s.Vorname).slice(0,2).toLowerCase()}${slugify(s.Name).slice(0,5).toLowerCase()}"});// ${s.Vorname} ${s.Name}`);
set.add(s.username);
s.hash = generateHash(s.username);
if (hashset.has(s.hash))
console.warn('Hash mehrfach vorhanden', s.Name, s.Vorname, s.Geburtsdatum, s.hash, s.Klasse);
hashset.add(s.hash)
}
console.log(hashset.size, 'Hashes erstellt, ', `${counter} Usernames ersetzt.`)
console.log((names.size > 0) ? `${[...names.values()].map(v => v.username).toString()} entfernen`:'');
return schueler;
}