Skip to content

Instantly share code, notes, and snippets.

@kunle
Last active July 31, 2017 18:36
Show Gist options
  • Save kunle/367963a18da725343162fe5fd0a95011 to your computer and use it in GitHub Desktop.
Save kunle/367963a18da725343162fe5fd0a95011 to your computer and use it in GitHub Desktop.
Separates given string into groups separated by the separator provided
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