Skip to content

Commit 3df9000

Browse files
committed
published package
1 parent 396f60c commit 3df9000

File tree

6 files changed

+209
-104
lines changed

6 files changed

+209
-104
lines changed

index.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
const { indexOf, atIndex } = require("./src/operators/accessing");
2+
const { removeWhere, findWhere } = require("./src/operators/fun");
3+
4+
module.exports.FunArray = class FunArray {
5+
#Array = [];
6+
7+
constructor() {
8+
const args = arguments;
9+
for (let i = 0; i < args.length; i++) {
10+
this.#Array.push(args[i]);
11+
}
12+
}
13+
14+
//Basic
15+
16+
print() {
17+
console.log(this.#Array);
18+
}
19+
20+
length() {
21+
return this.#Array.length;
22+
}
23+
24+
array() {
25+
return this.#Array;
26+
}
27+
28+
//Add or Remove items
29+
30+
push(value) {
31+
this.#Array.push(value);
32+
}
33+
34+
trash() {
35+
this.#Array.length = [];
36+
}
37+
38+
pop() {
39+
this.#Array.pop();
40+
}
41+
42+
shift() {
43+
this.#Array.shift();
44+
}
45+
46+
unshift() {
47+
for (let i = 0; i < arguments.length; i++) {
48+
this.#Array.unshift(arguments[i]);
49+
}
50+
}
51+
52+
toString() {
53+
this.#Array.toString();
54+
}
55+
56+
fill(value, start, end) {
57+
const arr = this.#Array;
58+
if (arguments.length === 1) return arr.fill(value);
59+
else if (arguments.length === 3) return arr.fill(value, start, end);
60+
}
61+
62+
//Accessing elelments
63+
64+
indexOf(value) {
65+
return Number(indexOf(this.#Array, value));
66+
}
67+
68+
atIndex(index) {
69+
if (typeof index === number) {
70+
return atIndex(this.#Array, index);
71+
}
72+
return new Error("Incorrect datatype for index");
73+
}
74+
75+
//Modifying Elements
76+
77+
removeAt(index) {
78+
if (typeof index === number) {
79+
this.#Array.splice(index, 1);
80+
} else {
81+
throw new Error("Incorrect datatype for index");
82+
}
83+
}
84+
85+
remove(value) {
86+
const index = this.#Array.indexOf(value);
87+
this.#Array.splice(index, 1);
88+
}
89+
90+
merge(obj) {
91+
this.#Array = [...this.#Array, ...obj.#Array];
92+
}
93+
94+
distinct() {
95+
return [...new Set(this.#Array)];
96+
}
97+
98+
slice(start, end) {
99+
let arr = [];
100+
arr = this.#Array.slice(start, end);
101+
return arr;
102+
}
103+
104+
//Object Array removing
105+
106+
removeWhere(expression) {
107+
removeWhere(this.#Array, expression);
108+
}
109+
110+
findWhere(expression) {
111+
return findWhere(this.#Array, expression);
112+
}
113+
114+
//Sorting
115+
116+
sortAsc(key) {
117+
if (arguments.length === 0) {
118+
return this.#Array.sort();
119+
} else if (arguments.length === 1) {
120+
return this.#Array.sort((a, b) => {
121+
return a[key] - b[key];
122+
});
123+
}
124+
}
125+
126+
sortDesc(key) {
127+
if (arguments.length === 0) {
128+
return this.#Array.sort((a, b) => {
129+
return b - a;
130+
});
131+
} else if (arguments.length === 1) {
132+
return this.#Array.sort((a, b) => {
133+
return b[key] - a[key];
134+
});
135+
}
136+
}
137+
138+
//Fun methods
139+
140+
replace(from, to, occurence) {
141+
let count = 0;
142+
if (arguments.length == 2) {
143+
for (let i = 0; i < this.#Array.length; i++) {
144+
if (this.#Array[i] === from) {
145+
this.#Array[i] = to;
146+
break;
147+
}
148+
}
149+
} else if (arguments.length == 3) {
150+
for (let i = 0; i < this.#Array.length; i++) {
151+
if (count === occurence) break;
152+
153+
if (this.#Array[i] === from) {
154+
this.#Array[i] = to;
155+
count++;
156+
}
157+
}
158+
} else {
159+
throw new Error("Many number of arguments");
160+
}
161+
}
162+
163+
replaceAll(from, to) {
164+
if (arguments.length == 2) {
165+
for (let i = 0; i < this.#Array.length; i++) {
166+
if (this.#Array[i] === from) {
167+
this.#Array[i] = to;
168+
}
169+
}
170+
} else {
171+
throw new Error("Many number of arguments");
172+
}
173+
}
174+
175+
funPush() {
176+
for (let i = 0; i < arguments.length; i++) {
177+
this.#Array.push(arguments[i]);
178+
}
179+
}
180+
181+
findAndUpdate(index, value) {
182+
this.#Array[index] = value;
183+
}
184+
};

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "@jitiendran/funarray",
33
"version": "1.0.0",
44
"description": "This is a npm package with fun and easy array operation",
5-
"main": "src/index.js",
5+
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"start": "node src/index.js"
8+
"start": "node index.js"
99
},
1010
"repository": {
1111
"type": "git",

src/index.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/operators/fun.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,21 @@ module.exports.removeWhere = (array, expression) => {
6363

6464
array.splice(index, 1);
6565
};
66+
67+
module.exports.findWhere = (array, expression) => {
68+
const exp = String(expression).trim();
69+
const operator = findOperator(exp);
70+
const arr = exp.split(operator);
71+
72+
const key = arr[0].trim();
73+
const value = arr[arr.length - 1];
74+
75+
let result = [];
76+
77+
for (let i = 0; i < array.length; i++) {
78+
if (performOperation(array[i][key], operator, value)) {
79+
result.push(array[i]);
80+
}
81+
}
82+
return result;
83+
};

src/test.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)