Skip to content

Instantly share code, notes, and snippets.

View allenhwkim's full-sized avatar

Allen Kim allenhwkim

View GitHub Profile
/**
given a non-empty array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.
For example, given array A such that:
A[0] = 4
A[1] = 2
A[2] = 2
A[3] = 5
A[4] = 1
@allenhwkim
allenhwkim / Codility Training - frog-river-one.js
Last active July 3, 2022 21:09
Codility Developer Training
/**
given a non-empty array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river.
If the frog is never able to jump to the other side of the river, the function should return −1.
For example, given X = 5 and array A such that:
[1, 3, 1, 4, 2, 3, 5, 4]
the function should return 6, as explained above.
function solution(N) {
const binString = N.toString(2);
const matches = binString.replace(/[0]+$/, '').match(/0+/g);
return (matches || []).reduce( (acc, cur) => Math.max(acc, cur.length), 0);
}
@allenhwkim
allenhwkim / release.yml
Last active May 18, 2022 20:43
semantic-release auto publish
name: Release
"on":
push:
branches:
- main
jobs:
release:
name: release
runs-on: ubuntu-latest
@allenhwkim
allenhwkim / restrict-keyboard-input.js
Created May 16, 2022 20:41
restrict Keyboard Input
$('#my-input').addEventListener('keydown', restrictKeyboardInput);
$('#my-input').addEventListener('paste', restrictKeyboardInput);
function restrictKeyboardInput(event) {
if (event.type === 'keydown' && !event.key.match(/^[a-zA-Z0-9-_\ .]$/)) {
if (event.keyCode === 8 || event.keyCode === 46 || event.keyCode === 9) {
// Allow backspace, delete and Tab key
} else if (event.keyCode >= 37 && event.keyCode <= 40) {
// Allow left, right, up and down arrows
} else {
@allenhwkim
allenhwkim / lazy-loading.module.js
Created February 4, 2022 22:15
AngularJs Lazy Loading
angular.module('lazyLoading',[])
.component('childAngularComponent', ChildAngularComponent)
.component('childChildAngularComponent', ChildChildAngularComponent);
var ChildAngularComponent = {
template: `
<div>
<h2>I'm Child Angular Component</h2>
<p>form.value: {{$ctrl.form.value}}</p>
@allenhwkim
allenhwkim / konsole.js
Created October 7, 2021 18:51
konsole, console wrapper with log level
export const konsole = {
LOG_LEVEL: 'log',
LOG_LEVELS:
{ all: 0, debug: 1, log: 2, info: 3, warn: 4, error: 5, none: 6 },
debug: function() { konsole.write('debug', arguments) },
info: function() { konsole.write('info', arguments) },
log: function() { konsole.write('log', arguments) },
warn: function() { konsole.write('warn', arguments) },
error: function() { konsole.write('error', arguments) },
@allenhwkim
allenhwkim / text-editable.directive.ts
Created September 30, 2021 21:37
Angular text-editable directive
import { Component, ElementRef, HostListener, forwardRef, AfterViewInit } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: '[text-editable]',
template: '<ng-content></ng-content>',
providers: [ {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TextEditableComponent), multi: true
@allenhwkim
allenhwkim / fire-dom-change.js
Created September 20, 2021 02:10
fireDOMChange(el) and waitForIdle(el)
function fireDOMChange(el, options={attributes: false, childList: true, subtree: true}) {
const observer = new MutationObserver(list => el.dispatchEvent(new CustomEvent('dom-change', {detail: list})));
observer.observe(el, options);
}
function waitForIdle(el, options) {
options = Object.assign({
wait: 100, attributes: false, childList: true, subtree: true
}, options);
return new Promise(resolve => {
@allenhwkim
allenhwkim / del-remote-stale-branches.js
Last active September 15, 2021 03:26
Delete all remote stale branches
#!/usr/bin/env node
console.log(`*************************************************************************
* This script will
* 1. Clean up local branches after merge and deleted (git remote prune origin)
* 2. Ask you to delete remote your stale branches that are
* - more than 3 months old
* - and, created by you
* To delete all users' stale branches, add --all at the end
************************************************************************`);
const execSync = require('child_process').execSync;