Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates.

\href{https://travis-ci.org/jamestalmage/normalize-range}{\texttt{ }} \href{https://coveralls.io/github/jamestalmage/normalize-range?branch=master}{\texttt{ }} \href{https://codeclimate.com/github/jamestalmage/normalize-range}{\texttt{ }} \href{https://david-dm.org/jamestalmage/normalize-range}{\texttt{ }} \href{https://david-dm.org/jamestalmage/normalize-range\#info=devDependencies}{\texttt{ }}

\href{https://nodei.co/npm/normalize-range/}{\texttt{ }}\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18363}{}\doxysection{Usage}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18363}

\begin{DoxyCode}{0}
\DoxyCodeLine{var nr = require('normalize-\/range');}
\DoxyCodeLine{}
\DoxyCodeLine{nr.wrap(0, 360, 400);}
\DoxyCodeLine{//=> 40}
\DoxyCodeLine{}
\DoxyCodeLine{nr.wrap(0, 360, -\/90);}
\DoxyCodeLine{//=> 270}
\DoxyCodeLine{}
\DoxyCodeLine{nr.limit(0, 100, 500);}
\DoxyCodeLine{//=> 100}
\DoxyCodeLine{}
\DoxyCodeLine{nr.limit(0, 100, -\/20);}
\DoxyCodeLine{//=> 0}
\DoxyCodeLine{}
\DoxyCodeLine{// There is a convenient currying function}
\DoxyCodeLine{var wrapAngle = nr.curry(0, 360).wrap;}
\DoxyCodeLine{var limitTo10 = nr.curry(0, 10).limit;}
\DoxyCodeLine{}
\DoxyCodeLine{wrapAngle(-\/30);}
\DoxyCodeLine{//=> 330 }

\end{DoxyCode}
 \hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18364}{}\doxysection{API}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18364}
\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18365}{}\doxysubsection{wrap(min, max, value)}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18365}
Normalizes a values that \char`\"{}wraps around\char`\"{}. For example, in a polar coordinate system, 270˚ can also be represented as -\/90˚. For wrapping purposes we assume {\ttfamily max} is functionally equivalent to {\ttfamily min}, and that {\ttfamily wrap(max + 1) === wrap(min + 1)}. Wrap always assumes that {\ttfamily min} is {\itshape inclusive}, and {\ttfamily max} is {\itshape exclusive}. In other words, if {\ttfamily value === max} the function will wrap it, and return {\ttfamily min}, but {\ttfamily min} will not be wrapped.


\begin{DoxyCode}{0}
\DoxyCodeLine{nr.wrap(0, 360, 0) === 0;}
\DoxyCodeLine{nr.wrap(0, 360, 360) === 0;}
\DoxyCodeLine{nr.wrap(0, 360, 361) === 1;}
\DoxyCodeLine{nr.wrap(0, 360, -\/1) === 359; }

\end{DoxyCode}


You are not restricted to whole numbers, and ranges can be negative.


\begin{DoxyCode}{0}
\DoxyCodeLine{var π = Math.PI;}
\DoxyCodeLine{var radianRange = nr.curry(-\/π, π);}
\DoxyCodeLine{}
\DoxyCodeLine{redianRange.wrap(0) === 0;}
\DoxyCodeLine{nr.wrap(π) === -\/π;}
\DoxyCodeLine{nr.wrap(4 * π / 3) === -\/2 * π / 3; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18366}{}\doxysubsection{limit(min, max, value)}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18366}
Normalize the value by bringing it within the range. If {\ttfamily value} is greater than {\ttfamily max}, {\ttfamily max} will be returned. If {\ttfamily value} is less than {\ttfamily min}, {\ttfamily min} will be returned. Otherwise, {\ttfamily value} is returned unaltered. Both ends of this range are {\itshape inclusive}.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18367}{}\doxysubsection{test(min, max, value, \mbox{[}min\+Exclusive\mbox{]}, \mbox{[}max\+Exclusive\mbox{]})}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18367}
Returns {\ttfamily true} if {\ttfamily value} is within the range, {\ttfamily false} otherwise. It defaults to {\ttfamily inclusive} on both ends of the range, but that can be changed by setting {\ttfamily min\+Exclusive} and/or {\ttfamily max\+Exclusive} to a truthy value.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18368}{}\doxysubsection{validate(min, max, value, \mbox{[}min\+Exclusive\mbox{]}, \mbox{[}max\+Exclusive\mbox{]})}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18368}
Returns {\ttfamily value} or throws an error if {\ttfamily value} is outside the specified range.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18369}{}\doxysubsection{name(min, max, value, \mbox{[}min\+Exclusive\mbox{]}, \mbox{[}max\+Exclusive\mbox{]})}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18369}
Returns a string representing this range in \href{https://en.wikipedia.org/wiki/Interval_(mathematics)\#Classification_of_intervals}{\texttt{ range notation}}.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18370}{}\doxysubsection{curry(min, max, \mbox{[}min\+Exclusive\mbox{]}, \mbox{[}max\+Exclusive\mbox{]})}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18370}
Convenience method for currying all method arguments except {\ttfamily value}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var angle = require('normalize-\/range').curry(-\/180, 180, false, true);}
\DoxyCodeLine{}
\DoxyCodeLine{angle.wrap(270)}
\DoxyCodeLine{//=> -\/90}
\DoxyCodeLine{}
\DoxyCodeLine{angle.limit(200)}
\DoxyCodeLine{//=> 180}
\DoxyCodeLine{}
\DoxyCodeLine{angle.test(0)}
\DoxyCodeLine{//=> true}
\DoxyCodeLine{}
\DoxyCodeLine{angle.validate(300)}
\DoxyCodeLine{//=> throws an Error}
\DoxyCodeLine{}
\DoxyCodeLine{angle.toString() // or angle.name()}
\DoxyCodeLine{//=> "{}[-\/180,180)"{} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18371}{}\doxysubsubsection{min}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18371}
{\itshape Required} ~\newline
 Type\+: {\ttfamily number}

The minimum value (inclusive) of the range.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18372}{}\doxysubsubsection{max}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18372}
{\itshape Required} ~\newline
 Type\+: {\ttfamily number}

The maximum value (exclusive) of the range.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18373}{}\doxysubsubsection{value}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18373}
{\itshape Required} ~\newline
 Type\+: {\ttfamily number}

The value to be normalized.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18374}{}\doxysubsubsection{returns}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18374}
Type\+: {\ttfamily number}

The normalized value.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18375}{}\doxysection{Building and Releasing}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18375}

\begin{DoxyItemize}
\item {\ttfamily npm test}\+: tests, linting, coverage and style checks.
\item {\ttfamily npm run watch}\+: autotest mode for active development.
\item {\ttfamily npm run debug}\+: run tests without coverage (istanbul can obscure line \#\textquotesingle{}s)
\end{DoxyItemize}

Release via {\ttfamily cut-\/release} tool.\hypertarget{md_sfcode_node_modules_normalize_range_readme_autotoc_md18376}{}\doxysection{License}\label{md_sfcode_node_modules_normalize_range_readme_autotoc_md18376}
MIT © \href{http://github.com/jamestalmage}{\texttt{ James Talmage}} 