Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Sep 8, 2018
1 parent 72b60b5 commit 2c38bf4
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 260 deletions.
48 changes: 23 additions & 25 deletions index.js
@@ -1,4 +1,4 @@
var _fs
let _fs
try {
_fs = require('graceful-fs')
} catch (_) {
Expand All @@ -17,24 +17,24 @@ function readFileWithCallback (file, options, callback) {
}

options = options || {}
var fs = options.fs || _fs
const fs = options.fs || _fs

var shouldThrow = true
let shouldThrow = true
if ('throws' in options) {
shouldThrow = options.throws
}

fs.readFile(file, options, function (err, data) {
fs.readFile(file, options, (err, data) => {
if (err) return callback(err)

data = stripBom(data)

var obj
let obj
try {
obj = JSON.parse(data, options ? options.reviver : null)
} catch (err2) {
if (shouldThrow) {
err2.message = file + ': ' + err2.message
err2.message = `${file}: ${err2.message}`
return callback(err2)
} else {
return callback(null, null)
Expand All @@ -53,20 +53,20 @@ function readFileSync (file, options) {
options = { encoding: options }
}

var fs = options.fs || _fs
const fs = options.fs || _fs

var shouldThrow = true
let shouldThrow = true
if ('throws' in options) {
shouldThrow = options.throws
}

try {
var content = fs.readFileSync(file, options)
let content = fs.readFileSync(file, options)
content = stripBom(content)
return JSON.parse(content, options.reviver)
} catch (err) {
if (shouldThrow) {
err.message = file + ': ' + err.message
err.message = `${file}: ${err.message}`
throw err
} else {
return null
Expand All @@ -75,8 +75,8 @@ function readFileSync (file, options) {
}

function stringify (obj, options) {
var spaces
var EOL = '\n'
let spaces
let EOL = '\n'
if (typeof options === 'object' && options !== null) {
if (options.spaces) {
spaces = options.spaces
Expand All @@ -86,7 +86,7 @@ function stringify (obj, options) {
}
}

var str = JSON.stringify(obj, options ? options.replacer : null, spaces)
const str = JSON.stringify(obj, options ? options.replacer : null, spaces)

return str.replace(/\n/g, EOL) + EOL
}
Expand All @@ -97,15 +97,13 @@ function writeFileWithCallback (file, obj, options, callback) {
options = {}
}
options = options || {}
var fs = options.fs || _fs
const fs = options.fs || _fs

var str = ''
let str = ''
try {
str = stringify(obj, options)
} catch (err) {
// Need to return whether a callback was passed or not
if (callback) callback(err, null)
return
return callback(err, null)
}

fs.writeFile(file, str, options, callback)
Expand All @@ -115,9 +113,9 @@ const writeFile = universalify.fromCallback(writeFileWithCallback)

function writeFileSync (file, obj, options) {
options = options || {}
var fs = options.fs || _fs
const fs = options.fs || _fs

var str = stringify(obj, options)
const str = stringify(obj, options)
// not sure if fs.writeFileSync returns anything, but just in case
return fs.writeFileSync(file, str, options)
}
Expand All @@ -129,11 +127,11 @@ function stripBom (content) {
return content
}

var jsonfile = {
readFile: readFile,
readFileSync: readFileSync,
writeFile: writeFile,
writeFileSync: writeFileSync
const jsonfile = {
readFile,
readFileSync,
writeFile,
writeFileSync
}

module.exports = jsonfile
113 changes: 57 additions & 56 deletions test/read-file-sync.test.js
@@ -1,147 +1,148 @@
var assert = require('assert')
var fs = require('fs')
var os = require('os')
var path = require('path')
var rimraf = require('rimraf')
var jf = require('../')
const assert = require('assert')
const fs = require('fs')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const jf = require('../')

/* global describe it beforeEach afterEach */

describe('+ readFileSync()', function () {
var TEST_DIR
describe('+ readFileSync()', () => {
let TEST_DIR

beforeEach(function (done) {
beforeEach((done) => {
TEST_DIR = path.join(os.tmpdir(), 'jsonfile-tests-readfile-sync')
rimraf.sync(TEST_DIR)
fs.mkdir(TEST_DIR, done)
})

afterEach(function (done) {
afterEach((done) => {
rimraf.sync(TEST_DIR)
done()
})

it('should read and parse JSON', function () {
var file = path.join(TEST_DIR, 'somefile3.json')
var obj = { name: 'JP' }
it('should read and parse JSON', () => {
const file = path.join(TEST_DIR, 'somefile3.json')
const obj = { name: 'JP' }
fs.writeFileSync(file, JSON.stringify(obj))

try {
var obj2 = jf.readFileSync(file)
const obj2 = jf.readFileSync(file)
assert.strictEqual(obj2.name, obj.name)
} catch (err) {
assert(err)
}
})

describe('> when invalid JSON', function () {
it('should include the filename in the error', function () {
var fn = 'somefile.json'
var file = path.join(TEST_DIR, fn)
describe('> when invalid JSON', () => {
it('should include the filename in the error', () => {
const fn = 'somefile.json'
const file = path.join(TEST_DIR, fn)
fs.writeFileSync(file, '{')

assert.throws(function () {
assert.throws(() => {
jf.readFileSync(file)
}, function (err) {
}, (err) => {
assert(err instanceof Error)
assert(err.message.match(fn))
return true
})
})
})

describe('> when invalid JSON and throws set to false', function () {
it('should return null', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
describe('> when invalid JSON and throws set to false', () => {
it('should return null', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
const data = '{not valid JSON'
fs.writeFileSync(file, data)

assert.throws(function () {
assert.throws(() => {
jf.readFileSync(file)
})

var obj = jf.readFileSync(file, { throws: false })
const obj = jf.readFileSync(file, { throws: false })
assert.strictEqual(obj, null)
})
})

describe('> when invalid JSON and throws set to true', function () {
it('should throw an exception', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
describe('> when invalid JSON and throws set to true', () => {
it('should throw an exception', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')
const data = '{not valid JSON'
fs.writeFileSync(file, data)

assert.throws(function () {
assert.throws(() => {
jf.readFileSync(file, { throws: true })
})
})
})

describe('> when json file is missing and throws set to false', function () {
it('should return null', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
describe('> when json file is missing and throws set to false', () => {
it('should return null', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')

var obj = jf.readFileSync(file, { throws: false })
const obj = jf.readFileSync(file, { throws: false })
assert.strictEqual(obj, null)
})
})

describe('> when json file is missing and throws set to true', function () {
it('should throw an exception', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
describe('> when json file is missing and throws set to true', () => {
it('should throw an exception', () => {
const file = path.join(TEST_DIR, 'somefile4-invalid.json')

assert.throws(function () {
assert.throws(() => {
jf.readFileSync(file, { throws: true })
})
})
})

describe('> when JSON reviver is set', function () {
it('should transform the JSON', function () {
var file = path.join(TEST_DIR, 'somefile.json')
var sillyReviver = function (k, v) {
describe('> when JSON reviver is set', () => {
it('should transform the JSON', () => {
const file = path.join(TEST_DIR, 'somefile.json')
const sillyReviver = function (k, v) {
if (typeof v !== 'string') return v
if (v.indexOf('date:') < 0) return v
return new Date(v.split('date:')[1])
}

var obj = {
const obj = {
name: 'jp',
day: 'date:2015-06-19T11:41:26.815Z'
}

fs.writeFileSync(file, JSON.stringify(obj))
var data = jf.readFileSync(file, { reviver: sillyReviver })
const data = jf.readFileSync(file, { reviver: sillyReviver })
assert.strictEqual(data.name, 'jp')
assert(data.day instanceof Date)
assert.strictEqual(data.day.toISOString(), '2015-06-19T11:41:26.815Z')
})
})

describe('> when passing encoding string as option', function () {
it('should not throw an error', function () {
var file = path.join(TEST_DIR, 'somefile.json')
describe('> when passing encoding string as option', () => {
it('should not throw an error', () => {
const file = path.join(TEST_DIR, 'somefile.json')

var obj = {
const obj = {
name: 'jp'
}
fs.writeFileSync(file, JSON.stringify(obj))

let data
try {
var data = jf.readFileSync(file, 'utf8')
data = jf.readFileSync(file, 'utf8')
} catch (err) {
assert.ifError(err)
}
assert.strictEqual(data.name, 'jp')
})
})

describe('> w/ BOM', function () {
it('should properly parse', function () {
var file = path.join(TEST_DIR, 'file-bom.json')
var obj = { name: 'JP' }
fs.writeFileSync(file, '\uFEFF' + JSON.stringify(obj))
var data = jf.readFileSync(file)
describe('> w/ BOM', () => {
it('should properly parse', () => {
const file = path.join(TEST_DIR, 'file-bom.json')
const obj = { name: 'JP' }
fs.writeFileSync(file, `\uFEFF${JSON.stringify(obj)}`)
const data = jf.readFileSync(file)
assert.deepStrictEqual(obj, data)
})
})
Expand Down

0 comments on commit 2c38bf4

Please sign in to comment.