Skip to content

Instantly share code, notes, and snippets.

View alex-cory's full-sized avatar
🔥
🤔

Alex Cory alex-cory

🔥
🤔
View GitHub Profile
@alex-cory
alex-cory / count.js
Created November 22, 2017 18:47
Counts the number of X in an array
/**
* @example
* const list = ['a', 'b', 'c', 'c', 'c']
* const numOfCs = count(list, letter => letter === 'c')
* console.log(numOfCs) // 3
*/
const count = (ls, cb) => ls.reduce((count, item) => cb(item) ? ++count : count, 0)
@alex-cory
alex-cory / strip.js
Created September 25, 2017 22:47
Cool strip function. strip('cool').from('coolio') using dynamic regex
const strip = toRemove => ({
from: str => {
const pattern = `(${toRemove.split('').join(')(')})`
return str.replace(RegExp(pattern, 'ig'), c => '')
}
})
strip('cool').from('coolio') // io
import { observer as observe, inject } from 'mobx'
import { withRouter } from 'react-router'
import React, { Component } from 'react'
// I wonder if this works
const observer = stores => Component => withRouter(inject(...stores)(observe(Component)))
@observer('store')
class MyComponent extends Component {
// ...
@alex-cory
alex-cory / isUniqueRegex.js
Created September 1, 2017 03:20
Tests if a string contains all unique characters
// does not include special chars
const isUnique = str => !/([a-z0-9]).*?\1/i.test(str)
// includes special chars
const isUnique = str => !/^.*(.).*\1.*$/i.test(str)
@alex-cory
alex-cory / uniq.js
Created August 16, 2017 09:45
Remove duplicates from array
var uniq = ar => Array.from(new Set(a))
var uniq = ar => [...new Set(a)]
// Others: https://stackoverflow.com/a/9229821/2782583
@alex-cory
alex-cory / capitalizations.js
Last active August 13, 2017 21:52
isAllUpperCase(word) isAllLowerCase(word) isCapitalizedOnly(word)
const isAllUp = s => s.match(/[^a-z]*/)[0].length === s.length
const isAllLow = s => s.match(/[a-z]*/)[0].length === s.length
const isCapitalized = s => s.substring(1, s.length).match(/[a-z]*/)[0].length === s.length - 1 && /^[A-Z]/.test(s)
console.log('All UP? expect: true got -> ', isAllUp('SOMEWORD'))
console.log('All UP? expect: false got -> ', isAllUp('Someword'))
console.log('All UP? expect: false got -> ', isAllUp('someword'))
console.log('All LOW? expect: true got -> ', isAllLow('someword'))
console.log('All LOW? expect: false got -> ', isAllLow('Someword'))
console.log('All LOW? expect: false got -> ', isAllLow('SOMEWORD'))
@alex-cory
alex-cory / isPrime.js
Last active July 30, 2017 22:54
JS isPrime
/**
* by definition, primes cannot be negative
*/
const isPrime = value => {
for(var i = 2; i < value; i++) {
if(value % i === 0) {
return false;
}
}
return value > 1;
@alex-cory
alex-cory / closest.js
Created July 29, 2017 18:45
Get Closest Number To
var closest = (nums, goal) => nums.reduce((prv, cur) => (
Math.abs(prv - goal) > Math.abs(cur - goal) ? cur : prv
))
closest([-1, 2, 1, -4], 3) // should return 2
@alex-cory
alex-cory / arrayToN.js
Created July 22, 2017 08:42
Generate Array [0, ...., N] Function
// Option 1: [0, ...., N]
var array = length => Array.from({ length }, (_, i) => i)
// Option 2: [0, ...., N]
var array = to => Array.from(Array(to), (_, i) => i)
// Option 3: [1, ....., N]
var array = length => Array.from({ length }, (_, i) => i + 1)
@alex-cory
alex-cory / alphabetArray.js
Last active July 22, 2017 08:36
Programmatically Generating Alphabet
String.fromCharCode(...Array(123).keys()).slice(97).split('')