Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util.indexOf() function in common #831

Open
mtuchi opened this issue Nov 22, 2024 · 3 comments
Open

Add util.indexOf() function in common #831

mtuchi opened this issue Nov 22, 2024 · 3 comments

Comments

@mtuchi
Copy link
Collaborator

mtuchi commented Nov 22, 2024

Description
We are using lodash in some of our adaptors and most often we use one two function from the entire library.

For example in DHIS2 adaptor we only use indexOf function from lodash. I think we can use lodash implementation of such function and turn it into a util function. For example indexOf

 /**
   * Gets the index at which the first occurrence of `value` is found in `array`
   * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
   * for equality comparisons. If `fromIndex` is negative, it's used as the
   * offset from the end of `array`.
   *
   * @param {Array} array The array to inspect.
   * @param {*} value The value to search for.
   * @param {number} [fromIndex=0] The index to search from.
   * @returns {number} Returns the index of the matched value, else `-1`.
   * @example
   *
   * util.indexOf([1, 2, 1, 2], 2);
   * // => 1
   *
   * // Search from the `fromIndex`.
   * util.indexOf([1, 2, 1, 2], 2, 2);
   * // => 3
   */
  function indexOf(array, value, fromIndex) {
    var length = array == null ? 0 : array.length;
    if (typeof fromIndex == 'number') {
      fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
    } else {
      fromIndex = 0;
    }
    var index = (fromIndex || 0) - 1,
        isReflexive = value === value;

    while (++index < length) {
      var other = array[index];
      if ((isReflexive ? other === value : other !== other)) {
        return index;
      }
    }
    return -1;
  }
@github-project-automation github-project-automation bot moved this to New Issues in v2 Nov 22, 2024
@josephjclark
Copy link
Collaborator

Wouldn't we be better off just re-exporting _ from common? So that all adaptors have access to lodash directly?

I think this is an idea that's been around for a long time - we just haven't gotten around to it. Actually until fairly recently I don't think we've had the technology to do it

@mtuchi
Copy link
Collaborator Author

mtuchi commented Nov 26, 2024

Yes and no, i think lodash has lots of functions exporting all of them might not be a good idea in a long run, and i am also worried about docs as well

@josephjclark
Copy link
Collaborator

We wouldn't document it though, we'd just link to lodash docs and expose the _ variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New Issues
Development

No branches or pull requests

2 participants