Skip to content

Instantly share code, notes, and snippets.

crazy4groovy

Block or report user

Report or block crazy4groovy

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@crazy4groovy
crazy4groovy / jsonblob.js
Last active Aug 31, 2019
API for jsonblob.com
View jsonblob.js
fetch = fecth || require('cross-fetch')
function jsonblob(json /* : Object */, method /* : string*/, id /* : string */) /* : Promise */ {
const isUpsert = method.toUpperCase() !== 'GET' && method.toUpperCase() !== 'DELETE'
const options = {
method,
headers: { "Content-Type": "application/json", "Accept": "application/json" },
body: isUpsert ? JSON.stringify(json) : undefined
}
@crazy4groovy
crazy4groovy / numberOfCarryOperations.js
Last active Aug 28, 2019
Count how many times a carry operation is performed while adding two numbers
View numberOfCarryOperations.js
function numberOfCarryOperations(a, b) {
const aDigits = String(a).split('').reverse()
const bDigits = String(b).split('').reverse()
const maxDigits = aDigits.length > bDigits.length ? aDigits : bDigits
const minDigits = maxDigits === aDigits ? bDigits : aDigits
/// console.log({a, b, maxDigits, minDigits})
let carry = 0
const carryCount = maxDigits.reduce((c, d, i) => {
const dA = Number(d) || 0
View cache_stampede.js
const refPromises = {};
const workflowWithOneCacheMiss = async (key) => {
const dataFromCache = await getCache(key);
if (dataFromCache !== undefined) {
return dataFromCache;
}
if (!refPromises[key]) {
@crazy4groovy
crazy4groovy / sort-by.js
Last active Aug 30, 2019
custom array sort by object field
View sort-by.js
function getVal(obj, fields) {
if (!(obj && fields[0])) return obj
return getVal(obj[fields.shift()], fields)
}
export const customSort = (arr, sortField, sortBy) => {
let sortVals
if (sortBy) {
const sortByWeight = sortBy.reduce(
(obj, item, index) => ({
@crazy4groovy
crazy4groovy / intersection-lines.js
Created Aug 8, 2019
How many straight lines, starting from point {x: 0, y: 0}, are needed to intersect with all the points in a given array?
View intersection-lines.js
function normalize({x, y}) {
const nx = x / Math.abs(x)
const ny = y / Math.abs(x)
return nx + '|' + ny
}
/*
console.log(normalize({x: 1, y: 2}))
console.log(normalize({x: 2, y: 4}))
@crazy4groovy
crazy4groovy / splittable-sort-array.js
Created Aug 7, 2019
How many groups (more than 1 number per group) of an integer list can be split out, each sorted, and concatenated together to produce the same result as sorting the list?
View splittable-sort-array.js
function solution(A) {
let index = 0
let results = 0
let remainder = 0
let number
while (index < A.length) {
number = A[index]
for (let i = index + 1; i < A.length; i++) {
if (number > A[i]) {
index = i
View min-positive-int.js
// given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A
function solution(A) {
const sortedUnique = [...new Set([...A])]
.filter((n) => (n > 0))
.sort((a, b) => (a - b))
if (sortedUnique.length === 0) return 1
let invalidIdx = sortedUnique.findIndex((n, i) => n !== (i + 1))
if (invalidIdx === -1) return sortedUnique[sortedUnique.length - 1] + 1
@crazy4groovy
crazy4groovy / imports.json
Last active Jul 23, 2019
Example SystemJS usage
View imports.json
{
"imports": {
"lodash": "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"
}
}
@crazy4groovy
crazy4groovy / csv-writer.js
Last active Jul 23, 2019
csv-stringify promisified wrapper
View csv-writer.js
import * as stringify from 'csv-stringify'
module.exports = function (delimiter = ',') {
const data = []
const result = {str: undefined}
const stringifier = stringify({
delimiter
})
@crazy4groovy
crazy4groovy / lazyimg-IntersectionObserver.js
Last active Jul 26, 2019
Intersection Observer examples
View lazyimg-IntersectionObserver.js
[...document.querySelectorAll('img[data-src]')].forEach((el, i) => {
const observer = new window.IntersectionObserver(function(entries, self) {
const {isIntersecting, target} = entries[0]
if (isIntersecting) {
const img = target
const src = img.getAttribute('data-src')
if (src) img.src = src
self.unobserve(img)
}
}, {})
You can’t perform that action at this time.