Skip to content

Commit

Permalink
feat(Modules): Add prop and propOr
Browse files Browse the repository at this point in the history
  • Loading branch information
beardedtim committed Sep 17, 2017
1 parent b1c757e commit e9b014e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/modules/prop.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import prop from '../../modules/prop'

describe('prop', () => {
test('returns the value at the given key', () => {
const key = 'name'
const obj = { name: 'Tim' }
const result = prop(key, obj)

expect(result).toBe('Tim')
})

test('is curried', () => {
const key = 'name'
const obj = { name: 'Tim' }
const result = prop(key)(obj)

expect(result).toBe('Tim')
})
})
29 changes: 29 additions & 0 deletions __tests__/modules/propOr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import propOr from '../../modules/propOr'

describe('propOr', () => {
test('returns the value at the given key', () => {
const key = 'name'
const obj = { name: 'Tim' }
const result = propOr('', key, obj)

expect(result).toBe('Tim')
})

test('returns the default if key is undefined', () => {
const key = 'age'
const obj = { name: 'Tim' }
const def = 18
const result = propOr(def, key, obj)

expect(result).toBe(def)
})

test('is a curried function', () => {
const key = 'age'
const obj = { name: 'Tim' }
const def = 18
const result = propOr(def)(key)(obj)

expect(result).toBe(def)
})
})
5 changes: 5 additions & 0 deletions modules/prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import curry from './curry'

const prop = curry((key, obj) => obj[key])

export default prop
9 changes: 9 additions & 0 deletions modules/propOr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import curry from './curry'
import prop from './prop'

const propOr = curry((def, key, obj) => {
const val = prop(key, obj)
return val === undefined ? def : val
})

export default propOr

0 comments on commit e9b014e

Please sign in to comment.