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 roundStrings(s1, s2) { | |
for (let i in s1) { | |
if (s1.hasOwnProperty(i)) { | |
if (s1[i] !== s2[s2.length - i - 1]) { | |
return -1; | |
} | |
} | |
} | |
return 1; | |
} |
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 CsvWriter(del, enc) { | |
this.del = del || ','; | |
this.enc = enc || '"'; | |
this.escapeCol = (col) => { | |
if(isNaN(col)) { | |
if(!col) { | |
col = ''; | |
} else { | |
col = String(col); |
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 mixin(receiver, supplier) { | |
if(Object.getOwnPropertyDescriptor) { | |
Object.keys(supplier).forEach(function(prop) { | |
let descriptor = Object.getOwnPropertyDescriptor(supplier, prop); | |
Object.defineProperty(receiver, prop, descriptor); | |
}); | |
} else { // For older browsers, does not support accessor properties | |
for(let prop in supplier) { | |
if (supplier.hasOwnProperty(prop) { | |
receiver[prop] = supplier[prop]; |
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 csvWriter(del, enc) { | |
this.del = del || ','; | |
this.enc = enc || '"'; | |
this.escapeCol = (col) => { | |
if(isNaN(col)) { | |
if(!col) { | |
col = ''; | |
} else { | |
col = String(col); |
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 fieldHeaders = ["First Name", "Last Name", "City", "State", "Country", "Phone Number"]; | |
let data = [["Tom", "Stevens", "Seattle", "WA", "USA", "123-123-1234"], ["Mike", "Williams", "Detroit", "MI", "USA", "234-234-2345"], ["Janie", "Jackson", "Vancouver", "BC", "Canada", "987-977-9876"]]; | |
data.unshift(fieldHeaders); | |
function createCSV(data) { | |
let csvContent = "data:text/csv;charset=utf-8,"; | |
data.forEach((dataArray, index) => { | |
let dataStr = dataArray.join(','); | |
csvContent += index < data.length ? dataStr + '\n' : dataStr; | |
}); |
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
let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; | |
let idx = [4, 3, 5, 0, 6, 1, 2]; | |
arr = idx.map((idxVal, index) => { | |
return arr[idx.indexOf(index)]; | |
}); |
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 binarySearch(items, value) { | |
let startIndex = 0, | |
stopIndex = items.length - 1, | |
middle = Math.floor((startIndex + stopIndex) / 2); | |
while(value !== items[middle] && startIndex < stopIndex) { | |
if(value < items[middle]) { | |
stopIndex = middle - 1; | |
} else if (value > items[middle]) { | |
startIndex = middle + 1; |
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(func, immediate, wait) { | |
let timeout; | |
return function() { | |
let context = this, | |
args = arguments; | |
let callNow = immediate && !timeout; | |
clearTimeout(timeout); | |
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 flatten(input) { | |
var out = []; | |
var loop = function(arr) { | |
return arr.map.((val) => { | |
return Array.isArray(val) ? loop(val) : out.push(val); | |
}); | |
}; | |
loop(input); | |
return out; | |
} |
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 traverse(node, current) { | |
if(node.isEqualNode(current)){ | |
return current; | |
} else { | |
let el = null; | |
for(let i = 0; !el && i < current.children.length; i++){ | |
el = traverse(node, current.children[i]); | |
} | |
return el; | |
} |