Blog Spot!


Javascript Custom Error Types, trow catch - exceptions

// Create a new object, that prototypally inherits from the Error constructor
function MyError(message) {
  this.name = 'MyError';
  this.message = message || 'Default Message';
  this.stack = (new Error()).stack;
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;

try {
  throw new MyError();
} catch (e) {
  console.log(e.name);     // 'MyError'
  console.log(e.message);  // 'Default Message'
}

try {
  throw new MyError('custom message');
} catch (e) {
  console.log(e.name);     // 'MyError'
  console.log(e.message);  // 'custom message'
}

developer.mozilla.org

Added on 25.Jan.2016
Tags: js exceptions errors custom

Useful javascript checkers, converters and seekers

... checks:

  • isJson(str)
  • isStr(str)
  • isArr(arr)
  • isObj(obj)
  • isNum(num)
  • isUndefined()
  • isFnx(fx)
  • isSet(variable)

... converters:

  • toBool(data)

... seekers:

  • inArr(needle, arrhaystack)
  • inStr(str, char)
  • getKey(obj, value)
  • setCharAt(str, index, char)
// ...checks

function isJson(str) {
    try {
        return JSON.parse(str);
    } catch (e) {
        return false;
    }
}

function isStr(str) {
    try {
        return str.constructor === String;
    } catch (e) {
        return false;
    } 
}

function isArr(arr) {
    try {
        return arr.constructor === Array;
    } catch (e) {
        return false;
    }
}

function isObj(obj) {
    try {
        return obj.constructor === Object;
    } catch (e) {
        return false;
    } 
}

function isNum(num) {
    return !isNaN(num);
}

function isUndefined(data) {
    return typeof data === "undefined";
}

function isFnx(fx) {
    return typeof fx === 'function';
}

function isSet() {
    var a = arguments,
        l = a.length,
        i = 0,
        undef;

    if (l === 0) {
        throw new Error('Empty isset');
    }

    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false;
        }
        i++;
    }
    return true;
}
// ...converters

function toBool(data) {
    return !!data;
}

/// ...seekers

function inArr(needle, arrhaystack) {
    return (arrhaystack.indexOf(needle) > -1);
}

function inStr(str, char) {
    if (typeof str === 'string') {
        return str.indexOf(char) !== -1;
    }
    return false;
}
function getKey(obj, value){
    for(var key in obj) {
        if(obj[key] == value){
            return key;
        }
    }
    return null;
}

function setCharAt(str, index, char) {
    return str.substr(0, index) + char + str.substr(index + char.length);
};

Added on 20.Jan.2016
Tags: js check validate node function

BerryBoot v2.0 - raspberry pi bootloader

Universal operating system installer.

For people short on SD cards: Berryboot is a simple boot selection screen, allowing you to put multiple Linux distribution on a single SD card.

In addition it allows you to put the operating system files on an external USB hard drive instead of on the SD card itself.

Added on 19.Jan.2016
Tags: linux pi raspberry grub boot

Connection to wifi through linux terminal

Scan

iwlist wlan0 scan

Connect

iwconfig wlan0 essid "<ssid>" key "<pass>"

Added on 16.Jan.2016
Tags: wifi wlan scan wireless

How to prompt for confirmation in bash script

read -p "confirm [y/N]: " -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    exit 1
fi

stackoverflow

Added on 15.Jan.2016
Tags: bash linux terminal script prompt

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top