-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (126 loc) · 3.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
var fs = require('fs');
var extend = require('node.extend');
var is = require('is');
var promiseback = require('promiseback');
var Promise = promiseback.Deferred.Promise;
var checkKey = function checkKey(key) {
if ((typeof key !== 'string' || key.length === 0) && typeof key !== 'symbol') {
throw new TypeError('key must be a Symbol, or a nonempty string');
}
};
var JSONData = function JSONData(raw) {
var hasTrailingNewline = (/\n\n$/).test(raw);
var indentMatch = String(raw).match(/^[ \t]+/m);
var indent = indentMatch ? indentMatch[0] : 2;
this.format = {
indent: indent,
trailing: hasTrailingNewline
};
if (raw) {
this.data = JSON.parse(raw);
}
};
JSONData.prototype.getAllBlankSpelling = function(callback) {
console.log("inside getAllBlankSpelling");
var data = extend({}, this.data);
var value = [];
for (var key in data) {
if (data[key] == "") {
var tempObj = {};
tempObj[key] = data[key];
value.push(tempObj);
}
}
callback(value);
}
JSONData.prototype.get = function(key, callback) {
var data = extend({}, this.data);
if (is.fn(key)) {
callback = key;
key = null;
}
var value = key ? data[key] : data;
if (is.hash(value)) {
value = extend({}, value);
}
var deferred = promiseback(callback);
deferred.resolve(value);
return deferred.promise;
};
JSONData.prototype.set = function(obj) {
if (!is.hash(obj)) {
throw new TypeError('object must be a plain object');
}
extend(true, this.data, obj);
};
JSONData.prototype.remove = function(key, callback) {
var data = this.data;
var deletion = Promise.resolve().then(function() {
checkKey(key);
var status = delete data[key];
if (!status) {
return Promise.reject(new Error('deletion failed'));
}
return void 0;
});
return promiseback(deletion, callback);
};
JSONData.prototype.stringify = function stringify() {
var endingNewlines = this.format.trailing ? '\n\n' : '\n';
var indent = this.format.indent || 2;
return new Buffer(JSON.stringify(this.data, null, indent) + endingNewlines);
};
var JSONFile = function JSONFile(filename, raw) {
JSONData.call(this, raw);
this.filename = filename;
};
JSONFile.prototype = new JSONData();
JSONFile.prototype.save = function(callback) {
var deferred = promiseback(callback);
fs.writeFile(this.filename, this.stringify(), function(err, result) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(result);
}
});
return deferred.promise;
};
JSONFile.prototype.saveSync = function() {
fs.writeFileSync(this.filename, this.stringify());
};
var readJSON = function readJSON(filename) {
var callback;
if (arguments.length > 1) {
callback = arguments[1];
if (!is.fn(callback)) {
throw new TypeError('callback must be a function if provided');
}
}
var deferred = promiseback(callback);
fs.readFile(filename, {
encoding: 'utf8'
}, function(err, raw) {
var file;
if (err) {
deferred.reject(err);
} else {
try {
file = new JSONFile(filename, raw);
deferred.resolve(file);
} catch (e) {
deferred.reject(e);
}
}
});
return deferred.promise;
};
var readJSONSync = function readJSONSync(filename) {
var raw = fs.readFileSync(filename, 'utf8');
return new JSONFile(filename, raw);
};
readJSON.sync = readJSONSync;
readJSON.JSONFile = JSONFile;
readJSON.JSONData = JSONData;
module.exports = readJSON;