Giter VIP home page Giter VIP logo

expand-range's Introduction

expand-range NPM version NPM monthly downloads NPM total downloads Linux Build Status

Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used by micromatch.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save expand-range

Example usage

var expand = require('expand-range');
expand('start..end..step', options);

// examples
console.log(expand('1..3')) //=> ['1', '2', '3']
console.log(expand('1..10..3')) //=> [ '1', '4', '7', '10' ]

Params

  • start: the number or letter to start with
  • end: the number or letter to end with
  • step: (optional) the step/increment to use. works with letters and numbers.
  • options: Options object to pass to fill-range, or a transform function (see fill-range readme for details and documentation)

This library wraps fill-range to support range expansion using .. separated strings. See fill-range for the full list of options and features.

Examples

expand('a..e')
//=> ['a', 'b', 'c', 'd', 'e']

expand('a..e..2')
//=> ['a', 'c', 'e']

expand('A..E..2')
//=> ['A', 'C', 'E']

expand('1..3')
//=> ['1', '2', '3']

expand('0..-5')
//=> [ '0', '-1', '-2', '-3', '-4', '-5' ]

expand('-9..9..3')
//=> [ '-9', '-6', '-3', '0', '3', '6', '9' ])

expand('-1..-10..-2')
//=> [ '-1', '-3', '-5', '-7', '-9' ]

expand('1..10..2')
//=> [ '1', '3', '5', '7', '9' ]

Custom function

Optionally pass a custom function as the second argument:

expand('a..e', function (val, isNumber, pad, i) {
  if (!isNumber) {
    return String.fromCharCode(val) + i;
  }
  return val;
});
//=> ['a0', 'b1', 'c2', 'd3', 'e4']

Benchmarks

[object Object]

History

v2.0.0

Changes

  • Special step characters are no longer supported, as the same thing can be accomplished with a custom transform function.
  • The signature in the transform function has changed. See fill-range for more details.

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

  • braces: Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support… more | homepage
  • fill-range: Fill in a range of numbers or letters, optionally passing an increment or step to… more | homepage
  • micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | homepage

Contributors

Commits Contributor
65 jonschlinkert
1 dcohenb
1 stevelacy

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on November 26, 2018.

expand-range's People

Contributors

dcohenb avatar jonschlinkert avatar stephenlacy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

expand-range's Issues

Tests fail

The tests on this package are failing for me, with Node version 6.10.2.

$ npm run test

> [email protected] test /home/jsmith/Git/nodejs-expand-range/package
> mocha



  expand range
    ✓ should return the number as an array if no range is specified
    ✓ should throw when the first arg is not a string.
    ✓ should expand numerical ranges
    ✓ should expand negative ranges
    ✓ should expand alphabetical ranges
    ✓ should fill in numerical ranges
    ✓ should fill in numerical ranges when numbers are passed as strings
    ✓ should fill in negative ranges
    ✓ should fill in rangines using the given increment
    ✓ should fill in negative ranges using the given increment
    ✓ should fill in negative ranges using the given increment
    ✓ should fill in alphabetical ranges
    ✓ should use increments with alphabetical ranges

  special characters:
    1) should repeat the first arg `n` times:
    2) should collapse values when `>` is passed:
    3) should randomize using the first two args when `?` is passed:
    4) should join the array using `|` as the separator:

  character classes:
    5) should return a string for a regex range when `true` is passed:
    6) should return a string for a regex when `~` or `|` are passed:
    7) should make a logical or when the range is out-of-order:
    8) should prefix the regex string when a `prefix` is passed on options:
    ✓ should return the string when arguments are invalid:
    9) should return null for invalid patterns:

  when a custom function is used for expansions
    ✓ should expose the current value as the first param.
    ✓ should expose the `isNumber` boolean as the third param.
    ✓ should expose any padding as the third param.
    10) should expose the index as the third param.


  17 passing (28ms)
  10 failing

  1) special characters: should repeat the first arg `n` times::

      AssertionError: [] deepEqual [ 'a', 'a', 'a' ]
      + expected - actual

      +[
      +  "a"
      +  "a"
      +  "a"
      +]
      -[]
      
      at Context.<anonymous> (test.js:108:12)

  2) special characters: should collapse values when `>` is passed::

      AssertionError: [] deepEqual [ 'abcde' ]
      + expected - actual

      +[
      +  "abcde"
      +]
      -[]
      
      at Context.<anonymous> (test.js:114:12)

  3) special characters: should randomize using the first two args when `?` is passed::

      AssertionError: false == true
      + expected - actual

      +true
      -false
      
      at Context.<anonymous> (test.js:126:5)

  4) special characters: should join the array using `|` as the separator::

      AssertionError: [] deepEqual [ '[a-c]' ]
      + expected - actual

      +[
      +  "[a-c]"
      +]
      -[]
      
      at Context.<anonymous> (test.js:132:12)

  5) character classes: should return a string for a regex range when `true` is passed::
     AssertionError: '[a-e]' deepEqual [ '[a-e]' ]
      at Context.<anonymous> (test.js:140:12)

  6) character classes: should return a string for a regex when `~` or `|` are passed::

      AssertionError: [] deepEqual [ '(A|F|K|P)' ]
      + expected - actual

      +[
      +  "(A|F|K|P)"
      +]
      -[]
      
      at Context.<anonymous> (test.js:147:12)

  7) character classes: should make a logical or when the range is out-of-order::

      AssertionError: [] deepEqual [ '(c|b|a)' ]
      + expected - actual

      +[
      +  "(c|b|a)"
      +]
      -[]
      
      at Context.<anonymous> (test.js:151:12)

  8) character classes: should prefix the regex string when a `prefix` is passed on options::

      AssertionError: [] deepEqual [ '(?!c|b|a)' ]
      + expected - actual

      +[
      +  "(?!c|b|a)"
      +]
      -[]
      
      at Context.<anonymous> (test.js:158:12)

  9) character classes: should return null for invalid patterns::
     AssertionError: [] == null
      at Context.<anonymous> (test.js:167:12)

  10) when a custom function is used for expansions should expose the index as the third param.:

      AssertionError: [ 'a', 'b', 'c', 'd', 'e' ] deepEqual [ 'a0', 'b1', 'c2', 'd3', 'e4' ]
      + expected - actual

       [
      +  "a0"
      +  "b1"
      +  "c2"
      +  "d3"
      +  "e4"
      -  "a"
      -  "b"
      -  "c"
      -  "d"
      -  "e"
       ]
      
      at Context.<anonymous> (test.js:216:12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.