From 3bbd55c36af78a5c98ee2dab1ffacf7328999cd2 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Tue, 9 Dec 2014 17:43:05 -0500 Subject: [PATCH] Refactoring code for escape display --- src/js/parser/javascript/charset_escape.js | 4 +- src/js/parser/javascript/escape.js | 95 +++++++++++----------- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/js/parser/javascript/charset_escape.js b/src/js/parser/javascript/charset_escape.js index 331f678..f5bec9b 100644 --- a/src/js/parser/javascript/charset_escape.js +++ b/src/js/parser/javascript/charset_escape.js @@ -4,7 +4,5 @@ import Escape from './escape.js'; export default _.extend({}, Escape, { type: 'charset_escape', - codeMap: _.extend({}, Escape.codeMap, { - b: 'backspace' - }) + b: 'backspace' }); diff --git a/src/js/parser/javascript/escape.js b/src/js/parser/javascript/escape.js index 97e2314..0b82d8e 100644 --- a/src/js/parser/javascript/escape.js +++ b/src/js/parser/javascript/escape.js @@ -4,61 +4,64 @@ import Base from './base.js'; export default _.extend({}, Base, { type: 'escape', - codeMap: { - b: 'word boundary', - B: 'non-word boundary', - d: 'digit', - D: 'non-digit', - f: 'form feed', - n: 'line feed', - r: 'carriage return', - s: 'white space', - S: 'non-white space', - t: 'tab', - v: 'vertical tab', - w: 'word', - W: 'non-word', - 1: 'Back reference (group = 1)', - 2: 'Back reference (group = 2)', - 3: 'Back reference (group = 3)', - 4: 'Back reference (group = 4)', - 5: 'Back reference (group = 5)', - 6: 'Back reference (group = 6)', - 7: 'Back reference (group = 7)', - 8: 'Back reference (group = 8)', - 9: 'Back reference (group = 9)', - 0: (arg) => { - if (arg) { - return 'octal: ' + arg; - } else { - return 'null'; - } - }, - c: (arg) => { - return 'ctrl-' + arg; - }, - x: (arg) => { - return '0x' + arg.toUpperCase(); - }, - u: (arg) => { - return 'U+' + arg.toUpperCase(); - } + code() { + return this.esc.code.textValue; + }, + + arg() { + return this.esc.arg.textValue; }, render() { - var code = this.codeMap[this.esc.code.textValue]; - - if (_.isFunction(code)) { - code = code(this.esc.arg.textValue); - } - this.container.addClass('escape'); - this.label = this.render_label(this.container, code); + this.label = this.render_label(this.container, _.result(this, this.code())); this.label.select('rect').attr({ rx: 3, ry: 3 }); + }, + + // Escape code mappings + b: 'word boundary', + B: 'non-word boundary', + d: 'digit', + D: 'non-digit', + f: 'form feed', + n: 'line feed', + r: 'carriage return', + s: 'white space', + S: 'non-white space', + t: 'tab', + v: 'vertical tab', + w: 'word', + W: 'non-word', + 1: 'Back reference (group = 1)', + 2: 'Back reference (group = 2)', + 3: 'Back reference (group = 3)', + 4: 'Back reference (group = 4)', + 5: 'Back reference (group = 5)', + 6: 'Back reference (group = 6)', + 7: 'Back reference (group = 7)', + 8: 'Back reference (group = 8)', + 9: 'Back reference (group = 9)', + 0() { + var arg = this.arg(); + + if (arg) { + return 'octal: ' + arg; + } else { + return 'null'; + } + }, + c() { + return 'ctrl-' + this.arg(); + }, + x() { + return '0x' + this.arg().toUpperCase(); + }, + u() { + return 'U+' + this.arg().toUpperCase(); } });