|
| 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 | +}; |
0 commit comments