forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStorage.ts
More file actions
127 lines (106 loc) · 2.85 KB
/
ArrayStorage.ts
File metadata and controls
127 lines (106 loc) · 2.85 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
import { inject, injectable, postConstruct } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import { IArrayStorage } from '../interfaces/storages/IArrayStorage';
import { IOptions } from '../interfaces/options/IOptions';
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
import { initializable } from '../decorators/Initializable';
@injectable()
export abstract class ArrayStorage <V> implements IArrayStorage <V> {
/**
* @type {IRandomGenerator}
*/
protected readonly randomGenerator: IRandomGenerator;
/**
* @type {IOptions}
*/
protected readonly options: IOptions;
/**
* @type {V[]}
*/
@initializable()
protected storage!: V[];
/**
* @type {string}
*/
@initializable()
protected storageId!: string;
/**
* @type {number}
*/
private storageLength: number = 0;
/**
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.randomGenerator = randomGenerator;
this.options = options;
}
@postConstruct()
public initialize (): void {
this.storage = [];
this.storageId = this.randomGenerator.getRandomString(6);
}
/**
* @param {number} key
* @returns {V}
*/
public get (key: number): V {
const value: V | undefined = this.storage[key];
if (!value) {
throw new Error(`No value found in array storage with key \`${key}\``);
}
return value;
}
/**
* @param {V} value
* @returns {number}
*/
public getKeyOf (value: V): number | null {
const key: number = this.storage.indexOf(value);
return key >= 0 ? key : null;
}
/**
* @returns {number}
*/
public getLength (): number {
return this.storageLength;
}
/**
* @returns {V[]}
*/
public getStorage (): V[] {
return this.storage;
}
/**
* @returns {string}
*/
public getStorageId (): string {
return this.storageId;
}
/**
* @param {this} storage
* @param {boolean} mergeId
*/
public mergeWith (storage: this, mergeId: boolean = false): void {
this.storage = [...this.storage, ...storage.getStorage()];
if (mergeId) {
this.storageId = storage.getStorageId();
}
}
/**
* @param {number} key
* @param {V} value
*/
public set (key: number, value: V): void {
if (key === this.storageLength) {
this.storage.push(value);
} else {
this.storage.splice(key, 0, value);
}
this.storageLength++;
}
}