Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. const LimitedArray = function(limit) {
  2. const storage = [];
  3. const limitedArray = {};
  4.  
  5. limitedArray.get = function (index) {
  6. checkLimit(index)
  7. return storage[index]
  8. };
  9.  
  10. limitedArray.set = function (index, value) {
  11. checkLimit(index)
  12. storage[index] = value
  13. };
  14.  
  15. limitedArray.each = function (callback) {
  16. for (let i = 0; i < storage.length; i++) {
  17. callback(storage[i], i, storage)
  18. }
  19. };
  20.  
  21. const checkLimit = function (index) {
  22. if (typeof index !== 'number') {
  23. throw new Error('setter requires a numeric index for its first argument')
  24. };
  25. if (limit <= index) {
  26. throw new Error('Error trying to access an over-the-limit index')
  27. };
  28. }
  29.  
  30. return limitedArray
  31. }
  32.  
  33. const getIndexBelowMaxForKey = function (str, max) {
  34. let hash = 0
  35. for (let i = 0; i < str.length; i++) {
  36. hash = (hash << 5) + hash + str.charCodeAt(i)
  37. hash = hash & hash // Convert to 32bit integer
  38. hash = Math.abs(hash)
  39. }
  40. return hash % max
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement