jadjoubran / codetogo.io

🚀 JavaScript code to go - Find updated snippets for common JavaScript use cases
https://codetogo.io
MIT License
231 stars 30 forks source link

Use Case Suggestion: thousandSeperator #49

Closed theIYD closed 6 years ago

theIYD commented 6 years ago

function thousandSeperator: Seperate thousands using commas

const thousandSeperator = (num) => {
    let parts = num.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}

console.log(thousandSeperator(130000));
//=> 130,000

Reference: https://goo.gl/jKeiy8

Thanks @jadjoubran 😊

jadjoubran commented 6 years ago

Thanks @theIYD This is a bit big and has the same situation as #18 so I'll keep it open with the label later when we see how codetogo evolves

theIYD commented 6 years ago

Cool !

Does Code To Go need one liner solutions specifically as of now ?

jadjoubran commented 6 years ago

Not necessarily one liner solutions, for example How to fetch json in JavaScript and other use cases are more than one line, however they're tackling a single use case, rather than a ready made function.

The reason why I'm avoiding functions is because 30-seconds-of-code has most of these covered

theIYD commented 6 years ago

30-seconds-of-code has one liner solutions (most of them). Of course there is a noticeable difference between the two.

Will be posting more use cases. 😊👍

jadjoubran commented 6 years ago

yes indeed, the problem is if you take a look at this example:

const flattenDepth = (arr, depth = 1) =>
  depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
  : arr.reduce((a, v) => a.concat(v), []);

you can immediately understand what the function does but reading what's happening inside the function takes a lot more time

And awesome thanks 😄