Last active
June 20, 2021 19:08
-
-
Save taurgis/fd5331f883bcb1518026df2cb4779fc3 to your computer and use it in GitHub Desktop.
UnlimitedArray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A wrapper around a standard array supported by Salesforce Commerce Cloud B2C | |
* to circumvent the 20.000 items quota limitation. | |
* | |
* WARNING: As this circumvents the quota limitation, you are open to high | |
* resource usage and possibly out of memory exceptions. | |
*/ | |
function UnlimitedArray() { | |
const listContainer = new Array(); | |
listContainer.push(new Array()); | |
this.listContainer = listContainer; | |
this.currentListPosition = 0; | |
Object.defineProperty(this, 'length', { | |
get: function() { | |
var totalLength = 0; | |
this.listContainer.forEach(function(list) { | |
totalLength += list.length; | |
}); | |
return totalLength; | |
} | |
} ) | |
} | |
/** | |
* Appends elements to the Array. | |
* | |
* @param {Object} value - one or more values that will be appended to the Array. | |
* | |
* @returns {Number} - the new length of the Array. | |
*/ | |
UnlimitedArray.prototype.push = function(value) { | |
var currentList = this.listContainer[this.currentListPosition]; | |
if(currentList.length === 20000) { | |
currentList = new Array(); | |
this.currentListPosition += 1; | |
this.listContainer.push(currentList); | |
} | |
currentList.push(value); | |
return this.length; | |
} | |
/** | |
* Returns if the array contains a specific value. | |
* | |
* @param {Object} value - The value to look for. | |
* @param {Number} fromIndex - the index from which to start looking for the element. | |
* | |
* @returns {boolean} - true if the value is found in the array else false. | |
*/ | |
UnlimitedArray.prototype.includes = function(value, fromIndex) { | |
if(!fromIndex) { | |
fromIndex = 0; | |
} | |
var correctedIndex = 0; | |
for(var i = 0; i < this.listContainer.length; i++) { | |
var currentList = this.listContainer[i]; | |
var currentListLength = currentList.length; | |
if(fromIndex <= (correctedIndex + currentListLength)) { | |
if(currentList.includes(value, fromIndex - correctedIndex)) { | |
return true; | |
} | |
} | |
correctedIndex += currentListLength; | |
} | |
return false; | |
} | |
/** | |
* Returns the first index at which a given element can be found in the array starting | |
* at fromIndex, or -1 if it is not present. | |
* | |
* @param {Object} value - the element to locate in the Array. | |
* @param {Number} fromIndex - the index from which to start looking for the element. | |
* | |
* @returns {Number} - the index of the element or -1 if it is no preset. | |
*/ | |
UnlimitedArray.prototype.indexOf = function(value, fromIndex ) { | |
if(!fromIndex) { | |
fromIndex = 0; | |
} | |
var correctedIndex = 0; | |
for(var i = 0; i < this.listContainer.length; i++) { | |
var currentList = this.listContainer[i]; | |
var currentListLength = currentList.length; | |
if(fromIndex <= (correctedIndex + currentListLength)) { | |
if(currentList.indexOf(value, fromIndex - correctedIndex) >= 0) { | |
return correctedIndex + currentList.indexOf(value, fromIndex - correctedIndex); | |
} | |
} | |
correctedIndex += currentListLength; | |
} | |
return -1; | |
} | |
UnlimitedArray.prototype.get = function(position) { | |
var currentTotalIndex = 0; | |
var result = null; | |
for(var i = 0; i < this.listContainer.length; i++) { | |
var list = this.listContainer[i]; | |
if(!result) { | |
var previousTotalIndex = currentTotalIndex; | |
currentTotalIndex += list.length; | |
if(currentTotalIndex > position) { | |
result = list[position - previousTotalIndex]; | |
} | |
} | |
}; | |
return result; | |
} | |
var unlimitedArray = new UnlimitedArray(); | |
for(var i = 0; i < 120000; i++) { | |
unlimitedArray.push(i); | |
} | |
unlimitedArray.indexOf(39000); | |
unlimitedArray.includes(39000, 38000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment