Last active
June 18, 2016 12:46
-
-
Save Istar-Eldritch/cc74ae658aaa4f3c6801fa94e8e63498 to your computer and use it in GitHub Desktop.
Deep Read function in TypeScript
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
import {promisify, all} from 'bluebird'; | |
import {readdir as _rd, lstat as _ls} from 'fs' | |
import {flatten} from 'ramda' | |
let readdir = promisify(_rd) | |
let lstat = promisify(_ls) | |
export function reader<T>(path:string, f: (string) => T): Promise<T[]> { | |
async function iter(path: string): Promise<T[]> { | |
let stat = await lstat(path) | |
if(stat.isDirectory()) { | |
let paths = await readdir(path) | |
let routes = await all(paths.map((file) => { | |
return iter(`${path}/${file}`) | |
})) | |
console.log(routes) | |
return flatten(routes) | |
} | |
else { | |
let route: T = f(path) | |
return [route]; | |
} | |
} | |
return iter(path) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment