Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # to-regex-range [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/to-regex-range.svg?style=flat)](https://www.npmjs.com/package/to-regex-range) [![NPM monthly downloads](https://img.shields.io/npm/dm/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![NPM total downloads](https://img.shields.io/npm/dt/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![Linux Build Status](https://img.shields.io/travis/micromatch/to-regex-range.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/to-regex-range)
  2. > Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save to-regex-range
  8. ```
  9. <details>
  10. <summary><strong>What does this do?</strong></summary>
  11. <br>
  12. This libary generates the `source` string to be passed to `new RegExp()` for matching a range of numbers.
  13. **Example**
  14. ```js
  15. const toRegexRange = require('to-regex-range');
  16. const regex = new RegExp(toRegexRange('15', '95'));
  17. ```
  18. A string is returned so that you can do whatever you need with it before passing it to `new RegExp()` (like adding `^` or `$` boundaries, defining flags, or combining it another string).
  19. <br>
  20. </details>
  21. <details>
  22. <summary><strong>Why use this library?</strong></summary>
  23. <br>
  24. ### Convenience
  25. Creating regular expressions for matching numbers gets deceptively complicated pretty fast.
  26. For example, let's say you need a validation regex for matching part of a user-id, postal code, social security number, tax id, etc:
  27. * regex for matching `1` => `/1/` (easy enough)
  28. * regex for matching `1` through `5` => `/[1-5]/` (not bad...)
  29. * regex for matching `1` or `5` => `/(1|5)/` (still easy...)
  30. * regex for matching `1` through `50` => `/([1-9]|[1-4][0-9]|50)/` (uh-oh...)
  31. * regex for matching `1` through `55` => `/([1-9]|[1-4][0-9]|5[0-5])/` (no prob, I can do this...)
  32. * regex for matching `1` through `555` => `/([1-9]|[1-9][0-9]|[1-4][0-9]{2}|5[0-4][0-9]|55[0-5])/` (maybe not...)
  33. * regex for matching `0001` through `5555` => `/(0{3}[1-9]|0{2}[1-9][0-9]|0[1-9][0-9]{2}|[1-4][0-9]{3}|5[0-4][0-9]{2}|55[0-4][0-9]|555[0-5])/` (okay, I get the point!)
  34. The numbers are contrived, but they're also really basic. In the real world you might need to generate a regex on-the-fly for validation.
  35. **Learn more**
  36. If you're interested in learning more about [character classes](http://www.regular-expressions.info/charclass.html) and other regex features, I personally have always found [regular-expressions.info](http://www.regular-expressions.info/charclass.html) to be pretty useful.
  37. ### Heavily tested
  38. As of April 07, 2019, this library runs [>1m test assertions](./test/test.js) against generated regex-ranges to provide brute-force verification that results are correct.
  39. Tests run in ~280ms on my MacBook Pro, 2.5 GHz Intel Core i7.
  40. ### Optimized
  41. Generated regular expressions are optimized:
  42. * duplicate sequences and character classes are reduced using quantifiers
  43. * smart enough to use `?` conditionals when number(s) or range(s) can be positive or negative
  44. * uses fragment caching to avoid processing the same exact string more than once
  45. <br>
  46. </details>
  47. ## Usage
  48. Add this library to your javascript application with the following line of code
  49. ```js
  50. const toRegexRange = require('to-regex-range');
  51. ```
  52. The main export is a function that takes two integers: the `min` value and `max` value (formatted as strings or numbers).
  53. ```js
  54. const source = toRegexRange('15', '95');
  55. //=> 1[5-9]|[2-8][0-9]|9[0-5]
  56. const regex = new RegExp(`^${source}$`);
  57. console.log(regex.test('14')); //=> false
  58. console.log(regex.test('50')); //=> true
  59. console.log(regex.test('94')); //=> true
  60. console.log(regex.test('96')); //=> false
  61. ```
  62. ## Options
  63. ### options.capture
  64. **Type**: `boolean`
  65. **Deafault**: `undefined`
  66. Wrap the returned value in parentheses when there is more than one regex condition. Useful when you're dynamically generating ranges.
  67. ```js
  68. console.log(toRegexRange('-10', '10'));
  69. //=> -[1-9]|-?10|[0-9]
  70. console.log(toRegexRange('-10', '10', { capture: true }));
  71. //=> (-[1-9]|-?10|[0-9])
  72. ```
  73. ### options.shorthand
  74. **Type**: `boolean`
  75. **Deafault**: `undefined`
  76. Use the regex shorthand for `[0-9]`:
  77. ```js
  78. console.log(toRegexRange('0', '999999'));
  79. //=> [0-9]|[1-9][0-9]{1,5}
  80. console.log(toRegexRange('0', '999999', { shorthand: true }));
  81. //=> \d|[1-9]\d{1,5}
  82. ```
  83. ### options.relaxZeros
  84. **Type**: `boolean`
  85. **Default**: `true`
  86. This option relaxes matching for leading zeros when when ranges are zero-padded.
  87. ```js
  88. const source = toRegexRange('-0010', '0010');
  89. const regex = new RegExp(`^${source}$`);
  90. console.log(regex.test('-10')); //=> true
  91. console.log(regex.test('-010')); //=> true
  92. console.log(regex.test('-0010')); //=> true
  93. console.log(regex.test('10')); //=> true
  94. console.log(regex.test('010')); //=> true
  95. console.log(regex.test('0010')); //=> true
  96. ```
  97. When `relaxZeros` is false, matching is strict:
  98. ```js
  99. const source = toRegexRange('-0010', '0010', { relaxZeros: false });
  100. const regex = new RegExp(`^${source}$`);
  101. console.log(regex.test('-10')); //=> false
  102. console.log(regex.test('-010')); //=> false
  103. console.log(regex.test('-0010')); //=> true
  104. console.log(regex.test('10')); //=> false
  105. console.log(regex.test('010')); //=> false
  106. console.log(regex.test('0010')); //=> true
  107. ```
  108. ## Examples
  109. | **Range** | **Result** | **Compile time** |
  110. | --- | --- | --- |
  111. | `toRegexRange(-10, 10)` | `-[1-9]\|-?10\|[0-9]` | _132μs_ |
  112. | `toRegexRange(-100, -10)` | `-1[0-9]\|-[2-9][0-9]\|-100` | _50μs_ |
  113. | `toRegexRange(-100, 100)` | `-[1-9]\|-?[1-9][0-9]\|-?100\|[0-9]` | _42μs_ |
  114. | `toRegexRange(001, 100)` | `0{0,2}[1-9]\|0?[1-9][0-9]\|100` | _109μs_ |
  115. | `toRegexRange(001, 555)` | `0{0,2}[1-9]\|0?[1-9][0-9]\|[1-4][0-9]{2}\|5[0-4][0-9]\|55[0-5]` | _51μs_ |
  116. | `toRegexRange(0010, 1000)` | `0{0,2}1[0-9]\|0{0,2}[2-9][0-9]\|0?[1-9][0-9]{2}\|1000` | _31μs_ |
  117. | `toRegexRange(1, 50)` | `[1-9]\|[1-4][0-9]\|50` | _24μs_ |
  118. | `toRegexRange(1, 55)` | `[1-9]\|[1-4][0-9]\|5[0-5]` | _23μs_ |
  119. | `toRegexRange(1, 555)` | `[1-9]\|[1-9][0-9]\|[1-4][0-9]{2}\|5[0-4][0-9]\|55[0-5]` | _30μs_ |
  120. | `toRegexRange(1, 5555)` | `[1-9]\|[1-9][0-9]{1,2}\|[1-4][0-9]{3}\|5[0-4][0-9]{2}\|55[0-4][0-9]\|555[0-5]` | _43μs_ |
  121. | `toRegexRange(111, 555)` | `11[1-9]\|1[2-9][0-9]\|[2-4][0-9]{2}\|5[0-4][0-9]\|55[0-5]` | _38μs_ |
  122. | `toRegexRange(29, 51)` | `29\|[34][0-9]\|5[01]` | _24μs_ |
  123. | `toRegexRange(31, 877)` | `3[1-9]\|[4-9][0-9]\|[1-7][0-9]{2}\|8[0-6][0-9]\|87[0-7]` | _32μs_ |
  124. | `toRegexRange(5, 5)` | `5` | _8μs_ |
  125. | `toRegexRange(5, 6)` | `5\|6` | _11μs_ |
  126. | `toRegexRange(1, 2)` | `1\|2` | _6μs_ |
  127. | `toRegexRange(1, 5)` | `[1-5]` | _15μs_ |
  128. | `toRegexRange(1, 10)` | `[1-9]\|10` | _22μs_ |
  129. | `toRegexRange(1, 100)` | `[1-9]\|[1-9][0-9]\|100` | _25μs_ |
  130. | `toRegexRange(1, 1000)` | `[1-9]\|[1-9][0-9]{1,2}\|1000` | _31μs_ |
  131. | `toRegexRange(1, 10000)` | `[1-9]\|[1-9][0-9]{1,3}\|10000` | _34μs_ |
  132. | `toRegexRange(1, 100000)` | `[1-9]\|[1-9][0-9]{1,4}\|100000` | _36μs_ |
  133. | `toRegexRange(1, 1000000)` | `[1-9]\|[1-9][0-9]{1,5}\|1000000` | _42μs_ |
  134. | `toRegexRange(1, 10000000)` | `[1-9]\|[1-9][0-9]{1,6}\|10000000` | _42μs_ |
  135. ## Heads up!
  136. **Order of arguments**
  137. When the `min` is larger than the `max`, values will be flipped to create a valid range:
  138. ```js
  139. toRegexRange('51', '29');
  140. ```
  141. Is effectively flipped to:
  142. ```js
  143. toRegexRange('29', '51');
  144. //=> 29|[3-4][0-9]|5[0-1]
  145. ```
  146. **Steps / increments**
  147. This library does not support steps (increments). A pr to add support would be welcome.
  148. ## History
  149. ### v2.0.0 - 2017-04-21
  150. **New features**
  151. Adds support for zero-padding!
  152. ### v1.0.0
  153. **Optimizations**
  154. Repeating ranges are now grouped using quantifiers. rocessing time is roughly the same, but the generated regex is much smaller, which should result in faster matching.
  155. ## Attribution
  156. Inspired by the python library [range-regex](https://github.com/dimka665/range-regex).
  157. ## About
  158. <details>
  159. <summary><strong>Contributing</strong></summary>
  160. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  161. </details>
  162. <details>
  163. <summary><strong>Running Tests</strong></summary>
  164. 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:
  165. ```sh
  166. $ npm install && npm test
  167. ```
  168. </details>
  169. <details>
  170. <summary><strong>Building docs</strong></summary>
  171. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  172. To generate the readme, run the following command:
  173. ```sh
  174. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  175. ```
  176. </details>
  177. ### Related projects
  178. You might also be interested in these projects:
  179. * [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used… [more](https://github.com/jonschlinkert/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range "Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used by micromatch.")
  180. * [fill-range](https://www.npmjs.com/package/fill-range): Fill in a range of numbers or letters, optionally passing an increment or `step` to… [more](https://github.com/jonschlinkert/fill-range) | [homepage](https://github.com/jonschlinkert/fill-range "Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`")
  181. * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
  182. * [repeat-element](https://www.npmjs.com/package/repeat-element): Create an array by repeating the given value n times. | [homepage](https://github.com/jonschlinkert/repeat-element "Create an array by repeating the given value n times.")
  183. * [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.")
  184. ### Contributors
  185. | **Commits** | **Contributor** |
  186. | --- | --- |
  187. | 63 | [jonschlinkert](https://github.com/jonschlinkert) |
  188. | 3 | [doowb](https://github.com/doowb) |
  189. | 2 | [realityking](https://github.com/realityking) |
  190. ### Author
  191. **Jon Schlinkert**
  192. * [GitHub Profile](https://github.com/jonschlinkert)
  193. * [Twitter Profile](https://twitter.com/jonschlinkert)
  194. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  195. Please consider supporting me on Patreon, or [start your own Patreon page](https://patreon.com/invite/bxpbvm)!
  196. <a href="https://www.patreon.com/jonschlinkert">
  197. <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" height="50">
  198. </a>
  199. ### License
  200. Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).
  201. Released under the [MIT License](LICENSE).
  202. ***
  203. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on April 07, 2019._