Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/refractalize/lsdir
Attempt to refactor some confusing node
https://github.com/refractalize/lsdir
Last synced: about 1 month ago
JSON representation
Attempt to refactor some confusing node
- Host: GitHub
- URL: https://github.com/refractalize/lsdir
- Owner: refractalize
- Created: 2011-02-24T23:42:16.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2011-03-05T09:21:17.000Z (almost 14 years ago)
- Last Synced: 2024-10-13T07:06:48.858Z (2 months ago)
- Language: Ruby
- Homepage:
- Size: 638 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lsdir
A particularly ugly piece of node - see if you can do better!
## The Challenge
`lsdir` is a node program that lists all the directories in the current directory. Ok, not very exciting or useful, but it demonstrates just how complicated node's asynchronous non-blocking API can make your code.
The challenge is to use `fs.readdir()` and `fs.stat()` (yes, the async ones) to read a directory listing, and for each file name returned, check if it's a directory. If it is, print it. Finally, print the number of directories found.
Sounds easy? Sure. But given the async nature of `fs.stat()`, you can't be sure which callback is the last one, and therefore, when to print the directory count.
The challenge is, then, to rewrite or refactor the lsdir.js code into something that aint ugly, and exposes the true intentions of the code.
## The Tests
Tests are written in ruby/rspec, so a quick `rspec lsdir_spec.rb` will show you if you're still on the right track. There's also a specs.watchr file, so you can run `watchr specs.watchr` if you want save-by-save test feedback.
# The Result
My solution to this is `zo`, an async friendly list processing library containing all the usual suspects `map`, `select` and `reduce`. See the [zo branch](https://github.com/refractalize/lsdir/tree/zo), or see the [zo repository](https://github.com/refractalize/zo).