Created
January 13, 2017 18:40
-
-
Save jwerle/34532a4302ed90cd5867f042bc766cd3 to your computer and use it in GitHub Desktop.
ArrayPartitionFromPrefixMarkers
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
function ArrayPartitionFromPrefixMarkers(array, start, stop) { | |
const partition = [] | |
let inPartition = false | |
for (let i = 0; i < array.length; ++i) { | |
const element = array[i] | |
// test element for start prefix to determine | |
// if we should enter partition view | |
if (false == inPartition && start == element.substring(0, start.length)) { | |
inPartition = true | |
} | |
if (inPartition) { | |
partition.push(element) | |
} | |
if (true == inPartition && stop == element.substring(0, stop.length)) { | |
partition.push(element) | |
break | |
} | |
} | |
return partition | |
} | |
const value = () => Math.random().toString('16').slice(2).slice(0, 16) | |
const elements = [ | |
`2014-${value()}`, | |
`2014-${value()}`, | |
`2014-${value()}`, | |
`2014-${value()}`, | |
`2015-${value()}`, | |
`2015-${value()}`, | |
`2015-${value()}`, | |
`2016-${value()}`, | |
`2016-${value()}`, | |
`2016-${value()}`, | |
`2016-${value()}`, | |
`2017-${value()}`, | |
`2017-${value()}`, | |
`2017-${value()}`, | |
`2017-${value()}`, | |
`2018-${value()}`, | |
`2018-${value()}`, | |
] | |
const partition = ArrayPartitionFromPrefixMarkers(elements, '2015', '2018') | |
console.log(partition) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment