refactor javascript. code deduplication.

belongs to #54