Skip to content

Instantly share code, notes, and snippets.

View kkoziarski's full-sized avatar

Krzysztof Koziarski kkoziarski

View GitHub Profile
@kkoziarski
kkoziarski / exceptions.js
Last active August 29, 2015 14:10
Exceptions - The Good Parts
var add = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw {
name: 'TypeError',
message: 'add needs numbers'
};
}
return a + b;
}
@kkoziarski
kkoziarski / Function.prototype.method.js
Last active August 29, 2015 14:10
Function.prototype.method - The Good Parts
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
// Add a method conditionally.
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
}
@kkoziarski
kkoziarski / closure.js
Created November 28, 2014 11:11
Closere - The Good Parts
//Instead of initializing myObject with an object literal, we will initialize myObject by
//calling a function that returns an object literal. That function defines a value variable. That variable is always available to the increment and getValue methods, but
//the function’s scope keeps it hidden from the rest of the program.
//We are not assigning a function to myObject. We are assigning the result of invoking
//that function. Notice the()on the last line. The function returns an object containing two methods, and those methods continue to enjoy the privilege of access to the
//value variable.
var myObject = function ( ) {
var value = 0;
return {
increment: function (inc) {
@kkoziarski
kkoziarski / module.js
Created November 28, 2014 11:12
Module - The Good Parts
var serial_maker = function () {
// Produce an object that produces unique strings. A
// unique string is made up of two parts: a prefix
// and a sequence number. The object comes with
// methods for setting the prefix and sequence
// number, and a gensym method that produces unique
// strings.
var prefix = '';
var seq = 0;
@kkoziarski
kkoziarski / number.integer.js
Created November 28, 2014 11:13
Number.integer - The Good Parts
//returns only integer parts
Number.method('integer', function ( ) {
return Math[this < 0 ? 'ceiling' : 'floor'](this);
});
document.writeln((-10 / 3).integer( )); // -3
@kkoziarski
kkoziarski / String.trim.js
Created November 28, 2014 11:14
String.trim - The Good Parts
//JavaScript lacks a method that removes spaces from the ends of a string.
//That is an easy oversight to fix:
String.method('trim', function ( ) {
return this.replace(/^\s+|\s+$/g, '');
});
document.writeln('"' + " neat ".trim( ) + '"');
@kkoziarski
kkoziarski / new-object.js
Created November 28, 2014 11:15
new object - The Good Parts
var mammal = function (spec) {
var that = {};
that.get_name = function ( ) {
return spec.name;
};
that.says = function ( ) {
return spec.saying || '';
};
return that;
};
@kkoziarski
kkoziarski / isNumber.js
Created November 28, 2014 11:15
isNumber - The Good Parts
var isNumber = function isNumber(value) {
return typeof value === 'number' && isFinite(value);
}
@kkoziarski
kkoziarski / isArray.js
Created November 28, 2014 11:16
isArray - The Good Parts
var isArray = function (value) {
return value &&
typeof value === 'object' &&
typeof value.length === 'number' &&
typeof value.splice === 'function' &&
!(value.propertyIsEnumerable('length'));
};
@kkoziarski
kkoziarski / redirect-to-https.js
Last active August 29, 2015 14:15
Redirect to HTTPS bookmarklet
//#JS #bookmarkelt
//redirect to https
(function() {
var url = window.location.href.toLowerCase();
if(url.indexOf('https:') < 0 && url.indexOf('http:') >= 0) {
var httpsUrl = 'https:' + window.location.href.substring(5); //url.replace('http', 'https');
window.location.href = httpsUrl;
}
})()