-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
159 lines (137 loc) · 2.83 KB
/
index.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// TODO:
// - rename this module
// - use nyc over istanbul
// - upgrade ts to latest version
// - run travis ci with more nodejs versions (12 -- 17)
// - setup linter
/*!
* arraylist-tn
*
* Copyright(c) 2019-2021 Imed Jaberi
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
import { IArrayList } from './index.model';
/**
* Java ArrayList implementation
*
* @api public
*/
class ArrayList<T = unknown> implements IArrayList<T> {
#list: Array<T>;
constructor() {
this.#list = [];
}
/**
* add one value to the array list
*/
public add(value: T): void {
this.#list.push(value);
}
/**
* add all values to the array list
*/
public addAll(array: T[]): void {
array.forEach((element) => this.#list.push(element));
}
/**
* delete element with the input index
*/
public remove(index: number): void {
this.#list = this.#list.filter(
(element) => element !== this.#list[index]
);
}
/**
* remove all elements
*/
public removeAll(): void {
this.#list = [];
}
/**
* get the length of the array list
*/
public size(): number {
return this.#list.length;
}
/**
* get the value you want with index
*/
public get(index: number): T {
return this.#list[index];
}
/**
* get all values
*/
public getAll(): T[] {
return this.#list;
}
/**
* update the array list
*/
public set(index: number, value: T): void {
this.#list[index] = value;
}
/**
* check the array list is empty or not
*/
public isEmpty(): boolean {
return this.#list.length === 0;
}
/**
* check the existence of a value (isExist)
*/
public contains(value: T): boolean {
return this.#list.filter((element) => element === value).length !== 0;
}
/**
* get the first index of input value
*/
public indexOf(value: T): number {
return this.#list.indexOf(value);
}
/**
* get the last index of input value
*/
public lastIndexOf(value: T): number {
return this.#list.lastIndexOf(value);
}
/**
* get the number of all input value
*/
public nbrIndex(value: T): number {
let nb = 0;
this.#list.forEach((element) => {
if (element === value) {
nb++;
}
});
return nb;
}
/**
* basic ASC sort for the array list
*/
private sortASC(table_name: T[]): T[] {
return table_name.sort((a: T, b: T) => (a < b ? -1 : a > b ? 1 : 0));
}
/**
* sort the arraylist
*/
public sort(typeOfSort?: string): T[] {
const sortedList = this.sortASC(this.#list);
return typeOfSort?.toLowerCase() === "desc"
? sortedList.reverse()
: sortedList;
}
}
/**
* Expose `ArrayList`.
*/
export default ArrayList;
// For CommonJS default export support
module.exports = ArrayList;
module.exports.ArrayList = ArrayList;
module.exports.default = ArrayList;