Last active
September 29, 2017 12:19
-
-
Save gdekefir/ad0a18090f731a6389bc809c076951c7 to your computer and use it in GitHub Desktop.
Returns a value of Maybe object after applying set of transformers
This file contains 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
const R = require('ramda'); | |
const composeMaybe = require('./compose-maybe').composeMaybe; | |
/** | |
* Returns a value of Maybe object after applying set of transformers | |
* default → [(y → z), (x → y), …, (o → p), ((a, b, …, n) → o)] → ((a, b, …, n) → z | default) | |
*/ | |
const composeMaybeWithDefault = R.curry((defaultVal, transformers) => | |
R.memoize(R.o(R.invoker(1, 'withDefault')(defaultVal), composeMaybe(...transformers))) | |
); | |
module.exports = { composeMaybeWithDefault }; |
This file contains 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
const R = require('ramda'); | |
const composeMaybeWithDefault = require('./compose-maybe-with-default').composeMaybeWithDefault; | |
describe('composeMaybeWithDefault', () => { | |
test('returns result of transformations', () => { | |
const fn = composeMaybeWithDefault(0, [R.inc, R.add(3), R.divide(8)]); | |
const result = fn(4); | |
expect(result).toBe(6); | |
}); | |
test('returns default value for null', () => { | |
const fn = composeMaybeWithDefault(0, [R.inc, R.always(null), R.divide(8)]); | |
const result = fn(4); | |
expect(result).toBe(0); | |
}); | |
test('returns default value for undefined', () => { | |
const fn = composeMaybeWithDefault(0, [R.inc, () => {}, R.divide(8)]); | |
const result = fn(4); | |
expect(result).toBe(0); | |
}); | |
test('returns NaN if result of calculations is NaN', () => { | |
const fn = composeMaybeWithDefault(0, [R.inc, R.always(NaN), R.divide(8)]); | |
const result = fn(4); | |
expect(isNaN(result)).toBeTruthy(); | |
}); | |
test('returns 0 if result of calculations is 0', () => { | |
const fn = composeMaybeWithDefault(5, [R.subtract(4)]); | |
const result = fn(4); | |
expect(result).toBe(0); | |
}); | |
}); |
This file contains 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
const R = require('ramda'); | |
const mapToMaybe = require('./map-to-maybe').mapToMaybe; | |
/** | |
* Returns version of compose for Maybe object | |
* ((y → z), (x → y), …, (o → p), ((a, b, …, n) → o)) → ((a, b, …, n) → Maybe z) | |
*/ | |
const composeMaybe = R.compose( | |
R.memoize, | |
R.apply(R.composeK), | |
R.map(mapToMaybe), | |
R.append(R.identity), // because R.composeK accepts at least 2 functions | |
R.unapply(R.identity) | |
); | |
module.exports = { composeMaybe }; |
This file contains 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
const R = require('ramda'); | |
const { Maybe } = require('zoomjs'); | |
const composeMaybe = require('./compose-maybe').composeMaybe; | |
describe('composeMaybe', () => { | |
test('returns Maybe object', () => { | |
const testObj = { | |
first: 5 | |
}; | |
const resultObj = composeMaybe(R.prop('some'), R.inc, R.prop('first'))(testObj); | |
expect(resultObj).toBeInstanceOf(Maybe); | |
}); | |
test('resulting Maybe should contain result of transformations', () => { | |
const testObj = { | |
first: 5 | |
}; | |
const resultObj = composeMaybe(R.inc, R.prop('first'))(testObj); | |
const value = resultObj.withDefault(0); | |
expect(value).toBe(6); | |
}); | |
test('returns Nothing for undefined value', () => { | |
const testObj = { | |
first: 5 | |
}; | |
const resultObj = composeMaybe(R.add(12), R.prop('some'), R.inc, R.prop('first'))(testObj); | |
expect(resultObj.isNothing()).toBeTruthy(); | |
}); | |
test('returns Nothing for null value', () => { | |
const resultObj = composeMaybe(R.always(null), R.inc)(5); | |
expect(resultObj.isNothing()).toBeTruthy(); | |
}); | |
test('returns Maybe.Just object with a value of NaN', () => { | |
const resultObj = composeMaybe(R.add(3), R.inc)('str'); | |
const value = resultObj.withDefault(0); | |
expect(isNaN(value)).toBeTruthy(); | |
}); | |
test('should accept 1 transformer function too', () => { | |
const resultObj = composeMaybe(R.inc)(3); | |
const value = resultObj.withDefault(0); | |
expect(value).toBe(4); | |
}); | |
test('is memoized', () => { | |
// eslint-disable-next-line fp/no-let | |
let counter = 0; | |
const fn = composeMaybe( | |
R.add(3), | |
val => { | |
// eslint-disable-next-line fp/no-mutation | |
counter += 1; | |
return R.add(5, val); | |
}, | |
R.inc | |
); | |
fn(3); | |
fn(3); | |
fn(7); | |
fn(3); | |
expect(counter).toBe(2); | |
}); | |
}); |
This file contains 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
const R = require('ramda'); | |
const { Maybe: { fromNullable } } = require('zoomjs'); | |
/** | |
* Returns Maybe object with result of transformation of applied value | |
* (a -> b) -> a -> Maybe a | |
*/ | |
const mapToMaybe = R.curry((transformer, val) => fromNullable(transformer(val))); | |
module.exports = { mapToMaybe }; |
This file contains 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
const R = require('ramda'); | |
const { Maybe } = require('zoomjs'); | |
const mapToMaybe = require('./map-to-maybe').mapToMaybe; | |
describe('mapToMaybe', () => { | |
test('returns Maybe object', () => { | |
const result = mapToMaybe(R.add(5))(3); | |
expect(result).toBeInstanceOf(Maybe); | |
}); | |
test('returns Maybe object with a value of transformation result', () => { | |
const result = mapToMaybe(R.add(5))(3); | |
const value = result.withDefault(0); | |
expect(value).toBe(8); | |
}); | |
test('returns Nothing for null value', () => { | |
const result = mapToMaybe(R.always(null))(3); | |
expect(result.isNothing()).toBeTruthy(); | |
}); | |
test('returns Nothing for undefined value', () => { | |
const result = mapToMaybe(() => {})(3); | |
expect(result.isNothing()).toBeTruthy(); | |
}); | |
test('returns Maybe.Just object with a value of NaN', () => { | |
const result = mapToMaybe(R.add(5))('str'); | |
const value = result.withDefault(0); | |
expect(isNaN(value)).toBeTruthy(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment