@guanghechen/parse-lineno
Parse something like '1,3-10' to numbers or intervals.
Install
-
npm
npm install --save-dev @guanghechen/parse-lineno
-
yarn
yarn add --dev @guanghechen/parse-lineno
Usage
-
Parse lineno string into numbers.
import { collectNumbers } from '@guanghechen/parse-lineno' collectNumbers('') // => [] collectNumbers('1') // => [1] collectNumbers('1-3') // => [1, 2, 3] collectNumbers('3,1-2,2,2') // => [1, 2, 3] collectNumbers('3,7-5,2,2') // => [2, 3, 5, 6, 7] collectNumbers('2,1-3') // => [1, 2, 3] collectNumbers('4,1-3') // => [1, 2, 3, 4] collectNumbers('2-4,1-3,5-9') // => [1, 2, 3, 4, 5, 6, 7, 8, 9] collectNumbers('2-4,1-3,6-9') // => [1, 2, 3, 4, 6, 7, 8, 9]
-
Parse lineno string into intervals.
import { collectIntervals } from '@guanghechen/parse-lineno' collectIntervals('') // => [] collectIntervals('1') // => [[1, 1]] collectIntervals('1-3') // => [[1, 3]] collectIntervals('3,1-2,2,2') // => [[1, 3]] collectIntervals('3,7-5,2,2') // => [[2, 3], [5, 7]] collectIntervals('2,1-3') // => [[1, 3]] collectIntervals('4,1-3') // => [[1, 4]] collectIntervals('2-4,1-3,5-9') // => [[1, 9]] collectIntervals('2-4,1-3,6-9') // => [[1, 4], [6, 9]]
-
Custom Separator (default is
/[,\s]+/
)collectNumbers('2#4-5#5-8', /#/) // => [2, 4, 5, 6, 7, 8] collectIntervals('2#4-5#5-8', /#/) // => [[2, 2], [4, 8]]