Comparing version 1.2.5 to 2.0.0
35
index.js
@@ -1,11 +0,3 @@ | ||
function isString (x) { | ||
var type = Object.prototype.toString.apply(x); | ||
return type.slice(type.indexOf(' ') + 1, -1) === 'String' && isNaN(Number(x)); | ||
} | ||
'use strict' | ||
function isObject (obj) { | ||
return obj === Object(obj) && !Array.isArray(obj); | ||
} | ||
module.exports = isJSON; | ||
@@ -24,9 +16,9 @@ isJSON.strict = strict; | ||
if (/\[(.*?)\]/.test(str)) { | ||
str = str.replace(/^\[/, '') | ||
.replace(/\]$/, '') | ||
.replace(/},{/g, '}\n{') | ||
.split(/\n/); | ||
return str.map(function (s) { return isJSON(s); }) | ||
.reduce(function (prev, curr) { return !!curr; }); | ||
if (/^\[(.*?)\]$/.test(str)) { | ||
return str.replace(/^\[/, '') | ||
.replace(/\]$/, '') | ||
.replace(/},{/g, '}\n{') | ||
.split(/\n/) | ||
.map(function (s) { return isJSON(s); }) | ||
.reduce(function (prev, curr) { return !!curr; }); | ||
} | ||
@@ -43,3 +35,12 @@ | ||
return false; | ||
} | ||
} | ||
} | ||
function isString (x) { | ||
return Object.prototype.toString.call(x) === '[object String]'; | ||
} | ||
function isObject (obj) { | ||
return Object.prototype.toString.call(obj) === '[object Object]'; | ||
} | ||
{ | ||
"name": "is-json", | ||
"version": "1.2.5", | ||
"version": "2.0.0", | ||
"description": "check if a string is a valid JSON string without using Try/Catch", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,24 +5,25 @@ var test = require('tape'); | ||
test('performe isJSON verifications', function (t) { | ||
t.plan(16); | ||
t.deepEqual(isJSON(null), false, '`null`, should return false'); | ||
t.deepEqual(isJSON(false), false, '`false`, should return false'); | ||
t.deepEqual(isJSON(''), false, '`\'\'`, should return false'); | ||
t.deepEqual(isJSON('normal string'), false, '\'normal string\', should return false'); | ||
t.deepEqual(isJSON(2014), false, '`2014`, should return false'); | ||
t.deepEqual(isJSON(2014.5), false, '`2014.5`, should return false'); | ||
t.deepEqual(isJSON([1,2,3,4]), false, '`[1,2,3,4]`, should return false'); | ||
t.deepEqual(isJSON({a: 12, b: [1,2,3]}), false, 'a JSON object `{a: 12, b: [1,2,3]},`, should return false'); | ||
t.deepEqual(isJSON({a: 12, b: [1,2,3]}, true), true, | ||
test('performe isJSON verifications', function (assert) { | ||
assert.deepEqual(isJSON('asdada[]asdadada sd asdasda das das'), false, '`asdada[]asdadada sd asdasda das das`, should return false'); | ||
assert.deepEqual(isJSON(null), false, '`null`, should return false'); | ||
assert.deepEqual(isJSON(false), false, '`false`, should return false'); | ||
assert.deepEqual(isJSON(''), false, '`\'\'`, should return false'); | ||
assert.deepEqual(isJSON('normal string'), false, '\'normal string\', should return false'); | ||
assert.deepEqual(isJSON(2014), false, '`2014`, should return false'); | ||
assert.deepEqual(isJSON(2014.5), false, '`2014.5`, should return false'); | ||
assert.deepEqual(isJSON([1,2,3,4]), false, '`[1,2,3,4]`, should return false'); | ||
assert.deepEqual(isJSON({a: 12, b: [1,2,3]}), false, 'a JSON object `{a: 12, b: [1,2,3]},`, should return false'); | ||
assert.deepEqual(isJSON({a: 12, b: [1,2,3]}, true), true, | ||
'a JSON object `{a: 12, b: [1,2,3]}` but pass the 2 arg as true (check objects too), should return true'); | ||
t.deepEqual(isJSON('{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}'), true, | ||
assert.deepEqual(isJSON('{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}'), true, | ||
'`{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}`, should return true'); | ||
t.deepEqual(isJSON('1,2,3'), false, '`1,2,3`, should return false'); | ||
t.deepEqual(isJSON('{1,2,3}'), false, '`{1,2,3}`, should return false'); | ||
t.deepEqual(isJSON('[{"a": 123}, {1,2,3}}]'), false, '`[{"a": 123, {1,2,3}}]`, should return false'); | ||
var cobj = '[{"a": {"aa": [1,2,3,4], "aaa": {"d": 1212}}}, {"b": "test", "c": [1,2,3], "date": "' + new Date() + '}]'; | ||
t.deepEqual(isJSON(cobj), true, cobj + ', should return true'); | ||
t.deepEqual(isJSON(new Date()), false, '`Date`, should return false'); | ||
t.deepEqual(isJSON.strict('{\n "config": 123,\n "test": "abcde" \n}'), true, '`{\n "config": 123,\n "test": "abcde" \n}`, should return true'); | ||
assert.deepEqual(isJSON('1,2,3'), false, '`1,2,3`, should return false'); | ||
assert.deepEqual(isJSON('{1,2,3}'), false, '`{1,2,3}`, should return false'); | ||
assert.deepEqual(isJSON('[{"a": 123}, {1,2,3}}]'), false, '`[{"a": 123, {1,2,3}}]`, should return false'); | ||
var cobj = '[{"a": {"aa": [1,2,3,4], "aaa": {"d": 1212}}}, {"b": "test", "c": [1,2,3], "date": "' + new Date() + '"}]'; | ||
assert.deepEqual(isJSON(cobj), true, cobj + ', should return true'); | ||
assert.deepEqual(isJSON(new Date()), false, '`Date`, should return false'); | ||
assert.deepEqual(isJSON.strict('{\n "config": 123,\n "test": "abcde" \n}'), true, '`{\n "config": 123,\n "test": "abcde" \n}`, should return true'); | ||
assert.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5648
57