Random programming things I'd want to remember

Wednesday, May 2, 2018

Fixing "Read More" link to be accessible to screen readers


Just a reminder for myself, to make links accessible to screenreaders, use the following template or a variation of thereof:
    <a href="..."
      <span data-bind="text: regularLinkText">
      <span data-bind="text: accessibleLinkText" class="sr-only"> <!-- this will be the text for screen reader and not visible to naked eye -->
    </a> 
The problem I kept having was that I set the data-bind="text: linkTextHere" on the actual <a> tag, and that wiped out whatever other controls I nested inside.

Wednesday, April 18, 2018

Skip a link while tabbing over a web page or access a link via tab

One way of doing it is to remove the href element of the link.

In contrast, if a link cannot be reached to while tabbing over a page, adding an

    href="javascript:void(0);" 
would make the link reachable via tabbing. Moreover, if a 'click' event is attached to the link, the element will respond to the Enter key press despite the fact that the 'keyup' (or other key type event) is not attached.


Wednesday, January 17, 2018

Visual Studio snippet for a TestMethod with a proper naming convention (naming convention was proposed by Roy Osherove)

Here it is, see this post for details on how to import it into your Visual Studio. Enjoy!

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Test method with a naming convention</Title>
      <Author>Roy Osherove, yevgeller</Author>
      <Description>Test method with the following naming convention: method under testing, condition, and expected results. The naming convention was taken from Roy Osherove's book "The Art of Unit Testing"</Description>
      <Shortcut>testmc</Shortcut>
    </Header>
    <Snippet>
      <Imports>
        <Import>
          <Namespace>
            Microsoft.VisualStudio.TestTools.UnitTesting;
          </Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>NameOfMethodUnderTest</ID>
          <ToolTip>Replace with the method that you want to test</ToolTip>
          <Default>YourMethod</Default>
        </Literal>
        <Literal>
          <ID>ConditionYouAreTesting</ID>
          <ToolTip>Condition that you want to test</ToolTip>
          <Default>IsNull</Default>
        </Literal>
        <Literal>
          <ID>ExpectedResult</ID>
          <ToolTip>The result that you expect</ToolTip>
          <Default>NullArgumentException</Default>
        </Literal>
      </Declarations>
        <Code Language="csharp">
          <![CDATA[          
          [TestMethod]
          public void $NameOfMethodUnderTest$_$ConditionYouAreTesting$_$ExpectedResult$()
          {

          }
          ]]>
        </Code>
      </Snippet>
  </CodeSnippet>
</CodeSnippets>



Thursday, January 11, 2018

Timer to count up in JavaScript

Here is my implementation, nothing original or extraordinary. Putting it here for future reference.
        
        var totalSeconds = 0;
        var timer;

        function startTimer() {
            timer = setInterval(function () {
                totalSeconds++;
                var t = document.getElementById("time2"); //or other place where to display data
                t.innerHTML = displayTimer(totalSeconds);
            }, 1000); 
        }

        function stopTimer() {
            clearInterval(timer);
        }

        function displayTimer(numberOfSeconds) {
            var result = "";

            var hours = Math.floor(numberOfSeconds / 3600);
            if (hours > 0) { result += hours + " h "; }

            var minutes = Math.floor((numberOfSeconds - (hours * 3600)) / 60);
            if (minutes > 0) { result += minutes + " m "; }

            result += (numberOfSeconds % 60 + " s");

            return result;
        }

Tuesday, January 9, 2018

To remember: declaring an inline array for splitting strings

This is how to declare an inline char array (C#) to split the string into multiple parts:

List result = inputLine.Split(new[] {'%'}, StringSplitOptions.RemoveEmptyEntries).ToList();