Created
November 10, 2017 15:39
-
-
Save rzkhosroshahi/85d60ce5cc7e8779e35de7d2f276a61b to your computer and use it in GitHub Desktop.
Functional string spliter
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
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