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 / 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 / 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 / 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 / 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)
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 / 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
@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 / isLeapYear.js
Created December 4, 2017 03:15
Tells whether the specified year is a leap year
const isLeapYear = year => new Date(year,1,29).getDate() === 29
// recursive
const isPalindrome = str => {
if (str.length === 0 || str.length === 1) return true
if (str[0] === str[str.length - 1]) return isPalindrome(str.slice(1, str.length - 1))
return false
}
// efficiently in O(n) time
const isPalindrome2 = str => {
var half = Math.floor(str.length / 2)
@alex-cory
alex-cory / capitalize.js
Created November 20, 2018 18:23
Capitalizes strings
/**
* Handles special characters too
*/
const capitalize = w => w.toLowerCase().replace(/(?:^|\s)\S/g, l => l.toUpperCase());