This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var re = /\{\d\}/ | |
var str = 'My name is {0} . My location is {1}. My country is {2}' | |
function formatter (str) { | |
var params = Array.prototype.splice.call(arguments, 1) | |
for(var i=0;i<params.length;i++){ | |
str = str.replace(re, params[i]) | |
} | |
return str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
div { | |
display: inline-block; | |
width: 200px; | |
border:1px solid; | |
box-sizing: border-box; | |
margin: 1px 0px; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const union = (arr1, arr2) => utility.uniq([...arr1, ...arr2]) | |
var utility = {union, uniq} | |
function uniq (arr) { | |
var elemMap = {} | |
for(var val of arr){ | |
elemMap[val] = elemMap[val] ? elemMap[val] += 1 : 1 | |
} | |
return Object.keys(elemMap).map(function (key) { | |
return Number(key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce(fn, delay) { | |
var timer = null | |
return function () { | |
var args = arguments | |
clearTimeout(timer) | |
timer = setTimeout(function () { | |
fn.apply(null, args) | |
}, delay) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function apply (fn, context, params) { | |
return fn.call(context, ...params) | |
} | |
var obj = {a: 2} | |
function foo (b,c,d) { | |
console.log(this.a, b, c, d) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isRelatedTo (o1, o2) { | |
function F() {} | |
F.prototype = o2 | |
return o1 instanceof F | |
} | |
if (!Object.create) { | |
Object.create = function(o) { | |
function F(){} | |
F.prototype = o; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Something = { | |
cool: function() { | |
this.greeting = "Hello World"; | |
this.count = this.count ? this.count + 1 : 1; | |
} | |
}; | |
Something.cool(); | |
console.log(Something.greeting) | |
console.log(Something.count); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = {pick} | |
function pick (object, identifierStr) { | |
var path = identifierStr.split('.') | |
if (path.length === 1 ) { | |
return object[path[0]] | |
} | |
var [first, ...rest] = path | |
return pick(object[first], rest.join('.')) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function head (arr) { | |
var [start, ...rest] = arr; | |
return start; | |
} | |
function tail (arr) { | |
var [start, ...rest] = arr; | |
return rest; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function groupBy (collection, func, callback) { | |
if (typeof func !== 'function') { | |
callback('please provide a function to group by') | |
return | |
} | |
if (typeof callback !== 'function') { | |
callback('please provide a function as a callback') | |
return | |
} |