Ian Obermiller

Part time hacker, full time dad.

Pretty printing text with even line lengths

The following demo pretty prints the entered text to the desired line width by making the lines as even as possible. The demo uses a dynamic programming algorithm to choose line breaks in such a way that the sum of the squares of the slack is minimized.

(more…)

jQuery Performance: :not() vs .not()

While doing some profiling on Socl, I noticed a particular jQuery selector that was taking an inordinate amount of time to run:

var toTranslate = this.$element.find('.translatable').not('.translated');

The purpose of the code is to find elements that can be translated, but haven’t been already. My suspicion was on the .not() addition, and lo and behold, inlining the clause into the first selector dramatically increased performance:

var toTranslate = this.$element.find('.translatable:not(.translated)');

(more…)

Highlight search terms in C#, Javascript and TypeScript

Following is a function that given a string and a search query will wrap bold tags around search terms that were found in the query:

Example:

text = “The theory of the big bang was proposed in…”
query = “Big Bang Theory”
result = “The <b>theory</b> of the <b>big</b> <b>bang</b> was proposed in…”
result as html = The theory of the big bang was proposed in….
(more…)

TypeScript Syntax Highlighting for WordPress

For my first post on TypeScript, I included many code samples, and found the Javascript highlighting to be lacking since TypeScript introduces a number of new keywords. To remedy, I’ve put together a simple plugin that adds a TypeScript brush to the SyntaxHighlighter Evolved plugin, using this excellent tutorial. Once installed, you can use the new tags to highlight your TypeScript code, either [ts][/ts] or [typescript][/typescript].

Download the "SyntaxHighlighter Evolved: TypeScript Brush" plugin

(more…)

TypeScript Highlights

Microsoft recently released a new, open source language called TypeScript. It is a strict superset of Javascript, and compiles directly to Javascript. The compiler is open source, and written in TypeScript itself. My team at Microsoft has been dogfooding TypeScript for several months now, and I thought I’d share a few of my favorite parts of the language.

(more…)

LINQ for Javascript

While evaluating the different options for bringing LINQ-like operators to JavaScript, I compiled the following list of existing libraries:

I’d love to hear about any others!

MVC error: “Cannot create an abstract class”

While debugging an MVC4 app, I ran into the following error: System.MissingMethodException: Cannot create an abstract class. It was only happening for a single Action, and I wasn’t able to set a breakpoint in that action. After some trial and error, I was able to find the issue in the code:

public virtual ActionResult Login(ActionResult redirectAction = null)
{
    ...
}

The goal was to have an T4MVC style helper for passing in an ActionResult instead of an untyped path. This was a bad idea, as ActionResult is an abstract class. When I hit http://localhost:2995/Account/Login, the MVC framework tried to instantiate ActionResult to fill in the default value for redirectAction. I simply reverted the parameter back to a string, and all was well.

public virtual ActionResult Login(string redirectPath = null)
{
    redirectPath = redirectPath ?? Url.Action(MVC.Home.Index());
    ...
}

Type or namespace does not exist when compiling MVC View

I ran into the following common error when trying to run my ASP.NET MVC 4 website:

d:\temp\2pirqzul.5yz\temp\root\898a687c\8681620c\App_Web_jfuizczv.0.cs(27): error CS0234: The type or namespace name 'Models' does not exist in the namespace 'DealBoxy' (are you missing an assembly reference?)

While normally a straightforward error, this one was confusing, because I was certain that I had referenced the DealBoxy project correctly.

It turns out that DealBoxy was compiling to an exe, and apparently ASP.NET won’t load exes the same way it will dlls. Changing the Output type to Class Library in the Application tab of the project settings did the trick.

Precompile Handlebars Template in .NET using Jurassic

The awesome Handlebars.js templating library, a superset of Mustache, has functions built-in for precompiling a template to a javascript function. Unfortunately, the instructions are only for Node.js. Using the Jurassic javascript compiler for .NET, it is simple to precompile your Handlebars template in C# and serve up the template functions in your javascript:

(more…)

RestSharp Extensions Returning Tasks

In order to use RestSharp with the async/await keywords, the methods must return Task objects. Since this isn’t provided out of the box, some simple wrappers will do the trick:

(more…)

Older Posts »