-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutonumeric.js
58 lines (50 loc) · 1.88 KB
/
Autonumeric.js
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
import {WebSystemObject} from "./WebSystemObject.js";
export class Autonumeric extends WebSystemObject {
static #globalId = 0;
constructor(lastPrevious = Autonumeric.#globalId, optimize = true, global = false) {
super();
this.global = global;
this.optimize = optimize;
this.lastPrevious = lastPrevious.map(x => Number(x));
}
get value() {
// Función para generar un autonumérico a partir de un arreglo de id's.
function autonum(array) {
// Javascript program to find the smallest elements missing in a sorted array.
function firstHole(array, start = 0, end = array.length - 1) {
if (start > end) return end + 1;
if (start !== array[start]) return start;
let mid = Math.trunc((start + end) / 2);
// Left half has all elements from 0 to mid
if (array[mid] === mid) return firstHole(array, mid + 1, end);
return firstHole(array, start, mid);
}
return firstHole(array.sort((a, b) => a - b));
}
let resultado;
if (this.lastPrevious instanceof Array) {
if (this.optimize) {
resultado = autonum(this.lastPrevious);
} else {
resultado = Math.max(...this.lastPrevious) + 1;
}
this.lastPrevious.push(resultado);
} else {
resultado = this.lastPrevious++;
}
if (this.global) {
Autonumeric.#globalId = resultado;
}
return resultado;
}
set value(v) {
if (this.lastPrevious instanceof Array) {
this.lastPrevious.push(v);
} else {
this.lastPrevious = v;
}
if (this.global) {
Autonumeric.#globalId = v;
}
}
}