From 09e0f4be3351c318190dd69236e0107b3164bfa6 Mon Sep 17 00:00:00 2001 From: levym Date: Thu, 22 Oct 2015 14:56:41 +0100 Subject: [PATCH] Add typescript definition file. --- README.md | 10 +++++----- index.js | 14 ++++---------- jsonfile.d.ts | 7 +++++++ test/read-file-sync.test.js | 2 +- test/read-file.test.js | 12 ++++++------ test/write-file.test.js | 16 ++++++++-------- 6 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 jsonfile.d.ts diff --git a/README.md b/README.md index 0901d82..1255226 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Installation API --- -### readFile(filename, [options], callback) +### readFile(filename, callback, [options]) `options`: Pass in any `fs.readFile` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). @@ -54,7 +54,7 @@ console.dir(jsonfile.readFileSync(file)) ``` -### writeFile(filename, [options], callback) +### writeFile(filename, obj, callback, [options]) `options`: Pass in any `fs.writeFile` options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`. @@ -78,13 +78,13 @@ var jsonfile = require('jsonfile') var file = '/tmp/data.json' var obj = {name: 'JP'} -jsonfile.writeFile(file, obj, {spaces: 2}, function(err) { +jsonfile.writeFile(file, obj, function(err) { console.error(err) -}) +}, {spaces: 2}) ``` -### writeFileSync(filename, [options]) +### writeFileSync(filename, obj, [options]) `options`: Pass in any `fs.writeFileSync` options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`. diff --git a/index.js b/index.js index aaf2c81..e803b4a 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,7 @@ var fs = require('fs') -function readFile (file, options, callback) { - if (callback == null) { - callback = options - options = {} - } +function readFile (file, callback, options) { + options = options || {} fs.readFile(file, options, function (err, data) { if (err) return callback(err) @@ -42,11 +39,8 @@ function readFileSync (file, options) { } } -function writeFile (file, obj, options, callback) { - if (callback == null) { - callback = options - options = {} - } +function writeFile (file, obj, callback, options) { + options = options || {} var spaces = typeof options === 'object' && options !== null ? 'spaces' in options diff --git a/jsonfile.d.ts b/jsonfile.d.ts new file mode 100644 index 0000000..98faed1 --- /dev/null +++ b/jsonfile.d.ts @@ -0,0 +1,7 @@ +declare module "jsonfile" { + var spaces: number; + function readFile(filename: string, callback:Function, options?:any); + function readFileSync(filename: string, options?:any); + function writeFile(filename: string, obj:any, callback:Function, options?:any); + function writeFileSync(filename: string, obj:any, options?:any); +} \ No newline at end of file diff --git a/test/read-file-sync.test.js b/test/read-file-sync.test.js index 06df60f..4b6deab 100644 --- a/test/read-file-sync.test.js +++ b/test/read-file-sync.test.js @@ -43,7 +43,7 @@ describe('+ readFileSync()', function () { jf.readFileSync(file) }, function (err) { assert(err instanceof Error) - assert(err.message.match(file)) + assert(err.message.match(/somefile\.json/)) return true }) }) diff --git a/test/read-file.test.js b/test/read-file.test.js index 634d4b3..ed8c62c 100644 --- a/test/read-file.test.js +++ b/test/read-file.test.js @@ -40,7 +40,7 @@ describe('+ readFile()', function () { jf.readFile(file, function (err, obj2) { assert(err instanceof Error) - assert(err.message.match(file)) + assert(err.message.match(/somefile\.json/)) done() }) }) @@ -61,13 +61,13 @@ describe('+ readFile()', function () { } fs.writeFileSync(file, JSON.stringify(obj)) - jf.readFile(file, {reviver: sillyReviver}, function (err, data) { + jf.readFile(file, function (err, data) { assert.ifError(err) assert.strictEqual(data.name, 'jp') assert(data.day instanceof Date) assert.strictEqual(data.day.toISOString(), '2015-06-19T11:41:26.815Z') done() - }) + }, {reviver: sillyReviver}) }) }) @@ -80,7 +80,7 @@ describe('+ readFile()', function () { } fs.writeFileSync(file, JSON.stringify(obj)) - jf.readFile(file, null, function (err) { + jf.readFile(file, function (err) { assert.ifError(err) assert.strictEqual(obj.name, 'jp') done() @@ -97,11 +97,11 @@ describe('+ readFile()', function () { } fs.writeFileSync(file, JSON.stringify(obj)) - jf.readFile(file, 'utf8', function (err) { + jf.readFile(file, function (err) { assert.ifError(err) assert.strictEqual(obj.name, 'jp') done() - }) + }, 'utf8') }) }) }) diff --git a/test/write-file.test.js b/test/write-file.test.js index caccaa5..80ceddf 100644 --- a/test/write-file.test.js +++ b/test/write-file.test.js @@ -70,7 +70,7 @@ describe('+ writeFile()', function () { reg: new RegExp(/hello/g) } - jf.writeFile(file, obj, {replacer: sillyReplacer}, function (err) { + jf.writeFile(file, obj, function (err) { assert.ifError(err) var data = JSON.parse(fs.readFileSync(file)) @@ -78,7 +78,7 @@ describe('+ writeFile()', function () { assert.strictEqual(typeof data.reg, 'string') assert.strictEqual(data.reg, 'regex:/hello/g') done() - }) + }, {replacer: sillyReplacer}) }) }) @@ -86,10 +86,10 @@ describe('+ writeFile()', function () { it('should not throw an error', function (done) { var file = path.join(TEST_DIR, 'somefile.json') var obj = { name: 'jp' } - jf.writeFile(file, obj, null, function (err) { + jf.writeFile(file, obj, function (err) { assert.ifError(err) done() - }) + }, null) }) }) @@ -97,12 +97,12 @@ describe('+ writeFile()', function () { it('should write file with spaces', function (done) { var file = path.join(TEST_DIR, 'somefile.json') var obj = { name: 'jp' } - jf.writeFile(file, obj, {spaces: 8}, function (err) { + jf.writeFile(file, obj, function (err) { assert.ifError(err) var data = fs.readFileSync(file, 'utf8') assert.strictEqual(data, JSON.stringify(obj, null, 8) + '\n') done() - }) + }, {spaces: 8}) }) }) @@ -110,12 +110,12 @@ describe('+ writeFile()', function () { it('should not error', function (done) { var file = path.join(TEST_DIR, 'somefile.json') var obj = { name: 'jp' } - jf.writeFile(file, obj, 'utf8', function (err) { + jf.writeFile(file, obj, function (err) { assert.ifError(err) var data = fs.readFileSync(file, 'utf8') assert.strictEqual(data, JSON.stringify(obj) + '\n') done() - }) + }, 'utf8') }) }) })