Skip to content

Commit f953686

Browse files
committed
Updated toString function and readme
1 parent 3df9000 commit f953686

File tree

4 files changed

+377
-24
lines changed

4 files changed

+377
-24
lines changed

README.md

Lines changed: 361 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,362 @@
11
# funarray-package
2-
This is npm package for arrays
2+
3+
This is the light weight array package with fun array manipulations and methods.
4+
5+
## Get Started
6+
7+
```
8+
npm install --save @jitiendran/funarray
9+
```
10+
11+
Or if you're not into package management, just [download a ZIP](https://github.com/jitiendran/funarray-package/archive/master.zip) file.
12+
13+
## Setup
14+
15+
After installing the package first the require the package from node modules into your javasctript file
16+
17+
```js
18+
const { FunArray } = require("@jitiendran/funarray");
19+
```
20+
21+
> Now, you can create a empty array by creating a object for the class FunArray.
22+
23+
```js
24+
let array = new FunArray();
25+
```
26+
27+
> You can also add element to array array while creating itself by doing the below.
28+
29+
```js
30+
let array = new FunArray(1, 2, 3, 4);
31+
```
32+
33+
## Common Operations
34+
35+
### 1. Create an Array
36+
37+
```js
38+
let fruits = new FunArray("Apple", "Bannana", "Orange");
39+
```
40+
41+
<div align=center>Or</div>
42+
43+
```js
44+
let fruits = new FunArray();
45+
46+
fruits.push("Apple");
47+
fruits.push("Bannana");
48+
```
49+
50+
### 2. Access an Array elements
51+
52+
```js
53+
let fruit = fruits.atIndex(0); //returns value
54+
```
55+
56+
<div align=center>Or</div>
57+
58+
```js
59+
const index = fruits.indexOf('Apple') //returns index
60+
61+
let fruit = fruits[index].
62+
```
63+
64+
### 3. Looping through array
65+
66+
```js
67+
for (let i = 0; i < fruits.length(); i++) {
68+
console.log(fruits.atIndex(i));
69+
}
70+
```
71+
72+
Output
73+
74+
```
75+
Apple
76+
Bannana
77+
Orange
78+
```
79+
80+
### 4. Removing an item from last
81+
82+
```js
83+
fruits.pop(); //removes last item
84+
85+
console.log(fruits.array()); //converts funarray to array
86+
```
87+
88+
Output
89+
90+
```
91+
[ 'Apple', 'Bannana' ]
92+
```
93+
94+
### 5. Remove an item from the beginning of an Array
95+
96+
```js
97+
fruits.shift(); //removes Apple from front
98+
```
99+
100+
### 6. Add an item to the beginning of an Array
101+
102+
```js
103+
fruits.unshift("Strawberry");
104+
105+
//output: ['Strawberry','Bannana','Orange']
106+
```
107+
108+
<div align="center">Or</div>
109+
110+
```js
111+
fruits.unshift('Strawberry','PineApple',...)
112+
```
113+
114+
### 7. Remove an item by index position
115+
116+
```js
117+
fruits.removeAt(0); //removes the elements at index 0
118+
```
119+
120+
### 8. Remove an item by value
121+
122+
```js
123+
fruits.remove("Apple"); //removes apple
124+
```
125+
126+
### 9. Copy an array
127+
128+
```js
129+
let copy = fruits.slice();
130+
```
131+
132+
### 10. Print fun array
133+
134+
```js
135+
fruits.print(); //prints the array
136+
```
137+
138+
### 11. Convert Array to String
139+
140+
```js
141+
const str = fruits.toString();
142+
```
143+
144+
### 12. Empty the whole array
145+
146+
```js
147+
fruits.trash();
148+
```
149+
150+
### 13. Fill the array
151+
152+
```js
153+
fruits.fill("Avacado"); //fills everything with Avacado
154+
```
155+
156+
<div align='center'>Or</div>
157+
158+
```js
159+
fruits.fill("Avacado", 0, 1); //fills Avacado starting from index 0 upto 1
160+
```
161+
162+
### 14. Merge with another array
163+
164+
```js
165+
let vegetables = new FunArray("Tomato", "Onion", "Brinjal");
166+
167+
fruits.merge(vegetables);
168+
169+
fruits.print();
170+
```
171+
172+
Output
173+
174+
```
175+
[ 'Apple', 'Bannana', 'Orange', 'Vegetables', 'Onion', 'Brinjal' ]
176+
```
177+
178+
### 15. Make the Array distinct
179+
180+
```js
181+
let num = new FunArray(1, 2, 3, 3).distinct(); //returns normal array
182+
183+
console.log(num);
184+
//output [1,2,3]
185+
```
186+
187+
### 16. Remove the element from Object array using expression
188+
189+
```js
190+
let users = new FunArray(
191+
{
192+
_id: 1,
193+
Username: "Albert",
194+
},
195+
{ _id: 2, Username: "Jason" }
196+
);
197+
198+
users.removeWhere("_id === 1"); //expression should be string
199+
console.log(users.array());
200+
```
201+
202+
Output
203+
204+
```
205+
[ { _id: 2, Username: 'Jason' } ]
206+
```
207+
208+
### 17. Find the elements from Object array using expression
209+
210+
```js
211+
let users = new FunArray(
212+
{
213+
_id: 1,
214+
Username: "Albert",
215+
},
216+
{ _id: 2, Username: "Albert" },
217+
{ _id: 3, Username: "Jason" }
218+
);
219+
220+
let foundUsers = users.findWhere("Username === Albert"); //returns normal array
221+
222+
console.log(foundUsers);
223+
```
224+
225+
Output
226+
227+
```
228+
[ { _id: 1, Username: 'Albert' }, { _id: 2, Username: 'Albert' } ]
229+
```
230+
231+
### 18. Sort the Array in ascending Order
232+
233+
```js
234+
const num = new FunArray(4, 2, 3, 3);
235+
236+
let users = new FunArray(
237+
{
238+
_id: 2,
239+
Username: "Albert",
240+
},
241+
{ _id: 1, Username: "Albert" },
242+
{ _id: 3, Username: "Jason" }
243+
);
244+
245+
num.sortAsc();
246+
247+
users.sortAsc("_id"); //sort by any key;
248+
249+
num.print();
250+
users.print();
251+
```
252+
253+
Output
254+
255+
```
256+
[ 2, 3, 3, 4 ]
257+
[
258+
{ _id: 1, Username: 'Albert' },
259+
{ _id: 2, Username: 'Albert' },
260+
{ _id: 3, Username: 'Jason' }
261+
]
262+
```
263+
264+
### 19. Sort the Array in ascending Order
265+
266+
```js
267+
const num = new FunArray(4, 2, 3, 3);
268+
269+
let users = new FunArray(
270+
{
271+
_id: 2,
272+
Username: "Albert",
273+
},
274+
{ _id: 1, Username: "Albert" },
275+
{ _id: 3, Username: "Jason" }
276+
);
277+
278+
num.sortDesc();
279+
280+
users.sortDesc("_id"); //sort by any key;
281+
282+
num.print();
283+
users.print();
284+
```
285+
286+
Output
287+
288+
```
289+
[ 4, 3, 3, 2 ]
290+
[
291+
{ _id: 3, Username: 'Jason' },
292+
{ _id: 2, Username: 'Albert' },
293+
{ _id: 1, Username: 'Albert' }
294+
]
295+
```
296+
297+
### 20. Replace a item in the array
298+
299+
```js
300+
const names = new FunArray("Albert", "Albert", "Jason");
301+
302+
names.replace("Albert", "Happy"); // By default occurence is 1
303+
304+
console.log(names.array());
305+
306+
names.replace("Albert", "Happy", 2); //specifying occurence
307+
308+
console.log(names.array());
309+
```
310+
311+
Output
312+
313+
```
314+
[ 'Happy', 'Albert', 'Jason' ]
315+
[ 'Happy', 'Happy', 'Jason' ]
316+
```
317+
318+
### 21. Replace All elements in the array
319+
320+
```js
321+
const names = new FunArray("Albert", "Albert", "Jason");
322+
323+
names.replaceAll("Albert", "Happy");
324+
```
325+
326+
Output
327+
328+
```
329+
[ 'Happy', 'Happy', 'Jason' ]
330+
```
331+
332+
### 22. Multiple push similar to creation
333+
334+
```js
335+
const names = new FunArray("Albert", "Albert", "Jason");
336+
337+
names.funPush("James", "Elric", "Alphonse");
338+
339+
names.print();
340+
```
341+
342+
Output
343+
344+
```
345+
[ 'Albert', 'Albert', 'Jason', 'James', 'Elric', 'Alphonse' ]
346+
```
347+
348+
### 23. Find and Update the elements in array
349+
350+
```js
351+
const names = new FunArray("Albert", "Albert", "Jason");
352+
353+
names.findAndUpdate(0, "Alphonse");
354+
355+
names.print();
356+
```
357+
358+
Ouput
359+
360+
```
361+
[ 'Alphonse', 'Albert', 'Jason' ]
362+
```

0 commit comments

Comments
 (0)