Skip to content

Node module for converting lists of numbers (usually page numbers) from strings to arrays and vice versa

License

Notifications You must be signed in to change notification settings

kcivey/number-list

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

number-list

Description

Convert arrays of positive integers (such as page numbers) to comma-separated strings, with ranges represented with hyphens, or do the reverse, converting strings to arrays. Numbers are sorted in the output, and duplicates are removed. To avoid memory problems (and because this is designed for page numbers), the integers must be less than 1 million.

Installation

$ npm install --save number-list

Usage

const NumberList = require('number-list');

Methods

.parse(string)

The argument is a string made up of numbers and number ranges, separated by commas or whitespace.

console.log(NumberList.parse('1-3, 5'));
// [ 1, 2, 3, 5 ]

console.log(NumberList.parse('3-4 1-6 2,2'));
// [ 1, 2, 3, 4, 5, 6 ]

.stringify(array)

The argument is an array of positive integers.

console.log(NumberList.stringify([1, 2, 3, 5]));
// 1-3, 5

console.log(NumberList.stringify([2, 7, 3, 2, 6]));
// 2-3, 6-7

About

Node module for converting lists of numbers (usually page numbers) from strings to arrays and vice versa

Resources

License

Stars

Watchers

Forks

Packages

No packages published