[d24f17c] | 1 | const negate = require('lodash/negate');
|
---|
| 2 |
|
---|
| 3 | // Coerces an a parameter into a callback for matching elements.
|
---|
| 4 | // This accepts an element name, an element type and returns a
|
---|
| 5 | // callback to match for those elements.
|
---|
| 6 | function coerceElementMatchingCallback(value) {
|
---|
| 7 | // Element Name
|
---|
| 8 | if (typeof value === 'string') {
|
---|
| 9 | return element => element.element === value;
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | // Element Type
|
---|
| 13 | if (value.constructor && value.extend) {
|
---|
| 14 | return element => element instanceof value;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | return value;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * @class
|
---|
| 22 | *
|
---|
| 23 | * @param {Element[]} elements
|
---|
| 24 | *
|
---|
| 25 | * @property {Element[]} elements
|
---|
| 26 | */
|
---|
| 27 | class ArraySlice {
|
---|
| 28 | constructor(elements) {
|
---|
| 29 | this.elements = elements || [];
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * @returns {Array}
|
---|
| 34 | */
|
---|
| 35 | toValue() {
|
---|
| 36 | return this.elements.map(element => element.toValue());
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | // High Order Functions
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @param callback - Function to execute for each element
|
---|
| 43 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 44 | * @returns {array} A new array with each element being the result of the callback function
|
---|
| 45 | */
|
---|
| 46 | map(callback, thisArg) {
|
---|
| 47 | return this.elements.map(callback, thisArg);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Maps and then flattens the results.
|
---|
| 52 | * @param callback - Function to execute for each element.
|
---|
| 53 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 54 | * @returns {array}
|
---|
| 55 | */
|
---|
| 56 | flatMap(callback, thisArg) {
|
---|
| 57 | return this
|
---|
| 58 | .map(callback, thisArg)
|
---|
| 59 | .reduce((a, b) => a.concat(b), []);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Returns an array containing the truthy results of calling the given transformation with each element of this sequence
|
---|
| 64 | * @param transform - A closure that accepts an element of this array as its argument and returns an optional value.
|
---|
| 65 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 66 | * @memberof ArraySlice.prototype
|
---|
| 67 | * @returns An array of the non-undefined results of calling transform with each element of the array
|
---|
| 68 | */
|
---|
| 69 | compactMap(transform, thisArg) {
|
---|
| 70 | const results = [];
|
---|
| 71 |
|
---|
| 72 | this.forEach((element) => {
|
---|
| 73 | const result = transform.bind(thisArg)(element);
|
---|
| 74 |
|
---|
| 75 | if (result) {
|
---|
| 76 | results.push(result);
|
---|
| 77 | }
|
---|
| 78 | });
|
---|
| 79 |
|
---|
| 80 | return results;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * @param callback - Function to execute for each element. This may be a callback, an element name or an element class.
|
---|
| 85 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 86 | * @returns {ArraySlice}
|
---|
| 87 | * @memberof ArraySlice.prototype
|
---|
| 88 | */
|
---|
| 89 | filter(callback, thisArg) {
|
---|
| 90 | callback = coerceElementMatchingCallback(callback);
|
---|
| 91 | return new ArraySlice(this.elements.filter(callback, thisArg));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * @param callback - Function to execute for each element. This may be a callback, an element name or an element class.
|
---|
| 96 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 97 | * @returns {ArraySlice}
|
---|
| 98 | * @memberof ArraySlice.prototype
|
---|
| 99 | */
|
---|
| 100 | reject(callback, thisArg) {
|
---|
| 101 | callback = coerceElementMatchingCallback(callback);
|
---|
| 102 | return new ArraySlice(this.elements.filter(negate(callback), thisArg));
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Returns the first element in the array that satisfies the given value
|
---|
| 107 | * @param callback - Function to execute for each element. This may be a callback, an element name or an element class.
|
---|
| 108 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 109 | * @returns {Element}
|
---|
| 110 | * @memberof ArraySlice.prototype
|
---|
| 111 | */
|
---|
| 112 | find(callback, thisArg) {
|
---|
| 113 | callback = coerceElementMatchingCallback(callback);
|
---|
| 114 | return this.elements.find(callback, thisArg);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * @param callback - Function to execute for each element
|
---|
| 119 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 120 | * @memberof ArraySlice.prototype
|
---|
| 121 | */
|
---|
| 122 | forEach(callback, thisArg) {
|
---|
| 123 | this.elements.forEach(callback, thisArg);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * @param callback - Function to execute for each element
|
---|
| 128 | * @param initialValue
|
---|
| 129 | * @memberof ArraySlice.prototype
|
---|
| 130 | */
|
---|
| 131 | reduce(callback, initialValue) {
|
---|
| 132 | return this.elements.reduce(callback, initialValue);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * @param value
|
---|
| 137 | * @returns {boolean}
|
---|
| 138 | * @memberof ArraySlice.prototype
|
---|
| 139 | */
|
---|
| 140 | includes(value) {
|
---|
| 141 | return this.elements.some(element => element.equals(value));
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | // Mutation
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Removes the first element from the slice
|
---|
| 148 | * @returns {Element} The removed element or undefined if the slice is empty
|
---|
| 149 | * @memberof ArraySlice.prototype
|
---|
| 150 | */
|
---|
| 151 | shift() {
|
---|
| 152 | return this.elements.shift();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * Adds the given element to the begining of the slice
|
---|
| 157 | * @parameter {Element} value
|
---|
| 158 | * @memberof ArraySlice.prototype
|
---|
| 159 | */
|
---|
| 160 | unshift(value) {
|
---|
| 161 | this.elements.unshift(this.refract(value));
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * Adds the given element to the end of the slice
|
---|
| 166 | * @parameter {Element} value
|
---|
| 167 | * @memberof ArraySlice.prototype
|
---|
| 168 | */
|
---|
| 169 | push(value) {
|
---|
| 170 | this.elements.push(this.refract(value));
|
---|
| 171 | return this;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * @parameter {Element} value
|
---|
| 176 | * @memberof ArraySlice.prototype
|
---|
| 177 | */
|
---|
| 178 | add(value) {
|
---|
| 179 | this.push(value);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | // Accessors
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * @parameter {number} index
|
---|
| 186 | * @returns {Element}
|
---|
| 187 | * @memberof ArraySlice.prototype
|
---|
| 188 | */
|
---|
| 189 | get(index) {
|
---|
| 190 | return this.elements[index];
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * @parameter {number} index
|
---|
| 195 | * @memberof ArraySlice.prototype
|
---|
| 196 | */
|
---|
| 197 | getValue(index) {
|
---|
| 198 | const element = this.elements[index];
|
---|
| 199 |
|
---|
| 200 | if (element) {
|
---|
| 201 | return element.toValue();
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | return undefined;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * Returns the number of elements in the slice
|
---|
| 209 | * @type number
|
---|
| 210 | */
|
---|
| 211 | get length() {
|
---|
| 212 | return this.elements.length;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Returns whether the slice is empty
|
---|
| 217 | * @type boolean
|
---|
| 218 | */
|
---|
| 219 | get isEmpty() {
|
---|
| 220 | return this.elements.length === 0;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | * Returns the first element in the slice or undefined if the slice is empty
|
---|
| 225 | * @type Element
|
---|
| 226 | */
|
---|
| 227 | get first() {
|
---|
| 228 | return this.elements[0];
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (typeof Symbol !== 'undefined') {
|
---|
| 233 | ArraySlice.prototype[Symbol.iterator] = function symbol() {
|
---|
| 234 | return this.elements[Symbol.iterator]();
|
---|
| 235 | };
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | module.exports = ArraySlice;
|
---|