Skip to content

Instantly share code, notes, and snippets.

@MJ111
MJ111 / disableBtnDuringAjax.js
Last active January 11, 2018 03:48
[Disable Button during Ajax] #javascript
/**
* disable clicked button before jQuery ajax request and enable after ajax request finished
* @param {Function} ajaxFn
* ajaxFn should return executed jQuery AJAX function
* @returns {Function}
* returned function should be placed in event listener of Element
*
* Usage:
* $('#deactivate-btn').click(disableBtnDuringAjax(function(e) {
* return $.get('/api/users', function (data) {
@MJ111
MJ111 / getQueryParam.js
Last active January 10, 2018 04:35
[get query param] #javascript
/**
* @param {string} name
* @returns {string|null}
* getQueryParam('a') returns '1' on page http://domain.com/page.html?a=1&b=2
**/
function getQueryParam(name) {
const q = window.location.search.match(
new RegExp('[?&]' + name + '=([^&#]*)')
);
return q && q[1];
@MJ111
MJ111 / manhattan_distance.cpp
Last active October 4, 2017 19:01
Round #51
// https://csacademy.com/contest/round-51/task/manhattan-distances/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int T, a, b, c, h;
cin >> T;
@MJ111
MJ111 / checksquare.cpp
Last active October 2, 2017 19:04
csacademy round#50
// can try http://mathworld.wolfram.com/ConvexHull.html, but overkill..
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int Dist(pair<int, int> a, pair<int, int> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
@MJ111
MJ111 / max_substring_solution.cpp
Last active September 20, 2017 18:17
Max Substring
/*
You are given a string S. Find a string T that has the most number of occurrences as a substring in S.
If the solution is not unique, you should find the one with maximum length. If the solution is still not unique, find the smallest lexicographical one.
cabdab
ab
cabcabc
c
@MJ111
MJ111 / handleScroll.js
Created August 17, 2017 08:02
Check if the element is in the middle of window at scroll event
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
function handleScroll() {
const position = document.getElementById('content').getBoundingClientRect()
// check if the element is in the middle of viewport(window)
if (position.top < window.innerHeight/2 && position.bottom > 0 && position.bottom > window.innerHeight/2) {
// do what you want to do
}
}
@MJ111
MJ111 / webpackDevServerIssue.md
Last active August 9, 2017 12:28
can't resolve 'tls' 'fs', ... when running webpack-dev-server issue

Don't pass file name as argument

$ node_modules/webpack-dev-server/bin/webpack-dev-server.js webpack.config.js

This will raises errors like this.

    ERROR in ./~/tunnel-agent/index.js
    Module not found: Error: Can't resolve 'net' in '/Users/projects/frontend/node_modules/tunnel-agent'
     @ ./~/tunnel-agent/index.js 3:10-24
     @ ./~/request/lib/tunnel.js

@ ./~/request/request.js

@MJ111
MJ111 / removeBOM.sh
Last active August 9, 2017 06:10 — forked from akost/convert.sh
Bash script for recursive utf-8 byte order mark removal
#!/bin/bash
# Recursive utf-8 byte order mark removal
# https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding
# Place this file in the root of your site, add execute permission and run
# Converts *.php, *.html, *.css, *.js files.
# To add file type by extension, e.g. *.cgi, add '-o -name "*.cgi"' to the find command
find ./ -name "*.php" -o -name "*.html" -o -name "*.css" -o -name "*.js" -o -name "*.sass" -type f |
while read file
@MJ111
MJ111 / removeBrokenSurrogate.js
Created August 1, 2017 03:55
[Remove divided surrogate pair unicode] #JavaScript #Unicode
/*
* This would be necessary If you have limited length textarea and sliced the input string.
* because individual surrogate codes are useless and raise error in Python and broken character is ugly
* (https://unicode-table.com/en/blocks/high-surrogates/)
*/
function SliceAndCleanInputStr (inputStr) { // inputStr = 'aaaaaa😄'
var sliced = inputStrstr.slice(0, 7) // "aaaaaa�"
return sliced.replace(/[\ud800-\udbff]/g, '') // "aaaaaa"
}
@MJ111
MJ111 / countdown.js
Created July 20, 2017 06:25
Calculate milliseconds between midnight and present time according to universal time
const now = new Date();
const utcnow = Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCMilliseconds()
);
let utctmr = Date.UTC(