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.

148 lines
3.9 KiB

4 years ago
  1. # normalize-range
  2. Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates.
  3. [![Build Status](https://travis-ci.org/jamestalmage/normalize-range.svg?branch=master)](https://travis-ci.org/jamestalmage/normalize-range)
  4. [![Coverage Status](https://coveralls.io/repos/jamestalmage/normalize-range/badge.svg?branch=master&service=github)](https://coveralls.io/github/jamestalmage/normalize-range?branch=master)
  5. [![Code Climate](https://codeclimate.com/github/jamestalmage/normalize-range/badges/gpa.svg)](https://codeclimate.com/github/jamestalmage/normalize-range)
  6. [![Dependency Status](https://david-dm.org/jamestalmage/normalize-range.svg)](https://david-dm.org/jamestalmage/normalize-range)
  7. [![devDependency Status](https://david-dm.org/jamestalmage/normalize-range/dev-status.svg)](https://david-dm.org/jamestalmage/normalize-range#info=devDependencies)
  8. [![NPM](https://nodei.co/npm/normalize-range.png)](https://nodei.co/npm/normalize-range/)
  9. ## Usage
  10. ```js
  11. var nr = require('normalize-range');
  12. nr.wrap(0, 360, 400);
  13. //=> 40
  14. nr.wrap(0, 360, -90);
  15. //=> 270
  16. nr.limit(0, 100, 500);
  17. //=> 100
  18. nr.limit(0, 100, -20);
  19. //=> 0
  20. // There is a convenient currying function
  21. var wrapAngle = nr.curry(0, 360).wrap;
  22. var limitTo10 = nr.curry(0, 10).limit;
  23. wrapAngle(-30);
  24. //=> 330
  25. ```
  26. ## API
  27. ### wrap(min, max, value)
  28. Normalizes a values that "wraps around". For example, in a polar coordinate system, 270˚ can also be
  29. represented as -90˚.
  30. For wrapping purposes we assume `max` is functionally equivalent to `min`, and that `wrap(max + 1) === wrap(min + 1)`.
  31. Wrap always assumes that `min` is *inclusive*, and `max` is *exclusive*.
  32. In other words, if `value === max` the function will wrap it, and return `min`, but `min` will not be wrapped.
  33. ```js
  34. nr.wrap(0, 360, 0) === 0;
  35. nr.wrap(0, 360, 360) === 0;
  36. nr.wrap(0, 360, 361) === 1;
  37. nr.wrap(0, 360, -1) === 359;
  38. ```
  39. You are not restricted to whole numbers, and ranges can be negative.
  40. ```js
  41. var π = Math.PI;
  42. var radianRange = nr.curry(-π, π);
  43. redianRange.wrap(0) === 0;
  44. nr.wrap(π) === -π;
  45. nr.wrap(4 * π / 3) === -2 * π / 3;
  46. ```
  47. ### limit(min, max, value)
  48. Normalize the value by bringing it within the range.
  49. If `value` is greater than `max`, `max` will be returned.
  50. If `value` is less than `min`, `min` will be returned.
  51. Otherwise, `value` is returned unaltered.
  52. Both ends of this range are *inclusive*.
  53. ### test(min, max, value, [minExclusive], [maxExclusive])
  54. Returns `true` if `value` is within the range, `false` otherwise.
  55. It defaults to `inclusive` on both ends of the range, but that can be
  56. changed by setting `minExclusive` and/or `maxExclusive` to a truthy value.
  57. ### validate(min, max, value, [minExclusive], [maxExclusive])
  58. Returns `value` or throws an error if `value` is outside the specified range.
  59. ### name(min, max, value, [minExclusive], [maxExclusive])
  60. Returns a string representing this range in
  61. [range notation](https://en.wikipedia.org/wiki/Interval_(mathematics)#Classification_of_intervals).
  62. ### curry(min, max, [minExclusive], [maxExclusive])
  63. Convenience method for currying all method arguments except `value`.
  64. ```js
  65. var angle = require('normalize-range').curry(-180, 180, false, true);
  66. angle.wrap(270)
  67. //=> -90
  68. angle.limit(200)
  69. //=> 180
  70. angle.test(0)
  71. //=> true
  72. angle.validate(300)
  73. //=> throws an Error
  74. angle.toString() // or angle.name()
  75. //=> "[-180,180)"
  76. ```
  77. #### min
  78. *Required*
  79. Type: `number`
  80. The minimum value (inclusive) of the range.
  81. #### max
  82. *Required*
  83. Type: `number`
  84. The maximum value (exclusive) of the range.
  85. #### value
  86. *Required*
  87. Type: `number`
  88. The value to be normalized.
  89. #### returns
  90. Type: `number`
  91. The normalized value.
  92. ## Building and Releasing
  93. - `npm test`: tests, linting, coverage and style checks.
  94. - `npm run watch`: autotest mode for active development.
  95. - `npm run debug`: run tests without coverage (istanbul can obscure line #'s)
  96. Release via `cut-release` tool.
  97. ## License
  98. MIT © [James Talmage](http://github.com/jamestalmage)