Skip to content

Add util.indexOf() function in common #831

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

Open
mtuchi opened this issue Nov 22, 2024 · 4 comments · May be fixed by #1225
Open

Add util.indexOf() function in common #831

mtuchi opened this issue Nov 22, 2024 · 4 comments · May be fixed by #1225
Assignees
Labels
Medium Complexity level P3 Priority Level

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

@josephjclark josephjclark added the Needs Priority This issue needs to be prioritised label Feb 12, 2025
@theroinaochieng theroinaochieng moved this from New Issues to DevX Backlog in v2 Feb 20, 2025
@josephjclark
Copy link
Collaborator

I think I am in favour of exporting _ from common (and all adaptors) and just linking to lodash docs.

There are other toolkits available, like ramda and lodash/fp.

I'd be interested in a little survey of modern utility libraries - is lodash still cool? - and a spike PR showing how this might look in code and docs (does _ count as a namespace? I don't think so, and the docsite only otherwise includes functions.... so we need to add special docs support for this?)

@martalovescoffee martalovescoffee added P3 Priority Level Medium Complexity level and removed Needs Priority This issue needs to be prioritised labels Apr 4, 2025
@hunterachieng hunterachieng linked a pull request Jun 5, 2025 that will close this issue
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Medium Complexity level P3 Priority Level
Projects
Status: DevX Backlog
Development

Successfully merging a pull request may close this issue.

4 participants