Guest User

Untitled

a guest
Dec 9th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. const splitWith3 = val => {
  2. return val
  3. .split(/(\d{3})/g)
  4. .filter(Boolean)
  5. }
  6.  
  7. const split = num => {
  8. switch (num.length % 3) {
  9. case 2:
  10. return [
  11. num.substr(0, 2),
  12. ...splitWith3(num.replace(/^\d{2}/, ''))
  13. ]
  14. break
  15. case 1:
  16. return [
  17. num.substr(0, 1),
  18. ...splitWith3(num.replace(/^\d{1}/, ''))
  19. ]
  20. break
  21. case 0:
  22. return splitWith3(num)
  23. break
  24. }
  25. }
  26.  
  27. export default split
Add Comment
Please, Sign In to add comment