Last active
July 2, 2017 16:34
-
-
Save webknjaz/7ae5a55652d3b8e31a44f8eb87ccb85c to your computer and use it in GitHub Desktop.
Insert start/stop markers into a string
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
#! /usr/bin/env node | |
'use strict' | |
let inp_str = 'Hello, world!' | |
let inp_markers = [ [6, 10], [0, 2] ] | |
String.from_iterator = (str_iter) => ''.concat(...str_iter) | |
const mark_str_segments = (str, markers) => String.from_iterator(chunk_generator(str, markers)) | |
function* chunk_generator(str, markers) { | |
`Emit chunks for new string with marked regions. | |
str (String): source string | |
markers (Array): unordered list of start-end region markers pairs | |
` | |
const start_mark = '<M>' | |
const end_mark = '</M>' | |
const _markers = [...markers] | |
.sort((m1, m2) => m1[0] - m2[0]) // sort markers by start index | |
const str_len = str.length // cache the str length | |
let cur_ind = 0 | |
for (let m of _markers) { | |
let m_start = m[0] | |
let m_end = m[1] + 1 | |
yield cur_ind < m_start // emit raw chunk before the marked region | |
? str.substring(cur_ind, m_start) | |
: '' | |
if (m_start >= str_len) return // start marker is out of bound | |
yield start_mark | |
yield str.substring(m_start, m_end) // emit marked chunk | |
yield end_mark | |
if (m_end >= str_len) return // end marker is out of bound | |
cur_ind = m_end | |
} | |
yield cur_ind < str.length // emit the rest of input string, unmarked | |
? str.substring(cur_ind) | |
: '' | |
} | |
console.log(`IN: '${inp_str}'`) | |
console.log(mark_str_segments(inp_str, inp_markers)) | |
console.log("IN: ''") | |
console.log(mark_str_segments('', inp_markers)) | |
console.log("IN: 'hey'") | |
console.log(mark_str_segments('hey', inp_markers)) | |
console.log("IN: 'heyyaaaaaaaaaaaaaaaaaaaaaahh'") | |
console.log(mark_str_segments('heyyaaaaaaaaaaaaaaaaaaaaaahh', inp_markers)) | |
console.log("IN: 'heyyaa'") | |
console.log(mark_str_segments('heyyaa', inp_markers)) | |
console.log("IN: 'heyyaaaaaa'") | |
console.log(mark_str_segments('heyyaaaaaa', inp_markers)) | |
console.log("IN: 'heyyaaaaaaa'") | |
console.log(mark_str_segments('heyyaaaaaaa', inp_markers)) | |
console.log("IN: 'heyyaaaaaaaa'") | |
console.log(mark_str_segments('heyyaaaaaaaa', inp_markers)) | |
console.log("IN: 'heyyaaaaaaaaaa'") | |
console.log(mark_str_segments('heyyaaaaaaaaaa', inp_markers)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment