3 Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates.
5 [![Build Status](https://travis-ci.org/jamestalmage/normalize-range.svg?branch=master)](https://travis-ci.org/jamestalmage/normalize-range)
6 [![Coverage Status](https://coveralls.io/repos/jamestalmage/normalize-range/badge.svg?branch=master&service=github)](https://coveralls.io/github/jamestalmage/normalize-range?branch=master)
7 [![Code Climate](https://codeclimate.com/github/jamestalmage/normalize-range/badges/gpa.svg)](https://codeclimate.com/github/jamestalmage/normalize-range)
8 [![Dependency Status](https://david-dm.org/jamestalmage/normalize-range.svg)](https://david-dm.org/jamestalmage/normalize-range)
9 [![devDependency Status](https://david-dm.org/jamestalmage/normalize-range/dev-status.svg)](https://david-dm.org/jamestalmage/normalize-range#info=devDependencies)
11 [![NPM](https://nodei.co/npm/normalize-range.png)](https://nodei.co/npm/normalize-range/)
16 var nr = require('normalize-range');
24 nr.limit(0, 100, 500);
27 nr.limit(0, 100, -20);
30 // There is a convenient currying function
31 var wrapAngle = nr.curry(0, 360).wrap;
32 var limitTo10 = nr.curry(0, 10).limit;
39 ### wrap(min, max, value)
41 Normalizes a values that "wraps around". For example, in a polar coordinate system, 270˚ can also be
43 For wrapping purposes we assume `max` is functionally equivalent to `min`, and that `wrap(max + 1) === wrap(min + 1)`.
44 Wrap always assumes that `min` is *inclusive*, and `max` is *exclusive*.
45 In other words, if `value === max` the function will wrap it, and return `min`, but `min` will not be wrapped.
48 nr.wrap(0, 360, 0) === 0;
49 nr.wrap(0, 360, 360) === 0;
50 nr.wrap(0, 360, 361) === 1;
51 nr.wrap(0, 360, -1) === 359;
54 You are not restricted to whole numbers, and ranges can be negative.
58 var radianRange = nr.curry(-π, π);
60 redianRange.wrap(0) === 0;
62 nr.wrap(4 * π / 3) === -2 * π / 3;
65 ### limit(min, max, value)
67 Normalize the value by bringing it within the range.
68 If `value` is greater than `max`, `max` will be returned.
69 If `value` is less than `min`, `min` will be returned.
70 Otherwise, `value` is returned unaltered.
71 Both ends of this range are *inclusive*.
73 ### test(min, max, value, [minExclusive], [maxExclusive])
75 Returns `true` if `value` is within the range, `false` otherwise.
76 It defaults to `inclusive` on both ends of the range, but that can be
77 changed by setting `minExclusive` and/or `maxExclusive` to a truthy value.
79 ### validate(min, max, value, [minExclusive], [maxExclusive])
81 Returns `value` or throws an error if `value` is outside the specified range.
83 ### name(min, max, value, [minExclusive], [maxExclusive])
85 Returns a string representing this range in
86 [range notation](https://en.wikipedia.org/wiki/Interval_(mathematics)#Classification_of_intervals).
88 ### curry(min, max, [minExclusive], [maxExclusive])
90 Convenience method for currying all method arguments except `value`.
93 var angle = require('normalize-range').curry(-180, 180, false, true);
107 angle.toString() // or angle.name()
116 The minimum value (inclusive) of the range.
123 The maximum value (exclusive) of the range.
130 The value to be normalized.
136 The normalized value.
138 ## Building and Releasing
140 - `npm test`: tests, linting, coverage and style checks.
141 - `npm run watch`: autotest mode for active development.
142 - `npm run debug`: run tests without coverage (istanbul can obscure line #'s)
144 Release via `cut-release` tool.
148 MIT © [James Talmage](http://github.com/jamestalmage)