Skip to content

Commit

Permalink
Merge 812a11e into d2c240b
Browse files Browse the repository at this point in the history
  • Loading branch information
DonutEspresso committed Aug 15, 2019
2 parents d2c240b + 812a11e commit 8b3532a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 57 deletions.
57 changes: 22 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Licensed under the MIT license.
*/


/*
Sometimes JSON comes back with values as a string:
Expand Down Expand Up @@ -34,45 +33,42 @@ This module helps you do that.
*/


'use strict';

var safeParse = require('safe-json-parse/tuple');
var lodash = require('lodash');
const safeParse = require('safe-json-parse/tuple');
const lodash = require('lodash');

// const
var DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS = {
boolean: true,
float: true,
exponential: false,
array: true,
object: true
};

var EXPONENTIAL_REGEX = /^[\-+]?[\d]*\.?[\d]*e[+\-]?[\d]+$/;
const EXPONENTIAL_REGEX = /^[\-+]?[\d]*\.?[\d]*e[+\-]?[\d]+$/;

/**
* normalize options values
* @private
* @method normalizeOptions
* @param {Object} options an options object
* @return {Object} a normalized options object
*/
*/
function normalizeOptions(options) {


// if options wasn't passed in, set it to the defaults
if (!options) {
return DEFAULT_OPTIONS;
} else {
// otherwise, loop through the passed in options and fill in any
// missing values with defaults
var key;
var opts = {};
let key;
const opts = {};

for (key in DEFAULT_OPTIONS) {
if (DEFAULT_OPTIONS.hasOwnProperty(key)) {
var defaultVal = DEFAULT_OPTIONS[key];
const defaultVal = DEFAULT_OPTIONS[key];

// if passed in option not undefined, copy it
if (typeof options[key] !== 'undefined') {
Expand All @@ -88,7 +84,6 @@ function normalizeOptions(options) {
}
}


/**
* Loop through all key/val pairs,
* and attempt to JSON parse those values.
Expand All @@ -101,9 +96,8 @@ function normalizeOptions(options) {
* @return {Object} a possibly parsed JSON object
*/
function parseJson(data, options, normalized) {

var key;
var parsed = {};
let key;
const parsed = {};

// loop through the key/val pairs
for (key in data) {
Expand All @@ -115,9 +109,6 @@ function parseJson(data, options, normalized) {
return parsed;
}




/**
* parse a value out of a string, maybe
* @private
Expand All @@ -128,14 +119,12 @@ function parseJson(data, options, normalized) {
* @return {Object | String | Boolean | Number} anything!
*/
function parse(val, options, normalized) {

// if input is not a string, return it right away.
if (typeof val !== 'string') {
return val;
}

var opts = (normalized === true) ?
options : normalizeOptions(options);
const opts = normalized === true ? options : normalizeOptions(options);

if (opts.boolean === true) {
if (val === 'true') {
Expand All @@ -146,14 +135,14 @@ function parse(val, options, normalized) {
}

if (opts.float === true && val.length > 0) {
var isExponential = EXPONENTIAL_REGEX.test(val);
const isExponential = EXPONENTIAL_REGEX.test(val);

// if we are not parsing exponentials, return exponentials as a string
if (isExponential && opts.exponential === false) {
return val;
}

var num = lodash.toNumber(val);
const num = lodash.toNumber(val);
if (isFinite(num)) {
return num;
}
Expand All @@ -163,13 +152,14 @@ function parse(val, options, normalized) {
if (opts.array === true || opts.object === true) {
// it's possible the string might be a JSON value.
// check if first or last characters are {}, [].
var firstChar = val[0];
var lastChar = val[val.length - 1];

if ((firstChar === '[' && lastChar === ']') ||
(firstChar === '{' && lastChar === '}')) {

var parsedTuple = safeParse(val);
const firstChar = val[0];
const lastChar = val[val.length - 1];

if (
(firstChar === '[' && lastChar === ']') ||
(firstChar === '{' && lastChar === '}')
) {
const parsedTuple = safeParse(val);
// if parse didn't throw, use the value we got.
if (!parsedTuple[0]) {
return parsedTuple[1];
Expand All @@ -181,11 +171,8 @@ function parse(val, options, normalized) {
return val;
}



module.exports = function factory(options) {

var opts = normalizeOptions(options);
const opts = normalizeOptions(options);

return {
parseJson: function parseJsonWithOptions(val) {
Expand Down
38 changes: 16 additions & 22 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
'use strict';

// external modules
var _ = require('lodash');
var assert = require('chai').assert;
const _ = require('lodash');
const assert = require('chai').assert;

// internal files
var lameJson = require('../lib/index.js');

const lameJson = require('../lib/index.js');

describe('lame-json node module.', function() {

var goodData = {
const goodData = {
foo: true,
raz: false,
bar: 123,
baz: {
hello: 'world'
},
qux: [ 1, 2, 3 ],
qux: [1, 2, 3],
xul: '1.2.3',
zub: 45.66,
buz: '1e6',
Expand All @@ -29,11 +27,11 @@ describe('lame-json node module.', function() {
badNumberInExponent: '53.3.5e3',
emptyString: ''
};
var stringData = {
const stringData = {
foo: 'true',
raz: 'false',
bar: '123',
baz: '{\"hello\": \"world\"}',
baz: '{"hello": "world"}',
qux: '[1, 2, 3]',
xul: '1.2.3',
zub: '45.66',
Expand All @@ -55,15 +53,15 @@ describe('lame-json node module.', function() {
};

it('should return JSON object as is', function() {
var newData = lameJson.parseJson(goodData);
const newData = lameJson.parseJson(goodData);

_.forEach(_.keys(newData), function(key) {
assert.strictEqual(newData[key], goodData[key]);
});
});

it('should return parsed JSON', function() {
var newData = lameJson.parseJson(stringData);
const newData = lameJson.parseJson(stringData);

assert.strictEqual(newData.foo, true);
assert.strictEqual(newData.raz, false);
Expand Down Expand Up @@ -91,7 +89,7 @@ describe('lame-json node module.', function() {
});

it('should not parse JSON with options set to false', function() {
var newData = lameJson.parseJson(stringData, {
const newData = lameJson.parseJson(stringData, {
boolean: false,
float: false,
array: false,
Expand All @@ -102,7 +100,7 @@ describe('lame-json node module.', function() {
});

it('should parse exponential strings when option set to true', function() {
var newData = lameJson.parseJson(stringData, {
const newData = lameJson.parseJson(stringData, {
exponential: true
});

Expand All @@ -111,7 +109,7 @@ describe('lame-json node module.', function() {
assert.strictEqual(newData.notANumber, '.');
assert.strictEqual(newData.leadingDot, 0.22);
assert.strictEqual(newData.endingDot, 22);
assert.strictEqual(newData.positiveExponent, 2e+2);
assert.strictEqual(newData.positiveExponent, 2e2);
assert.strictEqual(newData.negativeExponent, 2e-2);
assert.strictEqual(newData.negative, -2);
assert.strictEqual(newData.negativeFloat, -2.2);
Expand All @@ -122,8 +120,7 @@ describe('lame-json node module.', function() {
});

it('should parse partial JSON', function() {

var newData = lameJson.parseJson(stringData, {
const newData = lameJson.parseJson(stringData, {
boolean: false,
float: false
});
Expand Down Expand Up @@ -153,10 +150,8 @@ describe('lame-json node module.', function() {
assert.strictEqual(newData.emptyString, '');
});


it('should create instance with persistent options', function() {

var lame = lameJson({
const lame = lameJson({
boolean: false,
float: false
});
Expand All @@ -165,13 +160,12 @@ describe('lame-json node module.', function() {
assert.strictEqual(lame.parse('true'), 'true');
assert.strictEqual(lame.parse('1.2.3'), '1.2.3');

var qux = lame.parse(goodData.qux);
const qux = lame.parse(goodData.qux);
assert.strictEqual(qux[0], 1);
assert.strictEqual(qux[1], 2);
assert.strictEqual(qux[2], 3);


var newData = lame.parseJson(stringData, {
const newData = lame.parseJson(stringData, {
boolean: false,
float: false
});
Expand Down

0 comments on commit 8b3532a

Please sign in to comment.