\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{EllieAtWHL (EllieAtWHL)} \pdfinfo{ /Title (salesforce-javascript-functional.pdf) /Creator (Cheatography) /Author (EllieAtWHL (EllieAtWHL)) /Subject (Salesforce Javascript Functional Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{164F05} \definecolor{LightBackground}{HTML}{F7F9F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Salesforce Javascript Functional Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{EllieAtWHL (EllieAtWHL)} via \textcolor{DarkBackground}{\uline{cheatography.com/120941/cs/21955/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}EllieAtWHL (EllieAtWHL) \\ \uline{cheatography.com/ellieatwhl} \\ \uline{\seqsplit{trailblazer}.me/id/elliematthewman} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 6th March, 2020.\\ Updated 7th April, 2021.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Arrow Functions (Fat Arrows)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Arrow functions create a concise expression that encapsulates a small piece of functionality. Additionally, arrows retain the scope of the caller inside the function eliminating the need of self = this. \newline % Row Count 5 (+ 5) {\bf{Example}} \newline % Row Count 6 (+ 1) `// const multiply = function(x,y) \{` \newline % Row Count 7 (+ 1) `// ~ return x * y;` \newline % Row Count 8 (+ 1) `// \}` \newline % Row Count 9 (+ 1) `// Can be rewritten as:` \newline % Row Count 10 (+ 1) `// const multiply = (x, y) =\textgreater{} \{ return x * y \};` \newline % Row Count 11 (+ 1) `// Since the function is a single expression return and braces are not needed.` \newline % Row Count 13 (+ 2) `const multiply = (x, y) =\textgreater{} x * y;` \newline % Row Count 14 (+ 1) `console.log(multiply(5,10)) //50`% Row Count 15 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Function Delegates}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Function delegates encapsulate a method allowing functions to be composed or passed as data. \newline % Row Count 2 (+ 2) {\bf{Example}} \newline % Row Count 3 (+ 1) `const isZero = n =\textgreater{} n === 0;` \newline % Row Count 4 (+ 1) `const a = {[}0,1,0,3,4,0{]};` \newline % Row Count 5 (+ 1) \seqsplit{`console.log(a.filter(isZero).length);} // 3`% Row Count 6 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Expressions Instead of Statements}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Statements define an action and are executed for their side effect. Expressions produce a result without mutating state. \newline % Row Count 3 (+ 3) {\bf{Statement}} \newline % Row Count 4 (+ 1) `const getSalutation = function(hour) \{` \newline % Row Count 5 (+ 1) `~var salutation; // temp value` \newline % Row Count 6 (+ 1) `~if (hour \textless{} 12) \{` \newline % Row Count 7 (+ 1) `~~ salutation = "Good Morning";` \newline % Row Count 8 (+ 1) `\}` \newline % Row Count 9 (+ 1) `~else \{` \newline % Row Count 10 (+ 1) `~~ salutation = "Good Afternoon"` \newline % Row Count 11 (+ 1) `\}` \newline % Row Count 12 (+ 1) `~return salutation; // mutated value` \newline % Row Count 13 (+ 1) `\}` \newline % Row Count 14 (+ 1) {\bf{Expression}} \newline % Row Count 15 (+ 1) `const getSalutation = (hour) =\textgreater{} hour \textless{} 12 ? "Good Morning" : "Good Afternoon";` \newline % Row Count 17 (+ 2) \seqsplit{`console.log(getSalutation(10));} // Good Morning'`% Row Count 18 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Higher Order Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{A function that accepts another function as a parameter, or returns another function. \newline % Row Count 2 (+ 2) {\bf{Example}} \newline % Row Count 3 (+ 1) `function mapConsecutive(values, fn) \{` \newline % Row Count 4 (+ 1) `~let result = {[}{]};` \newline % Row Count 5 (+ 1) `~for(let i=0; i \textless{} values.length -1; i++) \{` \newline % Row Count 6 (+ 1) `~~result.push(fn(values{[}i{]}, values{[}i+1{]}));` \newline % Row Count 8 (+ 2) `~\}` \newline % Row Count 9 (+ 1) `~return result;` \newline % Row Count 10 (+ 1) `\}` \newline % Row Count 11 (+ 1) `const letters = {[}'a','b','c','d','e','f','g'{]};` \newline % Row Count 12 (+ 1) `let twoByTwo = mapConsecutive(letters, (x,y) =\textgreater{} {[}x,y{]});` \newline % Row Count 14 (+ 2) `console.log(twoByTwo);` \newline % Row Count 15 (+ 1) `// {[}{[}a,b{]}, {[}b,c{]}, {[}c,d{]}, {[}d,e{]}, {[}e,f{]}, {[}f,g{]}{]}`% Row Count 16 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Method Chaining}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Method chains allow a series of functions to operate in succession to reach a final result. Method chains allow \newline % Row Count 3 (+ 3) function composition similar to a pipeline. \newline % Row Count 4 (+ 1) {\bf{Example}} \newline % Row Count 5 (+ 1) `let cart = {[}\{name: "Drink", price: 3.12\},` \newline % Row Count 6 (+ 1) `~\{name: "Steak", price: 45.15\},` \newline % Row Count 7 (+ 1) `~\{name: "Drink", price: 11.01\}{]};` \newline % Row Count 8 (+ 1) `let drinkTotal = cart.filter(x=\textgreater{} x.name === "Drink")` \newline % Row Count 10 (+ 2) `~.map(x=\textgreater{} x.price)` \newline % Row Count 11 (+ 1) `~.reduce((t,v) =\textgreater{} t +=v)` \newline % Row Count 12 (+ 1) `~.toFixed(2);` \newline % Row Count 13 (+ 1) `console.log(Total Drink Cost \$\$\{drinkTotal\}); // Total Drink Cost \$14.13`% Row Count 15 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Currying}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Currying allows a function with multiple arguments to be translated into a sequence of functions. Curried functions can be tailored to match the signature of another function. \newline % Row Count 4 (+ 4) {\bf{Example}} \newline % Row Count 5 (+ 1) `const convertUnits = (toUnit, factor, offset = 0) =\textgreater{} input =\textgreater{} ((offset + input) *` \seqsplit{`factor).toFixed(2).concat(toUnit);`} \newline % Row Count 8 (+ 3) `const milesToKm = convertUnits('km', 1.60936, 0);` \newline % Row Count 10 (+ 2) `const poundsToKg = convertUnits('kg', 0.45460, 0);` \newline % Row Count 12 (+ 2) `const farenheitToCelsius = convertUnits('degrees C', 0.5556, -32);` \newline % Row Count 14 (+ 2) `milesToKm(10); //"16.09 km"` \newline % Row Count 15 (+ 1) `poundsToKg(2.5); //"1.14 kg"` \newline % Row Count 16 (+ 1) `farenheitToCelsius(98); //"36.67 degrees C"` \newline % Row Count 17 (+ 1) `const weightsInPounds = {[}5,15.4,9.8, 110{]};` \newline % Row Count 18 (+ 1) `// const weightsInKg = weightsInPounds.map(x =\textgreater{} convertUnits('kg', 0.45460,` \newline % Row Count 20 (+ 2) `0)(x));` \newline % Row Count 21 (+ 1) `// with currying` \newline % Row Count 22 (+ 1) `const weightsInKg = \seqsplit{weightsInPounds.map(poundsToKg);`} \newline % Row Count 24 (+ 2) `// 2.27kg, 7.00kg, 4.46kg, 50.01kg`% Row Count 25 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Array Manipulation Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Array Functions are the gateway to functional programming in JavaScript. These functions make short work of most imperative programming routines that work on arrays and collections. \newline % Row Count 4 (+ 4) `{[}{]}.every(fn)` \newline % Row Count 5 (+ 1) Checks if all elements in an array pass a test. \newline % Row Count 6 (+ 1) `{[}{]}.some(fn)` \newline % Row Count 7 (+ 1) Checks if any of the elements in an array pass a test. \newline % Row Count 9 (+ 2) `{[}{]}.find(fn)` \newline % Row Count 10 (+ 1) Returns the value of the first element in the array that passes a test. \newline % Row Count 12 (+ 2) `{[}{]}.filter(fn)` \newline % Row Count 13 (+ 1) Creates an array filled with only the array elements that pass a test. \newline % Row Count 15 (+ 2) `{[}{]}.map(fn)` \newline % Row Count 16 (+ 1) Creates a new array with the results of a function applied to every element in the array. \newline % Row Count 18 (+ 2) `{[}{]}.reduce(fn(accumulator, currentValue))` \newline % Row Count 19 (+ 1) Executes a provided function for each value of the array (from left-to-right). Returns a single value, the accumulator. \newline % Row Count 22 (+ 3) `{[}{]}.sort(fn(a,b))` {\emph{warning, mutates state!}} \newline % Row Count 23 (+ 1) Modifies an array by sorting the items within an array. An optional compare function can be used to customize sort behaviour. Use the spread operator to avoid mutation. `{[}...arr{]}.sort()` \newline % Row Count 27 (+ 4) `{[}{]}.reverse()` {\emph{warning, mutates state!}} \newline % Row Count 28 (+ 1) Reverses the order of the elements in an array. Use the spread operator to avoid mutation. `{[}...arr{]}.reverse()`% Row Count 31 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Pipelines}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{A pipeline allows for easy function composition when performing multiple operations on a variable. Since JavaScript lacks a Pipeline operator, a design pattern can be used to accomplish the task. \newline % Row Count 4 (+ 4) {\bf{Example}} \newline % Row Count 5 (+ 1) `const pipe = functions =\textgreater{} data =\textgreater{} \{` \newline % Row Count 6 (+ 1) `~return functions.reduce(` \newline % Row Count 7 (+ 1) `~~(value, func) =\textgreater{} func(value),` \newline % Row Count 8 (+ 1) `~data` \newline % Row Count 9 (+ 1) `~);` \newline % Row Count 10 (+ 1) `\};` \newline % Row Count 11 (+ 1) `let cart = {[}3.12, 45.15, 11.01{]};` \newline % Row Count 12 (+ 1) `const addSalesTax = (total, taxRate) =\textgreater{} (total * taxRate) + total;` \newline % Row Count 14 (+ 2) `const tally = orders =\textgreater{} pipe({[}` \newline % Row Count 15 (+ 1) `x =\textgreater{} x.reduce((total, val) =\textgreater{} total + val), // sum the order` \newline % Row Count 17 (+ 2) `x =\textgreater{} addSalesTax(x, 0.09),` \newline % Row Count 18 (+ 1) `x =\textgreater{} Order Total = \$\{x.toFixed(2)\} // convert to text` \newline % Row Count 20 (+ 2) `{]})(orders); // Order Total = 64.62`% Row Count 21 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}