Permalink
Browse files

refactoring

  • Loading branch information...
1 parent 92bddf4 commit 2b8e30dde2bd46337157115f600f6889a1f68ae5 @tokuhirom committed Dec 27, 2011
Showing with 33 additions and 27 deletions.
  1. +33 −27 utf728.js
View
@@ -3,33 +3,39 @@ var Iconv = require('iconv').Iconv,
util = require('util'),
undefined;
-var utf827 = new Iconv('UTF-8', 'UTF-7');
-var utf728 = new Iconv('UTF-7', 'UTF-8');
-function convert(str) {
- if (str.match(/^\+/)) {
- console.log("7");
- return utf728.convert(str);
- } else {
- return utf827.convert(str);
- }
+function MyApp() {
+ this.utf827 = new Iconv('UTF-8', 'UTF-7');
+ this.utf728 = new Iconv('UTF-7', 'UTF-8');
}
+MyApp.prototype = {
+ run: function () {
+ var self = this;
+
+ var rli = rl.createInterface(process.stdin, process.stdout);
+
+ rli.setPrompt('utf7> ');
+
+ rli.on('line', function(cmd) {
+ var e = self.convert(cmd);
+ util.puts(e);
+
+ rli.prompt();
+ }).on('close', function () {
+ util.puts('');
+ process.stdin.destroy();
+ });
+
+ rli.prompt();
+ },
+ convert: function (str) {
+ if (str.match(/^\+/)) {
+ console.log("7");
+ return this.utf728.convert(str);
+ } else {
+ return this.utf827.convert(str);
+ }
+ }
+};
-// setup readline
-var rli = rl.createInterface(process.stdin, process.stdout);
-
-rli.setPrompt('utf7> ');
-
-rli.addListener('line', function(cmd) {
- var e = convert(cmd);
- util.puts(e);
-
- rli.prompt();
-});
-
-rli.addListener('close', function() {
- util.puts('');
- process.stdin.destroy();
-});
-
-rli.prompt();
+(new MyApp()).run();

0 comments on commit 2b8e30d

Please sign in to comment.