Last active
July 31, 2017 18:36
-
-
Save kunle/367963a18da725343162fe5fd0a95011 to your computer and use it in GitHub Desktop.
Separates given string into groups separated by the separator provided
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const separate = (value: string, groupCount: number, separator = ' ') => { | |
if ((value === undefined || value === null) || (groupCount === null || groupCount === undefined)) { | |
throw new Error('value and groupCount are required'); | |
} | |
let reuslt = ''; | |
value.split('').forEach((c, i) => { | |
const currentPos = i + 1; | |
let isSeparatable = currentPos % groupCount === 0 && currentPos !== value.length; | |
if (isSeparatable) { | |
reuslt = `${reuslt}${c}${separator}`; | |
} else { | |
reuslt = `${reuslt}${c}`; | |
} | |
}); | |
return reuslt; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment