Skip to content

Instantly share code, notes, and snippets.

@danedavid
danedavid / balancedParanthesis.js
Created April 4, 2018 05:51
Function to check for balanced parenthesis in any string using Array.prototype.reduce(). Inspiration: https://medium.freecodecamp.org/check-out-these-useful-ecmascript-2015-es6-tips-and-tricks-6db105590377
const isBalanced = str => {
const arr = str.split('');
const res = arr.reduce((acc, item) => {
if ( acc.slice(-1)[0] === '(' ) {
if ( item === ')' ) {
acc.pop();
return acc;
}
}
if ( item === '(' || item === ')' ) {
@danedavid
danedavid / stringPermutation.js
Created May 2, 2018 16:59
Permutations of a String in JavaScript
// returns an array containing all permutations of a given string
const permute = str => {
let permutations = [];
if ( str.length <= 1 ) {
return [str];
}
for ( let i = 0; i < str.length; i++ ) {
let char = str[i];
@danedavid
danedavid / ls.js
Created June 7, 2018 11:13
Terminal command 'ls' using Node
const fs = require('fs');
fs.readdir('.', 'utf8', (err, files) => {
if ( err ) {
console.log('Error: ', err);
return;
}
let largestLength = files.slice().sort((a,b) => b.length - a.length)[0].length;
let columns = Math.floor( process.stdout.columns / largestLength );
const { RESTDataSource } = require('apollo-datasource-rest');
class GoogleBooksAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://www.googleapis.com/books/v1/';
}
async getBooks() {
const res = await this.get(`volumes?q=harry+potter`);
import { GraphQLClient } from 'graphql-request';
const client = new GraphQLClient('http://localhost:4000/api');
const handleSearch = (value) => {
client.request(/* GraphQL */`
query getBookList($search: String!) {
search(searchString: $search) {
count
books {
import React, { useState } from 'react';
import { TextInputFormElement } from './TextInputFormElement';
import { NumberInputFormElement } from './NumberInputFormElement';
const data = ['a', 1, 'b', 2, 'c', 3, 'd', 4, 'e', 5];
const App = () => {
const [activeElement, setActiveElement] = useState('a');
return (
<div id='intersector' className='intersection-line'>
import { useEffect } from 'react';
export const useActiveOnIntersect = (setActiveElement, elementRef) => {
const options = {
root: document.querySelector('#intersector'),
rootMargin: '0px',
threshold: 1.0,
};
const callback = (entries) => {
import { useEffect } from 'react';
export const useFocusOnActive = (active, inputRef) => {
useEffect(() => {
if ( active && inputRef.current ) {
inputRef.current.focus();
return () => inputRef.current.blur();
}
}, [active]);
};
import React, { useRef } from 'react';
import { useActiveOnIntersect } from './hooks/useActiveOnIntersect';
import { useFocusOnActive } from './hooks/useFocusOnActive';
export const TextInputFormElement = ({ char, active, setActiveElement }) => {
const containerEl = useRef();
const inputRef = useRef();
useActiveOnIntersect(() => setActiveElement(char), containerEl);
useFocusOnActive(active, inputRef);
@danedavid
danedavid / StopWords.json
Created December 10, 2019 07:58
Stop words & punctuations used by MS PowerBI
{
"Punctuations": [
"!", ".", ":", "'", ";", ",", "?",
"@", "#", "$", "%", "^", "&", "*",
"(", ")", "[", "]", "\"", "\\", "/",
"-", "_", "+", "=", "<", ">", "|"
],
"StopWords": [
"a", "able", "about", "across", "after", "all", "almost", "also", "am", "among", "an",
"and", "any", "are", "as", "at", "be", "because", "been", "but", "by", "can", "cannot",