Skip to content
Browse files

refactor

  • Loading branch information...
1 parent 1408a2b commit 6a73e0eb2133bf7721f66b6225d02255f7a0ee5d @chjj committed
Showing with 33 additions and 22 deletions.
  1. +33 −22 lib/tiny.js
View
55 lib/tiny.js
@@ -171,9 +171,7 @@ Tiny.prototype._parse = function(on, func) {
(function read() {
fs.read(fd, data, 0, data.length, pos, function(err, bytes) {
- if (err || !bytes) {
- return done(err);
- }
+ if (err || !bytes) return done(err);
var kstart = 0
, i = 0;
@@ -185,7 +183,7 @@ Tiny.prototype._parse = function(on, func) {
case 0x09:
state = 'data';
dstart = pos + 1;
- key += data.slice(kstart, i).toString('ascii');
+ key += data.toString('ascii', kstart, i);
break;
case 0x0A:
debug('Unexpected byte at offset: %d', pos);
@@ -212,7 +210,7 @@ Tiny.prototype._parse = function(on, func) {
}
if (state === 'key' && kstart < bytes) {
- key += data.slice(kstart).toString('ascii');
+ key += data.toString('ascii', kstart);
}
read();
@@ -497,15 +495,15 @@ Tiny.prototype.commit = function(func) {
on.attempt = 0;
}
- if (++on.attempt === 10) {
+ if (++on.attempt === 5) {
err.message = 'Write Error:\n'
+ err.message;
return func(err);
}
- return process.nextTick(function() {
+ return setTimeout(function() {
fs.write(fd, data, 0, data.length, self._total, on);
- });
+ }, 50);
}
self._total += bytes;
@@ -1012,45 +1010,58 @@ var has = function(obj, item) {
};
var para = function(obj, looper, func) {
- var c = 0
+ if (!obj) return func && func();
+
+ var j = 0
, i = 0
- , len
+ , l
, keys
, key;
if (typeof obj.length !== 'number'
|| typeof obj === 'function') {
keys = Object.keys(obj);
+ l = keys.length;
+ } else {
+ l = obj.length;
}
- len = (keys || obj).length;
- if (!len) return func && func();
+ if (!l) return func && func();
var next = function() {
- if (++c === len) func && func();
+ if (++j === l) func && func();
};
- for (; i < len; i++) {
+ for (; i < l; i++) {
key = keys ? keys[i] : i;
looper(next, obj[key], key, i);
}
};
var serial = function(obj, looper, func) {
+ if (!obj) return func && func();
+
var i = 0
- , keys;
+ , keys
+ , l;
if (typeof obj.length !== 'number'
|| typeof obj === 'function') {
keys = Object.keys(obj);
+ l = keys.length;
+ (function next() {
+ if (i === l) return func && func();
+ var j = i++, key = keys[j];
+ looper(next, obj[key], key, j);
+ })();
+ } else {
+ l = obj.length;
+ (function next() {
+ if (i === l) return func && func();
+ var j = i++;
+ looper(next, obj[j], j, j);
+ })();
}
-
- (function next() {
- var key = keys ? keys[i++] : i++
- , val = obj[key];
- if (!val) return func && func();
- looper(next, val, key, i);
- })();
};
var stringify = function(val) {

0 comments on commit 6a73e0e

Please sign in to comment.
Something went wrong with that request. Please try again.