\href{http://travis-ci.org/pvorb/node-clone}{\texttt{ }}

\href{http://npm-stat.com/charts.html?package=clone}{\texttt{ }}

offers foolproof {\itshape deep cloning} of objects, arrays, numbers, strings etc. in Java\+Script.\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7924}{}\doxysection{Installation}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7924}
\begin{DoxyVerb}npm install clone
\end{DoxyVerb}
 (It also works with browserify, ender or standalone.)\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7925}{}\doxysection{Example}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7925}

\begin{DoxyCode}{0}
\DoxyCodeLine{var clone = require('clone');}
\DoxyCodeLine{}
\DoxyCodeLine{var a, b;}
\DoxyCodeLine{}
\DoxyCodeLine{a = \{ foo: \{ bar: 'baz' \} \};  // initial value of a}
\DoxyCodeLine{}
\DoxyCodeLine{b = clone(a);                 // clone a -\/> b}
\DoxyCodeLine{a.foo.bar = 'foo';            // change a}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(a);               // show a}
\DoxyCodeLine{console.log(b);               // show b }

\end{DoxyCode}


This will print\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{ foo: \{ bar: 'foo' \} \}}
\DoxyCodeLine{\{ foo: \{ bar: 'baz' \} \} }

\end{DoxyCode}


{\bfseries{clone}} masters cloning simple objects (even with custom prototype), arrays, Date objects, and Reg\+Exp objects. Everything is cloned recursively, so that you can clone dates in arrays in objects, for example.\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7926}{}\doxysection{API}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7926}
{\ttfamily clone(val, circular, depth)}


\begin{DoxyItemize}
\item {\ttfamily val} -- the value that you want to clone, any type allowed
\item {\ttfamily circular} -- boolean

Call {\ttfamily clone} with {\ttfamily circular} set to {\ttfamily false} if you are certain that {\ttfamily obj} contains no circular references. This will give better performance if needed. There is no error if {\ttfamily undefined} or {\ttfamily null} is passed as {\ttfamily obj}.
\item {\ttfamily depth} -- depth to which the object is to be cloned (optional, defaults to infinity)
\end{DoxyItemize}

{\ttfamily clone.\+clone\+Prototype(obj)}


\begin{DoxyItemize}
\item {\ttfamily obj} -- the object that you want to clone
\end{DoxyItemize}

Does a prototype clone as \href{http://oranlooney.com/functional-javascript/}{\texttt{ described by Oran Looney}}.\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7927}{}\doxysection{Circular References}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7927}

\begin{DoxyCode}{0}
\DoxyCodeLine{var a, b;}
\DoxyCodeLine{}
\DoxyCodeLine{a = \{ hello: 'world' \};}
\DoxyCodeLine{}
\DoxyCodeLine{a.myself = a;}
\DoxyCodeLine{b = clone(a);}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(b); }

\end{DoxyCode}


This will print\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{ hello: "{}world"{}, myself: [Circular] \} }

\end{DoxyCode}


So, {\ttfamily b.\+myself} points to {\ttfamily b}, not {\ttfamily a}. Neat!\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7928}{}\doxysection{Test}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7928}
\begin{DoxyVerb}npm test
\end{DoxyVerb}
\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7929}{}\doxysection{Caveat}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7929}
Some special objects like a socket or {\ttfamily process.\+stdout}/{\ttfamily stderr} are known to not be cloneable. If you find other objects that cannot be cloned, please \href{https://github.com/pvorb/node-clone/issues/new}{\texttt{ open an issue}}.\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7930}{}\doxysection{Bugs and Issues}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7930}
If you encounter any bugs or issues, feel free to \href{https://github.com/pvorb/node-clone/issues}{\texttt{ open an issue at github}} or send me an email to \href{mailto:paul@vorba.ch}{\texttt{ paul@vorba.\+ch}}. I also always like to hear from you, if you’re using my code.\hypertarget{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7931}{}\doxysection{License}\label{md_sfcode_node_modules_defaults_node_modules_clone_README_autotoc_md7931}
Copyright © 2011-\/2015 \href{http://paul.vorba.ch/}{\texttt{ Paul Vorbach}} and \href{https://github.com/pvorb/node-clone/graphs/contributors}{\texttt{ contributors}}.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “\+Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions\+:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “\+AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 