Skip to content

Instantly share code, notes, and snippets.

Avatar

Noj Vek nojvek

View GitHub Profile
@nojvek
nojvek / format-json.ts
Last active Mar 29, 2020
Json pretty printer that respects maximum line length
View format-json.ts
// @ts-ignore
import units from 'mdn-data/css/units.json';
const widgetSample = {
widget: {
debug: `on`,
window: {
title: `Sample Konfabulator Widget`,
name: `main_window`,
dimensions: {
@nojvek
nojvek / webpack-terser.js
Created Jan 15, 2020
webpack remove unused code options
View webpack-terser.js
const optimizationOptions = {
mode: PRODUCTION ? `production` : `development`,
module: {rules},
optimization: {
chunkIds: `named`,
moduleIds: PRODUCTION ? `hashed` : `named`,
usedExports: true,
minimize: PRODUCTION,
minimizer: [
new TerserPlugin({
@nojvek
nojvek / spa-nav.ts
Created Jan 14, 2020
spa-nav: navigation without page reload
View spa-nav.ts
// change document title to new app
document.title = newApp.title
// change the url without re-loading to the new app's url
if (ev.type !== `popstate`) {
// if we push on a popstate event, we break browser back button navigation, don't do that
window.history.pushState(``, ``, url);
}
// disable old css and add new css if not already loaded
@nojvek
nojvek / PerfMeasure.ts
Created Jan 14, 2020
Add marks and measures to performance timeline
View PerfMeasure.ts
const START = `start`;
const END = `end`;
export class PerfMeasure {
public measureName: string;
public started: boolean;
public ended: boolean;
constructor(measureName: string) {
this.measureName = measureName;
@nojvek
nojvek / side_by_side_urls.js
Created Jan 14, 2020
Loading two urls side by side.
View side_by_side_urls.js
/*
* 1) Install Chrome Split Tabs plugin: https://chrome.google.com/webstore/detail/split-tabs/mamepagkigmnpoalafajabnljlkkocbk
* 2) Run `/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222`
* 3) Load two about:blank tabs side by side when chrome starts
* 4) Start QuickTime and record screen with the two chrome tabs side by side
* 5) Run `node side_by_side_urls.js`, which loads two urls in both tabs simultaneously
*/
const puppeteer = require(`puppeteer`);
const fetch = require(`node-fetch`);
@nojvek
nojvek / webpackJsonp.js
Created Nov 23, 2019
webpackJsonp modules
View webpackJsonp.js
moduleMap = {};
for (const p of webpackJsonp) {
const modules = p[1];
for (const [moduleName, moduleFn] of Object.entries(modules)) {
const moduleContent = moduleFn.toString();
const nameParts = moduleName.split(`!`);
const moduleId = nameParts[nameParts.length - 1] + (nameParts.length > 1 ? `!` : ``);
if (!moduleMap[moduleId]) {
moduleMap[moduleId] = moduleContent
} else if (moduleMap[moduleId] !== moduleContent) {
View words2num.js
const wordsToNumMap = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
@nojvek
nojvek / createElem.js
Created Mar 14, 2019
createEleme - simple dom helper for creating dom nodes
View createElem.js
/**
* A simple helper for creating nested dom tree inspired by snabbdom-jsx syntax
* @param {string} name
* @param {{[attr: string]: any}} attrs
* @param {Array<HTMLElement | string>} children
* @returns {HTMLElement}
*/
export function createElem(name, attrs, children) {
const elem = document.createElement(name);
@nojvek
nojvek / nested_iterator.js
Created Nov 27, 2018
Nested iterator in javascript
View nested_iterator.js
/*
Problem: Write a class that implements the following iterator interface:
bool hasNext()
int next()
The iterator is used to sequentially iterate over a collection. Given an implementation of this iterator MyIterator, its usage can be summarized by the following snippet:
input = [1,2,3]
@nojvek
nojvek / line_counter.js
Created Nov 26, 2018
Count lines by file extension and path and output csv ready to be sql-analyzed
View line_counter.js
/* eslint-env node */
const fs = require(`fs`);
const path = require(`path`);
const filePaths = fs.readFileSync(`${__dirname}/file_list.txt`, `utf-8`).trim().split(`\n`);
console.log(`Total files`, filePaths.length);
for (let i = 0, len = filePaths.length; i < len; ++i) {
const filePath = filePaths[i];
let lines = null;
You can’t perform that action at this time.