Saturday, August 20, 2016

Console magic

You can detect duplicate ids by evaluating this Javascript expresssion in Chrome devtools console:

([].slice.call(document.querySelectorAll("[id]")))
    .map(function(ele){ return ele.id;})
    .sort()
    .reduce(function(countMap, word) {countMap[word] = ++countMap[word] || 1;return countMap}, {})

Boom! You will get:

> Object { 
   id1: 1, 
   id2: 1,
   some-other-id: 1,
   duplicate-id: 2
   …
}

What is going on here:

  • document.querySelectorAll() returns the NodeList of elements that have id attribute
  • slice() - converts a NodeList to a JavaScript Array
  • map() - converts elements array to array of id strings
  • sort() - sorts the array. If you evaluate only up to here you will get the sorted list of ids (with duplicates)
  • reduce() - creates an object with id value as key and number of occurrences as value

Additionally, if you add Object.keys(resultFromAbove).sort() you will get a sorted list of unique ids.

Also an example of Map-Reduce 

BTW you can use similar, slightly modified selector expression to list the CSS classes that are used on the page and also mow many times.

Monday, August 15, 2016

Appear on hover

Recently I needed a way to have controls for resizing appear on hover using pure CSS. I wanted allow progressive discovery of the feature to collapse the panels but did not want have a clutter of controls. I also wanted that once the user learns about the new feature they can directly access it by moving their mouse there and clicking.

I did it using :nth-child(1), :nth-child(2), and :hover pseudo classes. For the pair on left hand side, the key idea is that, I had to use display:inline-block; on the container span and float: left; style on :nth-child(2)to get around the CSS limitation of not being able to use a following sibling in markup order to affect the style of preceding sibling.

Do you have better suggestions?

Saturday, August 13, 2016

A simple gradle plugin to print the task execution graph of a gradle build.

Here is a gist for a simple gradle plugin implementation that can generate an HTML report of your gradle build's Task Execution Graph.

Here is a sample output for a simple project:

↦ :clean - Deletes the build directory.
↦ :build - Assembles and tests this project.
   ┖ :assemble - Assembles the outputs of this project.
      ┖ :jar - Assembles a jar archive containing the main classes.
         ┖ :classes - Assembles main classes.
            ┖ :compileJava - Compiles main Java source. ⊣
            ┖ :processResources - Processes main resources. ⊣
   ┖ :check - Runs all checks.
      ┖ :test - Runs the unit tests.
         ┖ :classes - Assembles main classes. ↑
         ┖ :testClasses - Assembles test classes.
            ┖ :compileTestJava - Compiles test Java source.
               ┖ :classes - Assembles main classes. ↑
            ┖ :processTestResources - Processes test resources. ⊣

To use it simply apply it in your build.gradle like so:

:
:
apply from: 'build-task-graph.gradle'
:
:

And then run any build like this;

> .\gradlew -m clean build

and find the output report in:

build/reports/printteg.html


It should be a simple exercise to generate hyperlinked output or alternatively emit a .dot file graph or even generate SVG.

I use this to determine which tasks I should exclude using the -x flag and only (carefully) run the ones I need. Of course you have to know what you are doing. The idea is similar to my Maven Phases and Goals plugin.

Feedback welcome!

Tuesday, August 09, 2016

Angular 2 - template reference variable shadows the component property

Given the Angular 2 component:

import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  template: '<div ref-title>{{title}}</div>',
  styleUrls: ['app.component.css']
})
export class AppComponent {
  title =  'app works!';
}

you get this on the webpage:

[object HTMLDivElement]

The reason is that Template Reference Variable ref-title shadows the title of  AppComponent when evaluating {{title}}.

I guess this is sort of like the loop variables inside an

<li *ngFor="let title in titles">{{title}}</li>

But something does not feel right about TRV.

Sunday, August 07, 2016

A simple Weather App using Angular 2, Angular Material 2 and Electron

Working on a simple Weather App using

Also runs as a standard web app.

Built using



Will document the details soon!



Sunday, June 05, 2016

Weekend Hack

I recently acquired a tripod with Bluetooth remote for my smart phone. The tripod  is only up to 10 inches tall and works very well indoors when you have something to perch it on.


However I wanted to a taller, up to 5 feet tall monopod, which I can use to video record my son's outdoor or theater concerts. I hacked it with some material that I could find in the garage:

  • L bracket as the base
  • conduit clip and knob to hold the PVC pipe


  • PVC pipe
  • velcro to firmly hold the tripod



Here is the result



Do you have any hacking ideas you would like to share?

Thursday, June 02, 2016

Try this repeatedly in your browser console...

var party = Math.random(), country = Math.random(), what = (party >= country ? 64783292855 : 2569542479746150); 'party=' + party + ' country = ' + country + ' party/county = ' + what.toString(36);

Friday, May 06, 2016

Try this in your browser console...

var party = 64783292855, country = 1; alert((party/country).toString(36));

Thinking.oO { Party/Country != Patriotism , Party/Country == Treason }

var party = 0;
var country = 1;
var result = [ 'Treason', 'Patriotism' ];

party/county =

Monday, April 25, 2016

Electronic eyes


Electronic eyes

Fun!

Sunday, April 24, 2016

Electron based POM360


A simple POM360 tool to get a 360 view of Maven POM. It is developed using Electron framework. It uses mvn command.

Saturday, April 23, 2016

Electron based Portmon tool


A simple Portmon tool to monitor listening TCP ports and associated PIDs. It is developed using Electron framework. It uses netstat.

NOTE: Works on windows only at the moment.



Monday, December 28, 2015

Print angular module dependencies

Here is a simple function to print angular module dependencies:

    angular.__moduleDependencies__ = function (moduleName, indent, nonlast, seen) {
        var module, suffix;
        indent = indent || '';
        seen = seen || [];
        suffix = (indent === '' ? '' : (nonlast ? '├─ ' : '└─ '));
        if (seen.indexOf(moduleName) !== -1) {
            console.log(indent + suffix + moduleName + ' ^');
            return;
        }
        seen.push(moduleName);
        module = angular.module(moduleName);
        if (angular.isDefined(module)) {
            console.log(indent + suffix + moduleName);
            angular.forEach(module.requires, function(requiredModuleName, key){
                var requiredMod = angular.module(requiredModuleName);
                if (angular.isDefined(requiredMod)) {
                    
                    angular.__moduleDependencies__(requiredModuleName,
                        indent + '| ', key < (module.requires.length - 1), seen);
                } else {
                    console.error(indent + requiredModuleName);
                }
            });
        } else {
            console.error(indent + suffix + moduleName);
        }
    };

Usage

angular.__moduleDependencies__('todomvc')


Output

todomvc
 | ├─ ngRoute
 | | └─ ng
 | | | └─ ngLocale
 | └─ ngResource
 | | └─ ng ^

Sunday, December 27, 2015

Filed Eclipse enhancement

Many time I copy a file and the only difference is that I need to map a noun to another.e.g.

firstName -> lastName

getFirstName() -> getLastName()

FIRSTNAME -> LASTNAME

and even better:

FIRST_NAME -> LAST_NAME

FIRST-NAME -> LAST-NAME

This is supported by emacs to some extent. See below.

The Eclipse find/replace dialog should support this using a Case mapping chekbox option like this:

__ Options ____________________

[ ] Case sensitive

    [X] Case mapping

Some languages use camel case, some use hyphen or underscore. The dialog could take that into account.

Support Emacs style case matching replace behavior.

"
15.10.3 Replace Commands and Case
:
:
In addition, when the newstring argument is all or partly lower case, replacement commands try to preserve the case pattern of each occurrence. Thus, the command

M-x replace-string RET foo RET bar RET

replaces a lower case ‘foo’ with a lower case ‘bar’, an all-caps ‘FOO’ with ‘BAR’, and a capitalized ‘Foo’ with ‘Bar’. (These three alternatives—lower case, all caps, and capitalized, are the only ones that replace-string can distinguish.)

If upper-case letters are used in the replacement string, they remain upper case every time that text is inserted. If upper-case letters are used in the first argument, the second argument is always substituted exactly as given, with no case conversion. Likewise, if either case-replace or case-fold-search is set to nil, replacement is done without case conversion.
"

REF: http://www.gnu.org/software/emacs/manual/html_node/emacs/Replacement-and-Case.html#Replacement-and-Case

Sunday, December 13, 2015

TIP: Track window innerWidth using AngularJS

Here is a simple Plumkr to track the window innerWidth using AngularJS:

Try it