Skip to content

Instantly share code, notes, and snippets.

@guillaumegarcia13
Last active September 2, 2019 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaumegarcia13/21e050f40f55d2dec80f5f940f4254ed to your computer and use it in GitHub Desktop.
Save guillaumegarcia13/21e050f40f55d2dec80f5f940f4254ed to your computer and use it in GitHub Desktop.
Split long string into concatenated assignment (eg. base64 images)
const CHUNK_SIZE = 80; // put whatever size is relevant here
const CONCAT_OPERATOR = '+'; // put string concatenation operator here
const text = `Very long text would like to be splitted to fit any clumsy editor at there that does only support fixed number of characters per line`;
const regexp = new RegExp('.{1,'+ CHUNK_SIZE+'}', 'g');
const result = '`' + text.match(regexp).join('` ' + CONCAT_OPERATOR + ' \n`') + '`';
// Result with (CHUNK_SIZE=40 and CONCAT_OPERATOR='&&') is:
//
// `Very long text would like to be splitted` &&
// ` to fit any clumsy editor at there that ` &&
// `does only support fixed number of charac` &&
// `ters per line`
// Pre-defined scenarios
'`' + text.match(/.{1,80}/g).join('` &&\n`') + '`'; // ABAP Version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment