Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Sync interface #3

Merged
merged 1 commit into from

2 participants

@clyfe

Sync interface with tests.

Please message me when package is pushed to NPM registry.

@clyfe

Can you please pull this ? Should be fairly simple.

@balupton balupton merged commit eceaa6a into bevry:master
@balupton
Owner

Pulled and Published. Thanks a whole bunch Nicolae, very much appreciated!

@clyfe

Great, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 30, 2011
  1. @clyfe

    sync interface

    clyfe authored
This page is out of date. Refresh to see the latest.
Showing with 57 additions and 3 deletions.
  1. +24 −3 lib/cson.coffee
  2. +5 −0 package.json
  3. +28 −0 test/cson_sync_test.coffee
View
27 lib/cson.coffee
@@ -2,9 +2,11 @@
coffee = require 'coffee-script'
fs = require 'fs'
+
# Exports
module.exports =
-
+
+
# Parse a CSON file
# next(err,obj)
parseFile: (filePath,next) ->
@@ -14,7 +16,8 @@ module.exports =
# Done
return
-
+
+
# Parse a CSON string
# next(err,obj)
parse: (src,next) ->
@@ -37,7 +40,8 @@ module.exports =
# Done
return
-
+
+
# Turn an object into JSON/CSON
# next(err,str)
stringify: (obj,next) ->
@@ -54,3 +58,20 @@ module.exports =
# Done
return
+
+
+ # Parse a CSON file
+ parseFileSync: (filePath) -> @parseSync fs.readFileSync(filePath).toString()
+
+
+ # Parse a CSON string Synchronously
+ parseSync: (src) ->
+ try # Try parse JSON first
+ JSON.parse src
+ catch err # Now try parse CSON
+ eval coffee.compile('return '+src)
+
+
+ # Turn an object into JSON/CSON Synchronously
+ stringifySync: (obj) -> JSON.stringify obj
+
View
5 package.json
@@ -28,6 +28,11 @@
"name": "Benjamin Lupton",
"email": "b@lupton.cc",
"web": "http://balupton.com"
+ },
+ {
+ "name": "Nicolae Claudius",
+ "email": "nicolae{underscore}claudius{at}yahoo{dot}com",
+ "web": "http://clyfe.github.com"
}
],
"bugs": {
View
28 test/cson_sync_test.coffee
@@ -0,0 +1,28 @@
+# Requires
+assert = require 'assert'
+CSON = require __dirname+'/../lib/cson.coffee'
+fs = require 'fs'
+srcFile = __dirname+'/src/src.cson'
+outFile = __dirname+'/src/out.json'
+
+
+# Tests
+tests =
+
+ parsing: ->
+
+ # Read expectations
+ objExpected = JSON.parse fs.readFileSync(outFile).toString()
+ strExpected = JSON.stringify objExpected
+
+ # Test parseFileSync
+ obj = CSON.parseFileSync srcFile
+ assert.deepEqual(obj, objExpected, 'parsing: parseFile: result matched expected')
+
+ # Test Stringify
+ str = CSON.stringifySync obj
+ assert.equal(str, strExpected, 'parsing: stringify: result matched expected')
+
+
+module.exports = tests
+
Something went wrong with that request. Please try again.