-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module.exports = require('./src').Archetype; | ||
module.exports.to = require('./src/to'); | ||
|
||
module.exports.CastError = require('./src').CastError; | ||
module.exports.matchType = require('./src/helpers/matchType'); | ||
module.exports.to = require('./src/to'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Given a mapping from types ('object', 'string', 'boolean', 'undefined', | ||
* 'number', 'symbol', or 'function') to functions, returns a function that | ||
* takes a `value` and replaces it with the return value from the function | ||
* mapped to by `typeof value`. | ||
* | ||
* Example: `matchType({ 'string': JSON.parse })` will return a function that | ||
* takes a value and only calls `JSON.parse()` if that value is a string. | ||
* | ||
* @param {Object} obj Maps from `typeof` value to function | ||
* @return {Function} transforms value based on `typeof` | ||
*/ | ||
|
||
module.exports = function(obj) { | ||
return function(v) { | ||
const type = typeof v; | ||
if (typeof obj[type] === 'function') { | ||
return obj[type](v); | ||
} | ||
return v; | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const { matchType } = require('../'); | ||
|
||
describe('matchType', function() { | ||
it('works with JSON.parse()', function() { | ||
const parse = matchType({ string: JSON.parse }); | ||
|
||
const obj = { hello: 'world' }; | ||
|
||
// If given a string, will parse it | ||
assert.deepEqual(parse(JSON.stringify(obj)), obj); | ||
|
||
// If not, will do nothing | ||
assert.strictEqual(parse(obj), obj); | ||
assert.strictEqual(parse(null), null); | ||
assert.strictEqual(parse(undefined), undefined); | ||
}); | ||
|
||
it('works with trimming strings', function() { | ||
const trim = matchType({ string: str => str.trim() }); | ||
|
||
// If given a string, will trim it | ||
assert.equal(trim(' abc '), 'abc'); | ||
assert.equal(trim('abc'), 'abc'); | ||
|
||
// If not, will do nothing | ||
const obj = {}; | ||
assert.strictEqual(trim(obj), obj); | ||
assert.strictEqual(trim(null), null); | ||
assert.strictEqual(trim(undefined), undefined); | ||
}); | ||
}); |