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 myString = 'thisIsMyStringFool'; | |
// find and separate words based off capital letter | |
const replacedString = myString.replace(/([A-Z])/g, ' &1'); | |
//capitalize first letter of first word if not already | |
replacedString.replace(/^./, st => str.toUpperCase()); | |
console.log(replacedString); | |
// output => This Is My String Fool |
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 flattened = [[0, 1], [2, 3], [4, 5]].reduce( | |
( acc, cur ) => acc.concat(cur), | |
[] | |
); | |
// flattened is [0, 1, 2, 3, 4, 5] |
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 myArray = [0, 1, 1, 2, 3, 3] | |
myArray.reduce((a, b) => { | |
if (a.indexOf(b) < 0) { | |
a.push(b); | |
} | |
return a; | |
}, []); | |
// returns [0,1,2,3] |
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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | |
// Converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified. | |
// @param {object} - The value to convert to a JSON string. | |
// @param {func/ array} - function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties | |
// @return {json} - A JSON string representing the given value. | |
JSON.stringify(<object>, <function/array>, <integer>); | |
// const obj = {Name: "David", Age: 37, Sex: true} | |
// JSON.stringify(obj, null, 2); | |
// JSON.stringify(obj, ["Name"], 2); |
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 promisify = func => (...args) => { | |
return new Promise((resolve, reject) => { | |
func(...args, (err, result) => { | |
err ? reject(err) : resolve(result); | |
}) | |
}); | |
} | |
// const delay = promisify((d, cb) => setTimeout(cb, d)) | |
// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s |
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 toTitleCase = (str) => { | |
return ( | |
str.replace(/([^\W]+[^\s-]*) */g, | |
txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() | |
)) | |
}; | |
const string = 'this-is my string-yo'; | |
toTitleCase(string); |
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 dqs = (query) => document.querySelector(query); | |
const dqsa = (query) => document.querySelectorAll(query); | |
let element = dqs(".classname"); |
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
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback | |
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; | |
// Open (or create) the database | |
var open = indexedDB.open("MyDatabase", 1); | |
// Create the schema | |
open.onupgradeneeded = function() { | |
var db = open.result; | |
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"}); |
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 json = {}; | |
let posts = [] | |
const loadJSON = (path, cb) => { | |
var xobj = new XMLHttpRequest(); | |
xobj.overrideMimeType(`application/json`); | |
xobj.open(`GET`, path, true); | |
xobj.onreadystatechange = function () { | |
if (xobj.readyState === 4 && xobj.status === 200) { | |
cb(xobj.responseText); |
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
import HighlightJS from 'highlight.js' | |
createHighlightedCodeBlock (content, language) { | |
let lineNumber = 0 | |
const highlightedContent = HighlightJS.highlightAuto(content, [language]).value | |
/* Highlight.js wraps comment blocks inside <span class="hljs-comment"></span>. | |
However, when the multi-line comment block is broken down into diffirent | |
table rows, only the first row, which is appended by the <span> tag, is | |
highlighted. The following code fixes it by appending <span> to each line |