Created
March 23, 2015 02:38
-
-
Save TGOlson/b20d223002f5286979e4 to your computer and use it in GitHub Desktop.
PropertyGetter monad kata
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
| function PropertyGetterFactory(value) { | |
| return { | |
| get: function(prop) { | |
| return mapToPropertyGetter(partial(get, prop), value); | |
| }, | |
| map: function(callback) { | |
| return mapToPropertyGetter(callback, value); | |
| }, | |
| unwrap: always(value) | |
| }; | |
| } | |
| function mapToPropertyGetter(callback, value) { | |
| return PropertyGetterFactory(map(callback, value)); | |
| } | |
| function get(prop, obj) { | |
| return obj[prop]; | |
| } | |
| function partial(fn, value) { | |
| return fn.bind(null, value); | |
| } | |
| function map(callback, value) { | |
| if(isNil(value)) { | |
| return value; | |
| } else { | |
| return callback(value); | |
| } | |
| } | |
| function always(v) { | |
| return function() { | |
| return v; | |
| }; | |
| } | |
| function isNil(v) { | |
| return v === null || v === undefined; | |
| } | |
| module.exports = PropertyGetterFactory; |
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 R = require('ramda'); | |
| var PropertyGetterFactory = require('./property-getter'); | |
| ddescribe('PropertyGetter', function() { | |
| var data = { | |
| title: 'This is a title' | |
| }; | |
| var PropertyGetter = PropertyGetterFactory(data); | |
| function toBeAPropertyGetterWithValue(expected) { | |
| return R.eqDeep(this.actual.unwrap(), expected); | |
| } | |
| beforeEach(function() { | |
| this.addMatchers({ | |
| toBeAPropertyGetterWithValue: toBeAPropertyGetterWithValue | |
| }); | |
| }); | |
| it('should return a property on an object', function() { | |
| expect(PropertyGetter.get('title')).toBeAPropertyGetterWithValue(data.title); | |
| }); | |
| it('should return undefined if the initial value is undefined', function() { | |
| PropertyGetter = PropertyGetterFactory(undefined); | |
| expect(PropertyGetter.get('title')).toBeAPropertyGetterWithValue(undefined); | |
| }); | |
| it('should return undefined if the initial value does not have the defined property', function() { | |
| PropertyGetter = PropertyGetterFactory(5); | |
| expect(PropertyGetter.get('title')).toBeAPropertyGetterWithValue(undefined); | |
| }); | |
| it('should return an array item if the initial value is an array', function() { | |
| PropertyGetter = PropertyGetterFactory([1, 2]); | |
| expect(PropertyGetter.get('0')).toBeAPropertyGetterWithValue(1); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment