Q:

What is the name or description of my technique for filtering items in array?

I've tried posting this question on StackOverflow but they told me to try it here since it's not so specific. I'd like to know the name of the algorithm or technique of following implementation. I wonder if this thing present in standard libraries of other languages (my implementation is in Javascript).
Consider a function that takes collection/iterable and index which should return items that surround / are close to the index (including the item at the index). The number of returned items can be specified as well.
This is the output of the function:
const ary = [3, 4, 5, 7, 8, 20, 21, 23, 600, 120]

// Get item on index and four another items surrounding it
const result = getCollectionAround(ary, 5, 4)

// -> console.log(result)
[7, 8, 20, 21, 23]

The trick is to return items from this function even if the index is last / first one (which means getting item on last index and four before it). You should always be getting the correct amount of items back even if you're hitting the beginning/end of the array (this means that you have to make up for it by appending/prepending additional items).
This is the implementation
getCollectionAround(collection, referenceIndex, amount) {
    const totalCount = collection.length
    const requestedCount = Math.floor(totalCount * amount)

    let startIndex = Math.max(0, Math.round(referenceIndex - (requestedCount / 2)))
    if (startIndex + requestedCount > collection.length) {
      startIndex = Math.max(0, collection.length - requestedCount - 1)
    }

    // NOTE: + 1 include last index
    return collection.slice(startIndex, startIndex + 1 + requestedCount)
  }

Thanks for any leads. The reason for asking is because I might be using this a lot and I'd rather use some lightweight JS library (or at least module from it) that would do this for me or maybe create my own package for it if there is none.

A:

I think what you are referring to is "proximity search".
This is a subjective matter but as you have written the process with your rules when 5 items are returned you try to keep the index in middle. You may have to look for total elements in collection and or number of neighbors it has.
Someone can say that when they called this method GetCollectionAround for first index they would expect only 3 output 3,4,5 and you will return 3,4,5,7,8. In your example you have given 4 as amount which as I understand means the amount to return plus the indexed element. From the function call it looks like your return element would be a collection of 4.
One basic thing is that if you can fetch an element from collection you can apply a check/rule on it to find if it is needed to return on not and that check can be a complex monte carlo simulation comparison or a simple index check. Implementation of this check in your case is that its index is n more or less than the actual element index.
One assumption that you have is that index of this array represent something to fit the definition of "around" for example these are height of trees in a forest or number of people killed in accidents or wind speed on certain days. In each case you assume that index represents some sort of sequence or time line and when we call getCollectionAround(x,y,z) you will get a collection or empty set back that fits the critera of "around".
You can call this index based proximity search

