Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
236 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var fs = require('fs'); | ||
|
||
|
||
|
||
var Persist = function(path){ | ||
this.path = path || 'persist.json'; | ||
this.load(); | ||
this.__defineGetter__('length', function(){ | ||
var c = 0; | ||
for(var i in this.map){ | ||
c++; | ||
} | ||
return c; | ||
}); | ||
} | ||
|
||
|
||
Persist.prototype = { | ||
|
||
constructor: Persist, | ||
|
||
load: function(){ | ||
var content; | ||
try{ | ||
content = fs.readFileSync(this.path, 'utf-8'); | ||
this.map = JSON.parse(content); | ||
}catch(err){ | ||
this.map = {}; | ||
} | ||
}, | ||
|
||
save: function(){ | ||
var content = JSON.stringify(this.map, null, 4); | ||
fs.writeFileSync(this.path, content, 'utf-8'); | ||
}, | ||
|
||
getItem: function(key){ | ||
this.load(); | ||
return this.map[key]; | ||
}, | ||
|
||
setItem: function(key, value){ | ||
this.load(); | ||
this.map[key] = value; | ||
this.save(); | ||
}, | ||
|
||
removeItem: function(key, value){ | ||
this.load(); | ||
delete this.map[key]; | ||
this.save(); | ||
}, | ||
|
||
clear: function(){ | ||
this.map = {}; | ||
this.save(); | ||
}, | ||
|
||
|
||
each : function(callback){ | ||
for(var i in this.map){ | ||
callback(i, this.map[i]); | ||
} | ||
} | ||
}; | ||
|
||
exports.Persist = Persist; | ||
exports.persist = new Persist(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"service": "http://www.smushit.com/ysmush.it/ws.php" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
var Persist = require('../lib/persist').Persist; | ||
var nodeunit = require("nodeunit"); | ||
|
||
var persist; | ||
|
||
exports['persist'] = nodeunit.testCase({ | ||
|
||
setUp: function(callback){ | ||
persist = new Persist(); | ||
callback(); | ||
}, | ||
|
||
tearDown: function(callback){ | ||
persist.clear(); | ||
callback(); | ||
}, | ||
|
||
"persist getItem": function(test){ | ||
test.equal(null, persist.getItem('a')); | ||
test.done(); | ||
}, | ||
|
||
"persist setItem & removeItem": function(test){ | ||
persist.clear(); | ||
test.equal(null, persist.getItem('a')); | ||
persist.setItem('a', 'A'); | ||
test.equal('A', persist.getItem('a')); | ||
persist.removeItem('a'); | ||
test.equal(null, persist.getItem('a')); | ||
test.done(); | ||
}, | ||
|
||
"persist clear": function(test){ | ||
test.equal(null, persist.getItem('a')); | ||
persist.setItem('a', 'A'); | ||
persist.setItem('b', 'B'); | ||
persist.setItem('c', 'C'); | ||
test.equal('A', persist.getItem('a')); | ||
test.equal('B', persist.getItem('b')); | ||
test.equal('C', persist.getItem('c')); | ||
persist.removeItem('a'); | ||
test.equal(null, persist.getItem('a')); | ||
test.equal('B', persist.getItem('b')); | ||
test.equal('C', persist.getItem('c')); | ||
persist.clear(); | ||
test.equal(null, persist.getItem('b')); | ||
test.equal(null, persist.getItem('c')); | ||
test.done(); | ||
}, | ||
|
||
"persist length": function(test){ | ||
persist.clear(); | ||
test.equal(0, persist.length); | ||
persist.setItem('a', 'A'); | ||
test.equal(1, persist.length); | ||
persist.setItem('b', 'B'); | ||
persist.setItem('c', 'C'); | ||
test.equal(3, persist.length); | ||
persist.removeItem('a'); | ||
test.equal(2, persist.length); | ||
persist.clear(); | ||
test.equal(0, persist.length); | ||
test.done(); | ||
}, | ||
|
||
"persist each": function(test){ | ||
persist.clear(); | ||
var m = { | ||
'a': 'A', | ||
'b': 'B', | ||
'c': 'C' | ||
} | ||
persist.setItem('a', 'A'); | ||
persist.setItem('b', 'B'); | ||
persist.setItem('c', 'C'); | ||
persist.each(function(key, value){ | ||
test.equal(m[key], value); | ||
delete m[key]; | ||
}); | ||
var mKeyCount = 0; | ||
for(var i in m){ | ||
mKeyCount ++; | ||
} | ||
test.equal(0, mKeyCount); | ||
test.done(); | ||
} | ||
|
||
}); |