Skip to content

Commit

Permalink
rename hasKey to hasProp for 🌽sistancy (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsoft authored Mar 19, 2017
1 parent 545a009 commit 73a0f4f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ There may come a time when you need to adjust a value when a condition is true,
### Predicate Functions
All functions in this group have a signature of `* -> Boolean` and are used with the many predicate based functions that ship with `crocks`, like [`safe`](#safe), [`ifElse`](#ifelse) and `filter` to name a few. They also fit naturally with the `Pred` ADT. Below is a list of all the current predicates that are included with a description of their truth:

* `hasKey : (String | Number) -> a -> Boolean`: An Array or Object that contains the provided index or key
* `hasProp : (String | Number) -> a -> Boolean`: An Array or Object that contains the provided index or key
* `isAlt : a -> Boolean`: an ADT that provides `map` and `alt` functions
* `isApplicative : a -> Boolean`: an ADT that provides `map`, `ap` and `of` functions
* `isApply : a -> Boolean`: an ADT that provides `map` and `ap` functions
Expand Down
2 changes: 1 addition & 1 deletion crocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const pointFree = {
}

const predicates = {
hasKey: require('./predicates/hasKey'),
hasProp: require('./predicates/hasProp'),
isAlt: require('./predicates/isAlt'),
isApplicative: require('./predicates/isApplicative'),
isApply: require('./predicates/isApply'),
Expand Down
4 changes: 2 additions & 2 deletions crocks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const or = require('./logic/or')
const when = require('./logic/when')
const unless = require('./logic/unless')

const hasKey = require('./predicates/hasKey')
const hasProp = require('./predicates/hasProp')
const isAlt = require('./predicates/isAlt')
const isApplicative = require('./predicates/isApplicative')
const isApply = require('./predicates/isApply')
Expand Down Expand Up @@ -239,7 +239,7 @@ test('entry', t => {
t.equal(crocks.when, when, 'provides the when logic function')
t.equal(crocks.unless, unless, 'provides the unless logic function')

t.equal(crocks.hasKey, hasKey, 'provides the hasKey function')
t.equal(crocks.hasProp, hasProp, 'provides the hasProp function')
t.equal(crocks.isAlt, isAlt, 'provides the isAlt function')
t.equal(crocks.isApplicative, isApplicative, 'provides the isApplicative function')
t.equal(crocks.isApply, isApply, 'provides the isApply function')
Expand Down
45 changes: 0 additions & 45 deletions predicates/hasKey.spec.js

This file was deleted.

8 changes: 4 additions & 4 deletions predicates/hasKey.js → predicates/hasProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const curry = require('../helpers/curry')
const isString = require('./isString')
const isInteger = require('./isInteger')

// hasKey : (String | Number) -> a -> Boolean
function hasKey(key, x) {
// hasProp : (String | Number) -> a -> Boolean
function hasProp(key, x) {
if(!(isString(key) || isInteger(key))) {
throw new TypeError('has: Number or String required for first argument')
throw new TypeError('hasProp: Number or String required for first argument')
}

return (!!x && x[key] !== undefined)
}

module.exports = curry(hasKey)
module.exports = curry(hasProp)
46 changes: 46 additions & 0 deletions predicates/hasProp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const test = require('tape')
const helpers = require('../test/helpers')

const bindFunc = helpers.bindFunc

const noop = helpers.noop

const isFunction = require('../predicates/isFunction')

const hasProp = require('./hasProp')

test('hasProp function', t => {
const f = bindFunc(hasProp)

t.ok(isFunction(hasProp), 'is a function')

const err = /hasProp: Number or String required for first argument/
t.throws(f(undefined, {}), err, 'throws with undefined in first argument')
t.throws(f(null, {}), err, 'throws with null in first argument')
t.throws(f(false, {}), err, 'throws with false in first argument')
t.throws(f(true, {}), err, 'throws with true number in first argument')
t.throws(f({}, {}), err, 'throws with object in first argument')
t.throws(f([], {}), err, 'throws with array in first argument')
t.throws(f(noop, {}), err, 'throws with function in first argument')

const key = 'something'
const val = 'thirty-six'
const obj = { [key]: val }
const arr = [ 1, 2, 3 ]

t.equals(hasProp(key, undefined), false, 'returns false for undefined')
t.equals(hasProp(key, null), false, 'returns false for null')
t.equals(hasProp(key, 0), false, 'returns false for falsey number')
t.equals(hasProp(key, 1), false, 'returns false for truthy number')
t.equals(hasProp(key, ''), false, 'returns false for falsey string')
t.equals(hasProp(key, 'string'), false, 'returns false for truthy string')

t.equals(hasProp(key, obj), true, 'returns true when key exists on object')
t.equals(hasProp(key, {}), false, 'returns false when key does not exist on object')

t.equals(hasProp(2, arr), true, 'returns true when index exists in array')
t.equals(hasProp("1", arr), true, 'returns true when string index exists in array')
t.equals(hasProp(-1, arr), false, 'returns false when index does not exist in array')

t.end()
})

0 comments on commit 73a0f4f

Please sign in to comment.