Created
July 15, 2016 19:07
-
-
Save StreetStrider/199568dfb1b3bf753b554d74b7dac8aa to your computer and use it in GitHub Desktop.
ensurer
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
function Ensure (check, action) | |
{ | |
return function ensurer (data) | |
{ | |
return capture(() => check(data)) | |
.then(so => | |
{ | |
if (! so) | |
{ | |
if (! action) | |
{ | |
throw new TypeError('Ensure: cannot handle') | |
} | |
return capture(() => action(data)) | |
} | |
}) | |
} | |
} | |
Ensure.directory_exists = () => | |
{ | |
var fs = require('fs-sync') | |
return Ensure(fs.exists, fs.mkdir) | |
} | |
Ensure.directory_must_exist = () => | |
{ | |
var exists = require('fs-sync').exists | |
return Ensure(dir => | |
{ | |
if (! exists(dir)) | |
{ | |
throw new TypeError('directory_must_exist') | |
} | |
return true | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment