Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,21 @@ R.equals(float('nan'), float('nan')) # True

Similar to `hasPath`.

- [ ] hasIn
- [x] hasPath
- [x] hasIn

works for both dict and object

```python
class Obj:
def __init__(self, v):
self.v = v

obj1 = Obj(1)
R.hasIn('v', obj1) # True
R.hasIn('v', {'v': 1}) # True
```

- [x] hasPath
Support both dict and object.

```python
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from .gt import gt
from .gte import gte
from .has import has
from .hasIn import hasIn
from .hasPath import hasPath
from .head import head
from .identity import identity
Expand Down
12 changes: 12 additions & 0 deletions ramda/hasIn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .isNil import isNil
from .private._curry2 import _curry2
from .private._has import _has


def inner_hasIn(prop, obj):
if isNil(obj):
return False
return _has(obj, prop)


hasIn = _curry2(inner_hasIn)
68 changes: 68 additions & 0 deletions test/test_hasIn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import unittest

import ramda as R
from ramda.private._isFunction import _isFunction

"""
https://github.com/ramda/ramda/blob/master/test/hasIn.js
"""

dictFred = {'name': 'Fred', 'age': 23}
dictAnon = {'age': 99}


class Fred:
def __init__(self, name, age):
self.name = name
self.age = age


class Anon:
def __init__(self, age):
self.age = age


fred = Fred('Fred', 23)
anon = Anon(99)


class TestHasInForDict(unittest.TestCase):
def test_returns_a_function_that_checks_the_appropiate_property(self):
nm = R.hasIn('name')
self.assertEqual(True, _isFunction(nm))
self.assertEqual(True, nm(dictFred))
self.assertEqual(False, nm(dictAnon))


class TestHasInForObject(unittest.TestCase):
def test_returns_a_function_that_checks_the_appropiate_property(self):
nm = R.hasIn('name')
self.assertEqual(True, _isFunction(nm))
self.assertEqual(True, nm(fred))
self.assertEqual(False, nm(anon))

def test_checks_properties_from_parent(self):
class Person:
def __init__(self, name):
self.name = name

class Bob(Person):
def __init__(self, name, age):
super().__init__(name)
self.age = age
bob = Bob('Bob', 23)
self.assertEqual(True, R.hasIn('name', bob))


class TestHasInForOthers(unittest.TestCase):
def test_works_properly_when_called_with_two_arguments(self):
self.assertEqual(True, R.hasIn('name', fred))
self.assertEqual(False, R.hasIn('name', anon))

def test_returns_false_when_non_existent_object(self):
self.assertEqual(False, R.hasIn('name', None))


if __name__ == '__main__':
unittest.main()