r/javascript • • Aug 08 '16

A collection of most commonly used functions in modern webapps

https://www.npmjs.com/package/common-lib
0 Upvotes

6 comments sorted by

6

u/prawnsalad Aug 08 '16
exports.getFirstLetter = function(s) {
    return s.toUpperCase().charAt(0);
}

An entire function for this? Does mystring[0] not work well enough? Even if you did prefer charAt over indexed access, mystrings.charAt(0) doesn't need a wrapper.

3

u/JaxoDI Aug 08 '16

Not to mention how inefficient it is to uppercase the entire string, then only take the first letter...

5

u/sbruchmann Aug 08 '16
module.exports.getElementIndex = function(id, array) {
    for (var i = 0; i < array.length; i++) {
        if (array[i].id == id) {
      return i;
    }
  }
  return -1;
}

ugh…

const getElementIndexById = (els, id) => els.findIndex(el => el.id === id)

2

u/tbranyen Aug 08 '16

TIL Array#findIndex good stuff!

2

u/guywithalamename Aug 08 '16 edited Aug 08 '16

Half of this is already backed into the language.. I mean getElementIndex? seriously?

edit: Turns out the function does something completely different than the docs say it does..

-3

u/Scripter17 Aug 08 '16

What even is "npm install"?