Skip to content

Instantly share code, notes, and snippets.

@imkrish
Created October 11, 2018 19:34
Show Gist options
  • Save imkrish/5e806d3b17bf1e07c635108b14902d9d to your computer and use it in GitHub Desktop.
Save imkrish/5e806d3b17bf1e07c635108b14902d9d to your computer and use it in GitHub Desktop.
import {
trim,
match,
replace,
split,
test,
toLower,
toString,
toUpper,
partial,
map,
length,
compose,
pipe,
tail,
} from 'ramda'
// trim
const text = 'a, b, c , d'
const splitByComma = split(',')
console.log(map(trim, splitByComma(text)))
// > [ 'a', 'b', 'c', 'd' ]
// match
const getVowels = match(/[aeiou]/g)
console.log(getVowels('banana aeiou'))
// > [ 'a', 'a', 'a', 'a', 'e', 'i', 'o', 'u' ]
// replace
const replaceHatesWithLoves = replace(/[hH][aA][tT][eE]/g, 'love')
console.log(replaceHatesWithLoves('I hate hAtE HATE you'))
// > I love love love you
// split
const getPaths = pipe(
split('/'),
tail as any
)
const paths = '/usr/local/bin/node'
console.log(getPaths(paths))
// > [ 'usr', 'local', 'bin', 'node' ]
// test
const startWithK = test(/^[kK]/)
console.log(startWithK('Krish'))
// > true
// toLower - toUpper
const createRegex = (global: boolean, pattern: string) =>
new RegExp(pattern, global ? 'g' : '')
const createGlobalRegex = partial(createRegex, [true])
const matchWord = compose(
match,
createGlobalRegex,
toLower // toUpper
)
const countWord = (word: string) => {
return compose(
length as any,
matchWord(word),
toLower // toUpper
)
}
const countFoo = countWord('foo')
console.log(countFoo('aaa foo Foo fOo na la na o'))
// > 3
// toString
console.log(toString(42))
// > "42"
console.log(toString(['a', 'b', 'c']))
// > '["a", "b", "c"]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment