Skip to content

Instantly share code, notes, and snippets.

View niksumeiko's full-sized avatar

Nik Sumeiko niksumeiko

View GitHub Profile
@niksumeiko
niksumeiko / catching-promise-errors.js
Last active July 15, 2017 13:32
Catching errors thrown inside Nodejs promise
'use strict';
var Promise = require('promise');
/** @desc A function that returns a promise with an error-prone code. */
function build() {
return new Promise(function(fulfill, reject) {
@niksumeiko
niksumeiko / object-to-style-attribute.js
Created June 30, 2015 08:32
Converts style object into the corresponding CSS `style` attribute value
/**
* Takes a style object and returns the corresponding
* attribute value. Converts camel case property names
* to proper CSS selector names.
* @param {Object} obj Map of CSS properties to values.
* @return {string} The style attribute value.
*/
function toStyleAttribute = function(obj) {
return Object.keys(obj).map(function(key) {
@niksumeiko
niksumeiko / jquery.scrollDirection.js
Created July 14, 2013 09:03
jQuery function that identifies page scrolling direction. Used when it's needed to apply different functionality for different (up/down) scrolling directions.
// Variable that is going to hold previous 'document' scrollTop
// value (/vertical scrollbars position).
var prevScrollTop;
// Function that returns 'true' (/boolen) if user scrolls the
// page up, 'false' (/boolen) if user scrolls the page down.
function scrollsUp(scrollTop) {
var before = prevScrollTop;
@niksumeiko
niksumeiko / mac.emptyTrash
Created June 30, 2013 10:16
Mac Terminal command that securely removes files from Trash using incredibly secure 35-pass method. That basically means that first the data is removed, then written over 35 times using randomly generated patterns, making recovery quite literally impossible.
##
# Command to empty Trash with 35-pass security method. This is the most secure deletion,
# so you will not be able to recover any file deleted with this method.
# Replace <MAC_USER> with your Mac username.
##
srm -rfv /Users/<MAC_USER>/.Trash/*
##
# Command to empty Trash with 7-pass security method that meets the
@niksumeiko
niksumeiko / sendToNative.ts
Created May 16, 2017 10:15
Sending data from webView to native Android/iOS layer in hybrid mobile app
function sendToNative(data: string) {
if (/android/i.test(window.navigator.userAgent)) {
if (window.quicketNative) {
return window.quicketNative.send(data);
}
}
let frame = document.createElement('iframe');
frame.width = '100';
@niksumeiko
niksumeiko / utils.getLocalStorageUsedSpace.js
Last active December 24, 2015 15:49
JavaScript function that calculates allocated localStorage memory. Also applicable to get sessionStorage used memory.
/**
* Returns localStorage (or its particular key) allocated memory
* in Megabytes (MB).
* @param {string=} opt_key inside the storage to calculate
* used space for.
* @return {number} with 2 decimal points.
*/
function getLocalStorageUsedSpace(opt_key) {
var allocatedMemory = 0,
@niksumeiko
niksumeiko / laravel.controllers.users.php
Last active December 22, 2015 06:49
Demonstration of PHP Laravel framework Response class methods that sets custom HTTP status codes in your Controller response. Especially handy when wish to manage your REST API error responses.
<?php
class Users_Controller extends Base_Controller {
public function action_index() {
$id = Input::get('id');
if ($id) {
$user = DB::table('users')->where('id', $id)->first();
@niksumeiko
niksumeiko / utils.getUNIXTimestamp.js
Last active December 18, 2015 15:49
JavaScript function that returns current UNIX timestamp (eg 1371570826).
// Returns UNIX timestamp.
// Sometimes visible in different JavaScript developers teams as now().
function getUNIXTimestamp() {
return Math.round((new Date()).getTime() / 1000);
}
@niksumeiko
niksumeiko / utils.getRandomInteger.js
Last active December 18, 2015 15:48
JavaScript function that returns a random integer within specified range of integers (eg from 0 to 167).
// Returns a random integer between min and max.
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
@niksumeiko
niksumeiko / git.stash
Created June 17, 2013 18:43
Use 'git stash [, pop]' to commit to the proper GIT branch when discovered that you have made local changes in an inappropriate branch.
# Sometimes we discover that local changes we have made in our GIT project
# are not related to the branch we currently work in.
# So we need to switch the propoer branch before committing the changes.
# 'git stash [, pop]' is going to solve the problem easily.
# 1. While you have uncommitted changes, 'stash' them to create a temporary
# commit of the current state of the working copy (both cached and
# uncached files) and to revert the working copy to the current HEAD:
git stash