Skip to content

Commit

Permalink
v1.4.3. Improvement. Bugfix.
Browse files Browse the repository at this point in the history
- v1.4.3 August 30, 2013
	- Better error handling when requiring a file that has syntax errors
	- Fixed stringify of '{}' giving '{{}}' which is invalid
		- Closes [issue #21](#21)
  • Loading branch information
balupton committed Aug 30, 2013
1 parent 2ca92e8 commit 527feaa
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 89 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## History

- v1.4.3 August 30, 2013
- Better error handling when requiring a file that has syntax errors
- Fixed stringify of '{}' giving '{{}}' which is invalid
- Closes [issue #21](https://github.com/bevry/cson/issues/21)

- v1.4.2 June 7, 2013
- Updated dependencies

Expand Down
110 changes: 56 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
{
"name": "cson",
"version": "1.4.2",
"description": "CoffeeScript-Object-Notation Parser. Same as JSON but for CoffeeScript objects",
"homepage": "https://github.com/bevry/cson",
"keywords": [
"javascript",
"coffeescript",
"json",
"cson",
"parse",
"stringify"
],
"author": "Bevry Pty Ltd <us@bevry.me> (http://bevry.me)",
"maintainers": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)"
],
"contributors": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)",
"Ryan LeFevre <meltingice8917@gmail.com> (https://github.com/meltingice)",
"Linus G Thiel <linus@hanssonlarsson.se> (https://github.com/linus)",
"Zhang Cheng <czhang.oss@gmail.com> (https://github.com/zhangcheng)",
"Nicolae Claudius <claudius.nicolae@gmail.com> (https://github.com/clyfe)",
"Joel Perras <joel.perras@gmail.com> (https://github.com/jperras)"
],
"bugs": {
"url": "https://github.com/bevry/cson/issues"
},
"repository" : {
"type": "git",
"url": "http://github.com/bevry/cson.git"
},
"engines" : {
"node": ">=0.6.0"
},
"dependencies": {
"coffee-script": "~1.6.3",
"js2coffee": "~0.1.4"
},
"devDependencies": {
"chai": "~1.6.1",
"joe": "~1.2.0",
"joe-reporter-console": "~1.2.1"
},
"directories": {
"lib": "./out/lib"
},
"scripts": {
"test": "node ./out/test/everything.test.js"
},
"bin": {
"cson2json": "./bin/cson2json",
"json2cson": "./bin/json2cson"
},
"main": "./out/lib/cson"
"name": "cson",
"version": "1.4.3",
"description": "CoffeeScript-Object-Notation Parser. Same as JSON but for CoffeeScript objects",
"homepage": "https://github.com/bevry/cson",
"keywords": [
"javascript",
"coffeescript",
"json",
"cson",
"parse",
"stringify"
],
"author": "Bevry Pty Ltd <us@bevry.me> (http://bevry.me)",
"maintainers": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)"
],
"contributors": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)",
"Ryan LeFevre <meltingice8917@gmail.com> (https://github.com/meltingice)",
"Linus G Thiel <linus@hanssonlarsson.se> (https://github.com/linus)",
"Zhang Cheng <czhang.oss@gmail.com> (https://github.com/zhangcheng)",
"Nicolae Claudius <claudius.nicolae@gmail.com> (https://github.com/clyfe)",
"Joel Perras <joel.perras@gmail.com> (https://github.com/jperras)"
],
"bugs": {
"url": "https://github.com/bevry/cson/issues"
},
"repository": {
"type": "git",
"url": "http://github.com/bevry/cson.git"
},
"engines": {
"node": ">=0.8.0"
},
"dependencies": {
"coffee-script": "~1.6.3",
"js2coffee": "~0.1.4",
"extract-opts": "~2.2.0",
"requirefresh": "~1.1.1"
},
"devDependencies": {
"chai": "~1.7.2",
"joe": "~1.3.0",
"joe-reporter-console": "~1.2.1"
},
"directories": {
"lib": "./out/lib"
},
"scripts": {
"test": "node ./out/test/everything.test.js"
},
"bin": {
"cson2json": "./bin/cson2json",
"json2cson": "./bin/json2cson"
},
"main": "./out/lib/cson"
}
49 changes: 16 additions & 33 deletions src/lib/cson.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@ coffee = require('coffee-script')
js2coffee = require('js2coffee')
fsUtil = require('fs')
pathUtil = require('path')
{extractOpts} = require('extract-opts')
{requireFreshSafe} = require('requirefresh')

# Awesomeness
wait = (delay,fn) -> setTimeout(fn,delay)
wait = (delay,fn) -> setTimeout(fn, delay)

# Define
CSON =
# Parse a CSON file
# next(err,obj)
# next(err, obj)
parseFile: (filePath,opts,next) ->
# Prepare
if opts? is true and next? is false
next = opts
opts = null
opts or= {}
[opts, next] = extractOpts(opts, next)

# Resolve
filePath = pathUtil.resolve(filePath)

# Try require
if /\.(js|coffee)$/.test(filePath)
try
delete require.cache[filePath] # clear require cache for the config file
result = require(filePath)
delete require.cache[filePath] # clear require cache for the config file
catch err
return next(err,result)
finally
return next(null,result)
requireFreshSafe(filePath, next)

# Try read
else if /\.(json|cson)$/.test(filePath)
Expand All @@ -40,7 +32,7 @@ CSON =

# Parse
dataStr = data.toString()
@parse(dataStr,opts,next)
@parse(dataStr, opts, next)

# Unknown
else
Expand All @@ -61,13 +53,7 @@ CSON =

# Try require
if /\.(js|coffee)$/.test(filePath)
try
delete require.cache[filePath] # clear require cache for the config file
result = require(filePath)
delete require.cache[filePath] # clear require cache for the config file
return result
catch err
return err
result = requireFreshSafe(filePath)

# Try read
else if /\.(json|cson)$/.test(filePath)
Expand All @@ -80,7 +66,7 @@ CSON =
else
# Parse the result
dataStr = data.toString()
result = @parseSync(dataStr,opts)
result = @parseSync(dataStr, opts)

# Return
return result
Expand All @@ -95,24 +81,21 @@ CSON =
# next(err,obj)
parse: (src,opts,next) ->
# Prepare
if opts? is true and next? is false
next = opts
opts = null
opts or= {}
[opts, next] = extractOpts(opts, next)

# currently the parser only exists in a synchronous version
# so we use an instant timeout to simulate async code without any overhead
wait 0, =>
# Parse
result = @parseSync(src,opts)
result = @parseSync(src, opts)

# Check for error
if result instanceof Error
# Error
next(result)
else
# Success
next(null,result)
next(null, result)

# Chain
@
Expand All @@ -136,7 +119,7 @@ CSON =
result = err

# Return
result
return result


# Turn an object into CSON
Expand All @@ -154,7 +137,7 @@ CSON =
next(result)
else
# Success
next(null,result)
next(null, result)

# Chain
@
Expand All @@ -169,12 +152,12 @@ CSON =
result = result.replace(/^\s*result\s*\=\s/,'')
if typeof obj is 'object'
unless Array.isArray(obj)
result = '{\n'+result+'\n}'
result = '{\n'+result+'\n}' unless result is '{}'
catch err
result = err

# Return
result
return result


# Export
Expand Down
2 changes: 1 addition & 1 deletion src/test/async.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ joe.describe 'async', (describe,it) ->
expect(actualCsonStr).to.equal(expectedCsonStr)

# Create Tests
testExtensions = ['cson','cson','cson','json','coffee','js','cson']
testExtensions = ['cson','cson','cson','json','coffee','js','cson','cson']
for testExtension,i in testExtensions
createTest(testExtension,i)
2 changes: 1 addition & 1 deletion src/test/sync.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ joe.describe 'sync', (describe,it) ->
expect(actualCsonStr).to.equal(expectedCsonStr)

# Create Tests
testExtensions = ['cson','cson','cson','json','coffee','js','cson']
testExtensions = ['cson','cson','cson','json','coffee','js','cson','cson']
for testExtension,i in testExtensions
createTest(testExtension,i)
1 change: 1 addition & 0 deletions test/out-expected/8.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/out-expected/8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/src/8.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 527feaa

Please sign in to comment.