Skip to content

Instantly share code, notes, and snippets.

View davidicus's full-sized avatar

David Conner davidicus

View GitHub Profile
@davidicus
davidicus / stringReplace.js
Last active January 10, 2018 16:25
Separate camel/pascal case string into lead cap words
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
@davidicus
davidicus / flattenArray.js
Created December 25, 2017 20:52
Flatten an array of arrays
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
( acc, cur ) => acc.concat(cur),
[]
);
// flattened is [0, 1, 2, 3, 4, 5]
@davidicus
davidicus / dedupeArray.js
Created December 25, 2017 20:51
Remove duplicate values in an array
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]
@davidicus
davidicus / prettifyJson.js
Created December 25, 2017 19:45
Prettify JSON response for console.
// 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);
@davidicus
davidicus / promisify.js
Last active December 21, 2017 03:24
Make async code return a promise
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
@davidicus
davidicus / titleCase.js
Last active January 10, 2018 17:55
TitleCase a string function
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);
@davidicus
davidicus / selector.js
Created August 25, 2017 13:53
jQuery like selectors
const dqs = (query) => document.querySelector(query);
const dqsa = (query) => document.querySelectorAll(query);
let element = dqs(".classname");
@davidicus
davidicus / IndexedDB101.js
Created July 17, 2017 16:06 — forked from JamesMessinger/IndexedDB101.js
Very Simple IndexedDB Example
// 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"});
@davidicus
davidicus / jsonLoader.js
Created July 15, 2017 02:47
XMLHttpRequest to easily load json object
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);
@davidicus
davidicus / example.js
Created July 13, 2017 14:13 — forked from hackjutsu/example.js
[Highlight.js + line number] This snippet adds line number support to Highlight.js. Besides, special css trick is applied to stop the line numbers from being selected and copied. #tags: highlight, lepton
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