forked from node-modules/utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request node-modules#12 from node-modules/strict-json
feat(JSON): add strict JSON parse
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*! | ||
* utility - lib/crypto.js | ||
* | ||
* MIT Licensed | ||
*/ | ||
|
||
"use strict"; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
exports.strictJSONParse = function (str) { | ||
var obj = JSON.parse(str); | ||
if (!obj || typeof obj !== 'object') { | ||
throw new Error('JSON string is not object'); | ||
} | ||
return obj; | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/**! | ||
* utility - test/json.test.js | ||
* | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* fengmk2 <[email protected]> (http://fengmk2.github.com) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var utils = require('../'); | ||
|
||
describe('json.test.js', function () { | ||
describe('strictJSONParse()', function () { | ||
it('should parse normal json ok', function () { | ||
var obj = utils.strictJSONParse('{"foo": "bar"}'); | ||
obj.should.eql({foo: 'bar'}); | ||
}); | ||
|
||
it('should parse error when invalid json', function () { | ||
(function () { | ||
utils.strictJSONParse('{'); | ||
}).should.throw(); | ||
}); | ||
|
||
it('should parse error when non-object json', function () { | ||
(function () { | ||
utils.strictJSONParse('"hello"'); | ||
}).should.throw(); | ||
}); | ||
|
||
it('should parse error when null json', function () { | ||
(function () { | ||
utils.strictJSONParse('null'); | ||
}).should.throw(); | ||
}); | ||
}); | ||
}); |