<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>javascript on Curtis Timson</title><link>https://hugo.curtiscode.dev/tags/javascript/</link><description>Recent content in javascript on Curtis Timson</description><generator>Hugo -- gohugo.io</generator><language>en-GB</language><lastBuildDate>Fri, 11 Feb 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://hugo.curtiscode.dev/tags/javascript/index.xml" rel="self" type="application/rss+xml"/><item><title>Filtering duplicate data points on Chart.js</title><link>https://hugo.curtiscode.dev/post/js/chartjs-filtering-duplicate-data/</link><pubDate>Fri, 11 Feb 2022 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/chartjs-filtering-duplicate-data/</guid><description>With large data sets, Chart.js data points can start to look cluttered and impact performance. This article looks at how these could be filtered.
What is Chart.js? Chart.js is an open-source library which provides simple and flexible Javascript charting.
There is also an npm package, react-chartjs-2, which provides Chart.js as React components.
Duplicate data points When using a line chart with a lot of data and unchanging values on the y-axis, there can be a lot of overlapping data points, which aren&amp;rsquo;t providing much value to the user:</description></item><item><title>AngularJs with Jest Unit Testing</title><link>https://hugo.curtiscode.dev/post/angularjs/angularjs-jest-unit-testing/</link><pubDate>Fri, 17 Nov 2017 18:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/angularjs/angularjs-jest-unit-testing/</guid><description>Jest, a unit testing framework by Facebook, is commonly associated with the React JS library. However Jest is not specifically for React, and can be used to test any javascript code you wish.
Recently I&amp;rsquo;ve been using Jest with AngularJs 1.6 and have put together the following step-by-step guide on how to get setup.
This guide includes:
Installing Angular Mocks and Jest CLI Creating an AngularJs Service Create Jest unit test file Running Jest tests with failed example Correcting service and running successful Jest tests Checking code coverage of tests TL;DR - Full Working Example on GitHub Install Angular Mocks and Jest CLI To use Jest with AngularJs you will require the Angular Mocks and Jest CLI packages, which can be installed using npm:</description></item><item><title>Default parameters with ES6</title><link>https://hugo.curtiscode.dev/post/js/default-parameters-es6/</link><pubDate>Wed, 05 Oct 2016 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/default-parameters-es6/</guid><description>With ES5 if you wish to set default parameters on a javascript function you would be required to check the value and supply a default if one hadn&amp;rsquo;t been set.
var myFunction = function(arg1, arg2){ arg1 = arg1 || 25; arg2 = arg2 || true; //do something } Even this has problems as passing in 0 for arg1 or false for arg2 would both evaluate as &amp;ldquo;falsey&amp;rdquo; and therefore set the default values.</description></item><item><title>Filtering arrays in AngularJS</title><link>https://hugo.curtiscode.dev/post/angularjs/filtering-arrays-in-angularjs/</link><pubDate>Wed, 24 Jun 2015 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/angularjs/filtering-arrays-in-angularjs/</guid><description>Previously I wrote an articleon how to display data from a simple javascript array using AngularJS.
Following on from that this next article will show you how to filter that same array by particular properties.
In the previous article I used the following HTML markup with AngularJS to loop through an array of users to display their Name and Location:
&amp;lt;div ng-app=&amp;#34;myApp&amp;#34; ng-controller=&amp;#34;myController&amp;#34;&amp;gt; &amp;lt;ul&amp;gt; &amp;lt;li ng-repeat=&amp;#34;user in users&amp;#34;&amp;gt;{{user.Name}} | {{user.</description></item><item><title>Setting up a simple AngularJS app to display an array</title><link>https://hugo.curtiscode.dev/post/angularjs/setting-up-a-simple-angularjs-app-to-display-an-array/</link><pubDate>Tue, 02 Jun 2015 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/angularjs/setting-up-a-simple-angularjs-app-to-display-an-array/</guid><description>AngularJS is a powerful Single-Page-Application javascript framework created by Google.
The following is a simple tutorial on how to set up an AngularJS application to display a javascript array of user information.
First of all we&amp;rsquo;ll start by creating our javascript array. The following is an array of user information containing details of Name, Gender and Location.
var userData = [ { Name: &amp;#34;John Smith&amp;#34;, Gender: &amp;#34;M&amp;#34;, Location: &amp;#34;Wales&amp;#34; }, { Name: &amp;#34;Sally Smith&amp;#34;, Gender: &amp;#34;F&amp;#34;, Location: &amp;#34;USA&amp;#34; }, { Name: &amp;#34;Curtis Timson&amp;#34;, Gender: &amp;#34;M&amp;#34;, Location: &amp;#34;England&amp;#34; }, { Name: &amp;#34;Sam Wallace&amp;#34;, Gender: &amp;#34;M&amp;#34;, Location: &amp;#34;England&amp;#34; }, { Name: &amp;#34;Caroline James&amp;#34;, Gender: &amp;#34;F&amp;#34;, Location: &amp;#34;Scotland&amp;#34; } ]; Next we&amp;rsquo;ll set up the HTML application container required:</description></item><item><title>Preventing console.log issues in IE</title><link>https://hugo.curtiscode.dev/post/js/preventing-console-log-issues-in-ie/</link><pubDate>Mon, 27 Oct 2014 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/preventing-console-log-issues-in-ie/</guid><description>In some older browsers, including versions of Internet Explorer, window.console doesn&amp;rsquo;t exist and therefore calling console.log() will result in an error.
One way of fixing this is to create a function which checks whether console exists and use this function instead:
function consolelog(v){ if (window.console &amp;amp;&amp;amp; window.console.log){ window.console.log(v); } } However this requires all developers knowing about this function, and if someone forgets and directly calls console.</description></item><item><title>Round to the nearest decimal</title><link>https://hugo.curtiscode.dev/post/js/round-to-the-nearest-decimal/</link><pubDate>Fri, 10 Oct 2014 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/round-to-the-nearest-decimal/</guid><description>If we need round to the nearest whole number, we can use the Math.round() function to return this:
Math.round(1.34234); //returns 1 However what if we need to round to a decimal place? For example 0.5.
The following function can be used to do just that, and the logic itself can be adapted to be used for other languages too (C#, SQL etc).
function roundToMultiple(value, multiple){ var t = (1/multiple); return (Math.</description></item><item><title>Mixing Revealing Module and Singleton Javascript Patterns</title><link>https://hugo.curtiscode.dev/post/js/mixing-revealing-module-and-singleton-javascript-patterns/</link><pubDate>Thu, 09 Oct 2014 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/mixing-revealing-module-and-singleton-javascript-patterns/</guid><description>Until recently I&amp;rsquo;ve been using the Singleton pattern for my javascript objects.
This works well as my functions are namespaced and therefore the risk of clashing object names across Javascript files is reduced.
For this example I&amp;rsquo;ve created a singleton pattern javascript object which will show an alert window displaying &amp;ldquo;hello world&amp;rdquo;.
var MyFunction = { Init: function(){ this.Config.foo = &amp;#34;hello world&amp;#34;; }, Config:{ foo:null }, ShowAlert:function(){ alert(this.</description></item><item><title>Create a flashing tab notification page title</title><link>https://hugo.curtiscode.dev/post/js/create-a-flashing-tab-notification-page-title/</link><pubDate>Wed, 04 Jun 2014 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/create-a-flashing-tab-notification-page-title/</guid><description>Page title notifications switch between the default page title and a notification message continously in order to grab the user&amp;rsquo;s attention. This is commonly used with chat applications.
I&amp;rsquo;ve written a small javascript object which can be used to switch on and off page title notifications.
To activate the page title notification call the following:
pageTitleNotification.on(&amp;#34;New Message!&amp;#34;); Then call the following to turn it off:
pageTitleNotification.off() The default speed is 1000 milliseconds, but this can be customised by passing a 2nd parameter to the on() function.</description></item><item><title>Namespacing jQuery event handlers</title><link>https://hugo.curtiscode.dev/post/jquery/namespacing-jquery-event-handlers/</link><pubDate>Mon, 05 May 2014 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/jquery/namespacing-jquery-event-handlers/</guid><description>If we are to attach 2 or more event handlers to an element these will become stacked and run linear in the order they were attached.
For example:
$(&amp;#34;.foo&amp;#34;).on(&amp;#34;click&amp;#34;, function(e){ console.log(&amp;#34;bar&amp;#34;); }); $(&amp;#34;.foo&amp;#34;).on(&amp;#34;click&amp;#34;, function(e){ console.log(&amp;#34;baz&amp;#34;); }); If .foo is then clicked, the browser console will log:
&amp;#34;bar&amp;#34; &amp;#34;baz&amp;#34; If we wish to remove these event handlers we can call:
$(&amp;#34;.foo&amp;#34;).off(&amp;#34;click&amp;#34;); Simple.
However, what if we wish to only remove 1 of these event handlers and retain the other?</description></item><item><title>Default negative variables to zero in javascript</title><link>https://hugo.curtiscode.dev/post/js/default-negative-variables-to-zero-in-javascript/</link><pubDate>Wed, 30 Apr 2014 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/default-negative-variables-to-zero-in-javascript/</guid><description>If you need to default a value to 0 if its negative, you could do:
var i = -45; if (i&amp;lt;0){ i = 0; } console.log(i); //0 However, a shorter way of doing this would be to use Math.max() passing 0 as one of the parameters:
var i = -45; i = Math.max(0,i); console.log(i); //0 Likewise Math.min() can be used to set a maximum value:
var i = 999; i = Math.</description></item><item><title>console.log() vs alert() when debugging</title><link>https://hugo.curtiscode.dev/post/js/console-log-vs-alert-when-debugging/</link><pubDate>Thu, 28 Mar 2013 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/console-log-vs-alert-when-debugging/</guid><description>Using console.log() is a great way of getting quick results about what variables are holding what values at which point, and which areas of code are being called, without having to debug.
Before javascript debugging became so accessible, one of the only ways of checking the values of your variables would be to call alert(). However, this wasn’t ideal as it would stop the script from running while the alert() was displayed, preventing the script from continuing its usual process.</description></item><item><title>Reading URL hashtag values</title><link>https://hugo.curtiscode.dev/post/js/reading-url-hashtag-values/</link><pubDate>Sun, 27 Jan 2013 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/reading-url-hashtag-values/</guid><description>URL hashtags were primarily used in order to position the page scroll on load to a particular element.
With javascript we can access what the value of the hashtag is on page load.
if (window.location.hash){ var hash = window.location.hash.substring(1); alert(&amp;#34;hashtag value is &amp;#39;&amp;#34; + hash + &amp;#34;&amp;#39;&amp;#34;); } This snippet first checks that the URL contains a hashtag, and then uses substring() to remove the hash (#) character from the start of the string.</description></item><item><title>Check javascript function exists before calling it</title><link>https://hugo.curtiscode.dev/post/js/check-javascript-function-exists-before-calling-it/</link><pubDate>Fri, 23 Nov 2012 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/js/check-javascript-function-exists-before-calling-it/</guid><description>On large applications, there may be some instances where you need to make a call to a function which belongs in a different javascript file.
There might also be a case where this javascript file isn’t always included on every page, and should only be called if it is included.
If we are to call this function, and it hasn&amp;rsquo;t been included, a javascript error will be returned.
Therefore we need to first check that the functions exists in the current context.</description></item><item><title>Using jQuery callback functions</title><link>https://hugo.curtiscode.dev/post/jquery/using-jquery-callback-functions/</link><pubDate>Sun, 05 Aug 2012 00:00:00 +0000</pubDate><guid>https://hugo.curtiscode.dev/post/jquery/using-jquery-callback-functions/</guid><description>I’ve heard questions in the past regarding how to run a script after a jQuery animation or event has been completed.
The majority of these believe that you need to delay the script for a few seconds, allowing the animation to complete before the 2nd function is called. For example:
$foo.load(&amp;#34;MyWebPage.html&amp;#34;); $foo.delay(1000).css(&amp;#34;color&amp;#34;, &amp;#34;red&amp;#34;); Here, the jQuery object $foo is being loaded with a web page’s content, then while this is happening a delay of 1000 milliseconds is set, before the css colour is changed to red.</description></item></channel></rss>