Skip to content

Commit

Permalink
README and a parseFile() method!
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexJWayne committed Jan 13, 2012
1 parent b541500 commit 578392f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 73 deletions.
65 changes: 63 additions & 2 deletions README.md
Expand Up @@ -78,5 +78,66 @@ With the npm module installed, simply require it.
API
---

* `coffeeson.parse(coffeesonString)`
Parse a coffeeson string and return a JS native object
`.parse(coffeesonString)`
Simply parse a coffeeson string and return a JS native object.

coffeeson.parse('a:123').a #=> 123

`.parseFile(path)`
Asynchronously parse a coffeeson file and return a JS native object.

coffeeson.parseFile 'config.coffeeson', (err, result) ->
result.a #=> 123

`.toJSON(src)`
Parse a coffeeson string and return an equivalent JSON string.

coffeeson.toJSON 'a:123' #=> '{"a":123}'

`.toPrettyJSON(src)`
Parse a coffeeson string and return an equivalent JSON string, nicely indented.

coffeeson.toPrettyJSON 'a:b:123' #=> '''
{
"a": {
"b": 123
}
}
'''

`.fileToJSON(path, [callback(err, json)])
Asynchronously read a file and callback with the content as JSON.

coffeeson.fileToJSON 'config.coffeeson', (err, json) ->
json #=> '{"a":{"b":123}}'

`.fileToPrettyJSON(path, [callback(err, json)])`
Asynchronously read a file and callback with the content as pretty and indented JSON.

coffeeson.fileToPrettyJSON 'config.coffeeson', (err, json) ->
json #=> '''
{
"a": {
"b": 123
}
}
'''

`.convertFile(path, [callback(err)])`
Asynchronously read a file and save a .json file right next the source file.

coffeeson.convertFile 'config.coffeeson', (err) ->
# "config.json" now contains JSON version of "config.coffeeson"

`.convertFilePretty(path, [callback(err)])`
Asynchronously read a file and save a pretty and indented .json file right next the source file. Works exactly like `convertFile()`










7 changes: 7 additions & 0 deletions lib/coffeeson.coffee
Expand Up @@ -10,6 +10,12 @@ compile = (src) ->
parse = (src) ->
eval compile(src)

# Parse a coffeeson file and return a JS native object
parseFile = (name, cb) ->
fs.readFile name, (err, src) ->
return cb err if err
cb no, parse(src)

# Parse a coffeeson string and return JSON
toJSON = (src, replacer, spacer) ->
JSON.stringify parse(src), replacer, spacer
Expand Down Expand Up @@ -46,6 +52,7 @@ convertFilePretty = (name, cb) ->
# Export methods
module.exports = {
parse
parseFile
toJSON
toPrettyJSON
fileToJSON
Expand Down
69 changes: 0 additions & 69 deletions lib/coffeeson.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"url": "git://github.com/Squeegy/coffeeson.npm.git"
},
"bugs": {
"url": "https://github.com/balupton/cson.npm/issues"
"url": "http://github.com/Squeegy/coffeeson.npm/issues"
},
"licenses": [
{
Expand Down
9 changes: 8 additions & 1 deletion test/coffeeson_test.coffee
Expand Up @@ -47,7 +47,14 @@ describe 'coffeeson', ->
it '.parse() parses coffeeson', ->
result = coffeeson.parse src.complex
JSON.stringify(result).should.equal json.complex


it '.parseFile() parses a coffeeson file and returns a native JS object', (done) ->
coffeeson.parseFile 'test/sample.coffeeson', (err, result) ->
err.should.not.be.ok
JSON.stringify(result).should.equal json.complex
done()


it '.toJSON() jsonifies coffeeson', ->
result = coffeeson.toJSON src.complex
result.should.equal json.complex
Expand Down

0 comments on commit 578392f

Please sign in to comment.