Skip to content

Instantly share code, notes, and snippets.

View afonsomatos's full-sized avatar
🎯
Focusing

Afonso Matos afonsomatos

🎯
Focusing
  • Lisboa, Portugal
View GitHub Profile
@afonsomatos
afonsomatos / object-when.js
Last active August 29, 2015 14:12
Object.when implementation in ES5
/*!
Object-when implementation on ES5 (made by afonsomatos)
Released under the MIT license (2014-11-10)
*/
// object.when
Object.defineProperty(Object.prototype, "when", {
@afonsomatos
afonsomatos / now.js
Created January 3, 2015 16:00
Most accurate javascript timing
// Most accurate javascript timing
now = function() {
return performance.now()||
performance.mozNow() ||
performance.msNow() ||
performance.oNow() ||
performance.webkitNow() ||
Date.now() ||
new Date().getTime();
@afonsomatos
afonsomatos / getLocation.js
Created January 3, 2015 16:00
Get script file location
// Get script file location
// doesn't work for older browsers
var getScriptLocation = function() {
var fileName = "fileName";
var stack = "stack";
var stackTrace = "stacktrace";
var loc = null;
var matcher = function(stack, matchedLoc) { return loc = matchedLoc; };
@afonsomatos
afonsomatos / createTable.js
Last active August 29, 2015 14:22
Lightweight table creator
/*!
* Easy to use table creator
* afonsomatos <[email protected]>
*
* @param {Array} - Array containing the rows of the table
* @param {Object} [{rows: Array.length,
* padding: 2,
* marginLeft: 2,
* lineHeight: 0,
* cols: <row in most length>.length }] - Configure object
@afonsomatos
afonsomatos / colorful.html
Created June 15, 2015 17:44
Colorful tag (this is a joke)
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
@afonsomatos
afonsomatos / NumberES6.js
Last active August 29, 2015 14:23
Numbers' methods and new literals in es6
// Octals 0 + o + integer
0o8
// Binary 0 + b + integer
0b11
// ParseInt doesn't support binary literals
parseInt('0b11', 2); // 0
// Instead use Number()
Number('0b11'); // 3
// Or remove prefix
parseInt('11', 2); // 3
@afonsomatos
afonsomatos / MathES6.js
Last active August 29, 2015 14:23
Math methods and constants in ES6
// Get sign of number (-1, +1) or NaN or (-+)zero
Math.sign(-8); // -1
Math.sign(2); // 1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
// Remove decimal fraction of x
Math.trunc(Math.PI); // 3
Math.trunc(-3.99999); // -3
// Cube root of x
@afonsomatos
afonsomatos / StringES6.js
Created June 17, 2015 09:46
Most important String features in ES6
// If strings starts with substr
'myString'.startsWith('my', 0 /* start searching */); // true
// If string ends with substr
'myString'.endsWith('ring', 8 /* end searching */); // true
// If strings has another substr
'myString'.includes('str', 0 /* start searching */); // true
// Multiply string
'woof '.repeat(5);
// -- Template strings
// String interpolation
@afonsomatos
afonsomatos / SymbolES6.js
Created June 17, 2015 11:52
New primitive Symbol on ES6
// Primitive value
typeof Symbol(); // 'symbol'
// Unique
Symbol() === Symbol(); // false
// Invoked without `new`
new Symbol(); // TypeError: Symbol is not a constructor
// Set description of Symbol
let sy = Symbol('personal primitive');
String(sy); // 'Symbol(personal primitive)'
// Symbols as property keys and method names
@afonsomatos
afonsomatos / TemplateStringES6.js
Last active August 29, 2015 14:23
Overview of template strings and tagged templates on ES6
// Template strings
`Hello my name is ${firstName} ${lastName}`
// Mult-line
`This is a multi-line
string that appears
to be very cool perhaps
it is.`
// Tagged template
tagFunction`Hello\n ${firstName} ${lastName}`
// Implement tagFunction