This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var uniq = ar => Array.from(new Set(a)) | |
var uniq = ar => [...new Set(a)] | |
// Others: https://stackoverflow.com/a/9229821/2782583 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const strip = toRemove => ({ | |
from: str => { | |
const pattern = `(${toRemove.split('').join(')(')})` | |
return str.replace(RegExp(pattern, 'ig'), c => '') | |
} | |
}) | |
strip('cool').from('coolio') // io |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isLeapYear = year => new Date(year,1,29).getDate() === 29 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Handles special characters too | |
*/ | |
const capitalize = w => w.toLowerCase().replace(/(?:^|\s)\S/g, l => l.toUpperCase()); |