Skip to content

Instantly share code, notes, and snippets.

@rzkhosroshahi
Created November 10, 2017 15:39
Show Gist options
  • Save rzkhosroshahi/85d60ce5cc7e8779e35de7d2f276a61b to your computer and use it in GitHub Desktop.
Save rzkhosroshahi/85d60ce5cc7e8779e35de7d2f276a61b to your computer and use it in GitHub Desktop.
Functional string spliter
const curry = require('lodash/curry');
const spliter = curry((separateTo,sign,string) => {
string = string.split('');
return string.reduce((comb,current,i) =>
((i+1) % separateTo === 0 && (i + 1) !== string.length)
? comb + current + sign
: comb + current
)});
let cardNumber = "6037224422442244";
const fourSpliter = spliter(4);
console.log(fourSpliter(":)",cardNumber)); // 6037:)2244:)2244:)2244
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment