Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the isJsonString method to the json module. #5

Merged
merged 1 commit into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ flattenObject(obj);
```


#### isJsonString(str)

Returns a boolean that specifies whether the string is parsable JSON.

```JavaScript
const str = JSON.stringify({red: 5, green: 6});

isJsonString(str); // true
```


#### stringToBoolean(str)

Usually used for url parameters, converts null, undefined, 0, false, or '' to false even if they are strings. All other values are true.
Expand Down
9 changes: 9 additions & 0 deletions lib/json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const _ = require('lodash');

function isJsonString(str) {
try {
return _.isObject(JSON.parse(str));
} catch (e) {
return false;
}
}

function safeJsonParse(obj) {
if (obj && _.isString(obj)) {
try {
Expand All @@ -12,5 +20,6 @@ function safeJsonParse(obj) {
}

module.exports = {
isJsonString,
safeJsonParse
};
38 changes: 36 additions & 2 deletions test/json.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
const { expect } = require('chai');

const { safeJsonParse } = require('../');
const { isJsonString, safeJsonParse } = require('../');

describe('json', function () {
describe('isJsonString()', function () {
it('should return false if passed undefined', function () {
expect(isJsonString(undefined)).to.be.false;
});

it('should return false if passed null', function () {
expect(isJsonString(null)).to.be.false;
});

it('should return false if passed a blank string', function () {
expect(isJsonString('')).to.be.false;
});

it('should return false if passed a number', function () {
expect(isJsonString(555)).to.be.false;
});

it('should return false if passed an invalid json object string', function () {
expect(isJsonString('{"red":5,"green":55,"blue":Koolaid"}')).to.be.false;
});

it('should return false if passed an invalid json array string', function () {
expect(isJsonString('["Red","Green","Blue"')).to.be.false;
});

it('should return true if passed a valid json object string', function () {
expect(isJsonString('{"red":5,"green":55,"blue":"Koolaid"}')).to.be.true;
});

it('should return true if passed a valid json array string', function () {
expect(isJsonString('["Red","Green","Blue"]')).to.be.true;
});
});

describe('json', function() {
describe('safeJsonParse()', function () {
it('should return null if passed undefined', function () {
expect(safeJsonParse(undefined)).to.be.null;
Expand Down