source: trip-planner-front/node_modules/normalize-range/readme.md@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[6a3a178]1# normalize-range
2
3Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates.
4
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)
10
11[![NPM](https://nodei.co/npm/normalize-range.png)](https://nodei.co/npm/normalize-range/)
12
13## Usage
14
15```js
16var nr = require('normalize-range');
17
18nr.wrap(0, 360, 400);
19//=> 40
20
21nr.wrap(0, 360, -90);
22//=> 270
23
24nr.limit(0, 100, 500);
25//=> 100
26
27nr.limit(0, 100, -20);
28//=> 0
29
30// There is a convenient currying function
31var wrapAngle = nr.curry(0, 360).wrap;
32var limitTo10 = nr.curry(0, 10).limit;
33
34wrapAngle(-30);
35//=> 330
36```
37## API
38
39### wrap(min, max, value)
40
41Normalizes a values that "wraps around". For example, in a polar coordinate system, 270˚ can also be
42represented as -90˚.
43For wrapping purposes we assume `max` is functionally equivalent to `min`, and that `wrap(max + 1) === wrap(min + 1)`.
44Wrap always assumes that `min` is *inclusive*, and `max` is *exclusive*.
45In other words, if `value === max` the function will wrap it, and return `min`, but `min` will not be wrapped.
46
47```js
48nr.wrap(0, 360, 0) === 0;
49nr.wrap(0, 360, 360) === 0;
50nr.wrap(0, 360, 361) === 1;
51nr.wrap(0, 360, -1) === 359;
52```
53
54You are not restricted to whole numbers, and ranges can be negative.
55
56```js
57var π = Math.PI;
58var radianRange = nr.curry(-π, π);
59
60redianRange.wrap(0) === 0;
61nr.wrap(π) === -π;
62nr.wrap(4 * π / 3) === -2 * π / 3;
63```
64
65### limit(min, max, value)
66
67Normalize the value by bringing it within the range.
68If `value` is greater than `max`, `max` will be returned.
69If `value` is less than `min`, `min` will be returned.
70Otherwise, `value` is returned unaltered.
71Both ends of this range are *inclusive*.
72
73### test(min, max, value, [minExclusive], [maxExclusive])
74
75Returns `true` if `value` is within the range, `false` otherwise.
76It defaults to `inclusive` on both ends of the range, but that can be
77changed by setting `minExclusive` and/or `maxExclusive` to a truthy value.
78
79### validate(min, max, value, [minExclusive], [maxExclusive])
80
81Returns `value` or throws an error if `value` is outside the specified range.
82
83### name(min, max, value, [minExclusive], [maxExclusive])
84
85Returns a string representing this range in
86[range notation](https://en.wikipedia.org/wiki/Interval_(mathematics)#Classification_of_intervals).
87
88### curry(min, max, [minExclusive], [maxExclusive])
89
90Convenience method for currying all method arguments except `value`.
91
92```js
93var angle = require('normalize-range').curry(-180, 180, false, true);
94
95angle.wrap(270)
96//=> -90
97
98angle.limit(200)
99//=> 180
100
101angle.test(0)
102//=> true
103
104angle.validate(300)
105//=> throws an Error
106
107angle.toString() // or angle.name()
108//=> "[-180,180)"
109```
110
111#### min
112
113*Required*
114Type: `number`
115
116The minimum value (inclusive) of the range.
117
118#### max
119
120*Required*
121Type: `number`
122
123The maximum value (exclusive) of the range.
124
125#### value
126
127*Required*
128Type: `number`
129
130The value to be normalized.
131
132#### returns
133
134Type: `number`
135
136The normalized value.
137
138## Building and Releasing
139
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)
143
144Release via `cut-release` tool.
145
146## License
147
148MIT © [James Talmage](http://github.com/jamestalmage)
Note: See TracBrowser for help on using the repository browser.