Saturday, March 28, 2015

js quickie: random pastels

I found a page with some too clever by half random hex color generators but I know from experience that forcing things to be pastels leads to less of a "angry fruit salad" effect, so here's my modification of their code to constrain it to pastels:

function randomPastel(){
    return '#' + (function co(lor){   return (lor += ['a','b','c','d','e','f'][Math.floor(Math.random()*6)])&& (lor.length == 6) ?  lor : co(lor); })('');

tbh, it's probably not super efficient code (recursion hardly ever is!) and I'm even taken away some of the elegance by wrapping it in a boring-ly named function etc, but hey.

til: jquery $(document).ready shorthand

Oh, huh. Since I've know jQuery, I've used the formulation:

  //do something

but in going over some jQuery UI example, I found out

  //do something

is a more concise replacement, that also waits for the DOM to be loaded.

Here's a pretty decent summary page including some other options.

Sunday, March 22, 2015

the joy of hack: diy "jsonp-ishness"

For a side project I've been working on, the library relies on a series of static JSON files to describe parts of the layout. Unfortunately, that part of the project wasn't working when put on the client's webserver. Investigating, I saw that my browser could successfully request the image files in the same folder, but the JSON was all 404'ing.

I asked for a screenshot of the folder listing, and the JSON files were present but listed as type "unknown". This was some kind of custom webserver, so I'm guessing it was configured to ignore filetypes it didn't know about --  including JSON, apparently.

The fix was straightforward-ish; hack the JSON loader for this set of files so it pulled value from a map hard-coded in the script itself. It was a sparsely populated map, only 4 or so of the 100+ pages had JSON that was anything but the empty array [], so the hack function returns an empty array if it couldn't find a value in the map, rather than me having to populate the map with 100 blank entries.

So for this arrangement, this technique was probably more efficient anyway. For that matter, this meant I could now pull up the project's index.html from the local filesystem and have it all work, which wasn't the case when the files were in external JSON.

So a win all around. Nothing too ground-breaking, just a kind of poor man's adhoc retelling of JSONP...

Friday, March 20, 2015

Thursday, March 19, 2015

qa joke!

"QA Engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 999999999 beers. Orders a lizard. Orders -1 beers. Orders a sfdeljknesv."

Wednesday, March 18, 2015

graph paper, free if you print at work Today I realized that if one has a need for a small amount of graph paper, and one has access to the Internet and a stocked printer, one no longer has a need for a small amount of graph paper.

Tuesday, March 17, 2015

handlebars 101

I really like the concept of handlebars (in terms of the library vs framework debate, and decoupling the "V" and "C" out of "MVC", unlike other heavier tools) but loops weren't as obvious. In older versions of Handlebars you had to write helpers, but like this stackoverflow describes now it's as easy as
{{#each myArray}}
    Index: {{@index}} Value = {{this}}
{{#each myObject}}
    Key: {{@key}} Value = {{this}}
and of course you can replace "myArray" or "myObject"with "." if you are passing in a simple array or object.