Skip to content

Instantly share code, notes, and snippets.

View motss's full-sized avatar
🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!

The web walker motss

🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!
View GitHub Profile
@motss
motss / Mapping a clock angle onto screen.js
Last active January 11, 2016 17:13
Mapping clock angle using Math.atan2
/**
* 1 - Start off everything from the origin (0, 0) just like how to draw something on a graph in Mathematics
* where the Cartesian Plane has a +ve and -ve x- and y- axes and the circle is drawn on it by having a
* centroid at the origin (0, 0);
* 2 - Do the same thing for finding an angle from a coordinate as you know a circle has 360 degree, however
* the method to find a particular angle from the coordiante you have chosen is kind of not doing what
* you would have expected. Say, the method you use to find the angle is by using Math.atan2, it actually
* returns a computed angle from the X axis and the range of the returned value falls between -pi and +pi.
*/
/**
@motss
motss / Javascript Floating Point Precision Issue.js
Created November 23, 2015 07:57
Javascript Floating Point Precision Issue
// Example 1.
var firstFloatingPointNumber = 1.0;
var secondFloatingPointNumber = 0.7;
var result = firstFloatingPointNumber - secondFloatingPointNumber; // 0.30000000000000004, instead of 0.3.
// To fix that precision issue.
result = (firstFloatingPointNumber - secondFloatingPointNumber)/ 10; // 0.3
@motss
motss / nginx + http2 + http-https redirect.conf
Last active December 13, 2015 16:43
config for setting up http2 + http-https redirect for NginX (version 1.9.7) with OpenSSL (1.0.1f).
upstream secured {
server 0.0.0.0:8443;
}
server {
listen 80;
server_name example.com www.example.com;
location / {
add_header Cache-Control public;
@motss
motss / getWeek.js
Last active February 1, 2016 17:04
getWeek
// compute week number based on date and first day of the week is Sunday.
// Eg. Let _date = '2015-11-12',
// Ref 1 for week number starting from Monday: http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2020
// Ref 2 for week number starting from Sunday: http://www.timeanddate.com/calendar/custom.html?year=2026&country=69&fdow=7&wno=4&df=1
// To get week numbers for calendars starting from Sunday (US calendar system).
function getWeek(_date) {
let now = new Date(_date); // new Date('2015-11-12');
now = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 4); // FIrst 4-day week of a year.
let onejan = new Date(now.getFullYear(), 0, 1);
@motss
motss / es2015-promise.js
Created December 18, 2015 16:05
ES2015 Promise
var dates = [1, 2, 3];
var done = dates.map(function (date) {
return new Promise(function (resolve, reject) {
var json = ['a', 'b', 'c'];
if (date > Math.random() * 10) {
setTimeout(reject, Math.random() * 5000, date);
}else {
setTimeout(resolve, Math.random() * 5000, json[Math.ceil(Math.random() * 3) - 1]);
}
});
@motss
motss / test.js
Last active December 22, 2015 16:49
Comparison between getting data from Firebase using sync and async
// PART 1a - Uncomment the following lines if running on Node;
// var _ = require("lodash");
// var Firebase = require('firebase');
// PART 1b - Uncomment the following lines if running on Chrome Dev Tool;
// var script = document.createElement('script');
// script.type = 'text/javascript';
// script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js';
// document.head.appendChild(script);
// var script = document.createElement('script');
@motss
motss / gulpfile.babel.js
Last active August 21, 2016 17:22
Minify Polymer Elements (HTML, CSS and JS) with Gulp to make things fly!
'use strict';
// Process flow: gulp backup --> gulp build --> gulp revert (optional)
// Folder structure in order to make this works:
// |- my-element-icons.html // extracted icons, if any.
// |- my-element.theme.html // external CSS file.
// |- my-element.html // main html.
// '- my-element.js // load JS file externally.
const fs = require('fs');
const gulp = require('gulp');
@motss
motss / assertion-error-log.log
Last active January 23, 2016 09:36
Assertion Error when serving static with Express 4 and node-spdy
spdy:connection:server id=0 frame +249ms { type: 'HEADERS',
id: 105,
priority: { parent: 0, exclusive: true, weight: 183 },
fin: true,
writable: true,
headers:
{ ':method': 'GET',
':authority': 'semafore.motss.koding.io:3000',
':scheme': 'https',
':path': '/bower_components/polymer/polymer-micro.html',
@motss
motss / simple-loading-screen.html
Last active July 20, 2017 18:31
simple loading screen
<!-- Credits to http://codepen.io/mikeambrosi/pen/JdEMmY -->
<html>
<head>
<style>
/* Run on https://autoprefixer.github.io/ to compile CSS */
.loading {
position: absolute;
top: 50%;
left: 50%;
margin: -15px 0 0 -15px;
@motss
motss / fullyearcalendar.js
Created February 5, 2016 08:43
Full year calendar for Firebase
(function () {
try {
var _ = require('lodash');
}catch (err) {
if (err) {
try {
typeof _ === 'function';
}catch (err) {
if (err) {
console.log('script-src');