Detects if a file is binary in Node.\+js using ✨promises✨. Similar to \href{http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary}{\texttt{ Perl\textquotesingle{}s {\ttfamily -\/B} switch}}, in that\+:
\begin{DoxyItemize}
\item it reads the first few thousand bytes of a file
\item checks for a {\ttfamily null} byte; if it\textquotesingle{}s found, it\textquotesingle{}s binary
\item flags non-\/\+ASCII characters. After a certain number of \char`\"{}weird\char`\"{} characters, the file is flagged as binary
\end{DoxyItemize}

Much of the logic is pretty much ported from \href{https://github.com/ggreer/the_silver_searcher}{\texttt{ ag}}.

Note\+: if the file doesn\textquotesingle{}t exist or is a directory, an error is thrown.\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13266}{}\doxysection{Installation}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13266}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install isbinaryfile }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13267}{}\doxysection{Usage}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13267}
Returns {\ttfamily Promise$<$boolean$>$} (or just {\ttfamily boolean} for {\ttfamily $\ast$\+Sync}). {\ttfamily true} if the file is binary, {\ttfamily false} otherwise.\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13268}{}\doxysubsection{is\+Binary\+File(filepath)}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13268}

\begin{DoxyItemize}
\item {\ttfamily filepath} -\/ a {\ttfamily string} indicating the path to the file.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13269}{}\doxysubsection{is\+Binary\+File(bytes\mbox{[}, size\mbox{]})}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13269}

\begin{DoxyItemize}
\item {\ttfamily bytes} -\/ a {\ttfamily Buffer} of the file\textquotesingle{}s contents.
\item {\ttfamily size} -\/ an optional {\ttfamily number} indicating the file size.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13270}{}\doxysubsection{is\+Binary\+File\+Sync(filepath)}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13270}

\begin{DoxyItemize}
\item {\ttfamily filepath} -\/ a {\ttfamily string} indicating the path to the file.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13271}{}\doxysubsection{is\+Binary\+File\+Sync(bytes\mbox{[}, size\mbox{]})}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13271}

\begin{DoxyItemize}
\item {\ttfamily bytes} -\/ a {\ttfamily Buffer} of the file\textquotesingle{}s contents.
\item {\ttfamily size} -\/ an optional {\ttfamily number} indicating the file size.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13272}{}\doxysubsection{Examples}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13272}
Here\textquotesingle{}s an arbitrary usage\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const isBinaryFile = require("{}isbinaryfile"{}).isBinaryFile;}
\DoxyCodeLine{const fs = require("{}fs"{});}
\DoxyCodeLine{}
\DoxyCodeLine{const filename = "{}fixtures/pdf.pdf"{};}
\DoxyCodeLine{const data = fs.readFileSync(filename);}
\DoxyCodeLine{const stat = fs.lstatSync(filename);}
\DoxyCodeLine{}
\DoxyCodeLine{isBinaryFile(data, stat.size).then((result) => \{}
\DoxyCodeLine{  if (result) \{}
\DoxyCodeLine{    console.log("{}It is binary!"{})}
\DoxyCodeLine{  \}}
\DoxyCodeLine{  else \{}
\DoxyCodeLine{    console.log("{}No it is not."{})}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{const isBinaryFileSync = require("{}isbinaryfile"{}).isBinaryFileSync;}
\DoxyCodeLine{const bytes = fs.readFileSync(filename);}
\DoxyCodeLine{const size = fs.lstatSync(filename).size;}
\DoxyCodeLine{console.log(isBinaryFileSync(bytes, size)); // true or false }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13273}{}\doxysection{Testing}\label{md_sfcode_node_modules_isbinaryfile_README_autotoc_md13273}
Run {\ttfamily npm install}, then run {\ttfamily npm test}. 