1

I've been searching for ways to make my code more concise, so I want to find a way to automatically replace an identifier with an abbrevation of that identifier (whenever I type that identifier). It would be similar to the "autocorrect" feature that is found in some word processors.

Example:

var con = console.log;
//from this point on, whenever I type "console.log" as a variable name, I want the text to be automatically replaced with "con"
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • I'd need some way to avoid replacing matches of `con` inside string literals, like this: `"con"`. I'd most likely need to use a Javascript parser (to distinguish between identifiers and string literals). – Anderson Green Dec 18 '12 at 06:30
  • Some IDEs have automatic code replacement features, don't they? – Anderson Green Dec 18 '12 at 06:46
  • As a workaround, I could simply prevent a function from being called in a certain section of code, and then allow it to be called again after a certain point - it's a clumsy workaround, but it might work in a very limited number of scenarios. – Anderson Green Dec 18 '12 at 07:12

2 Answers2

2

use

var con = function(str){console.log(str);};
con('hello world'); //=> hello world

It looks like you want to be able to use a kind of intellisense or autocomplete. Your editor should provide that. You can try Komodo Edit or Visual Studio (2012). In Komodo Edit the above code example leads to autocompletion (it shows con if you typed 'co').

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • I'm trying to automatically replace one identifier with another identifier (as described in the original post). – Anderson Green Dec 18 '12 at 06:31
  • I hope the wording of my question is clearer now - sorry for the confusion. – Anderson Green Dec 18 '12 at 06:32
  • You may be interested in some obfuscator then, like googles closure compiler: https://developers.google.com/closure/compiler/ – KooiInc Dec 18 '12 at 06:32
  • What I'm really trying to do is automatically replace variable names with abbreviations as I type (sort of like an AutoCorrect feature). I have clarified the question's wording again - hopefully it is less vague now. – Anderson Green Dec 18 '12 at 06:36
0

Write your code long hand, and run it through an obfuscator, such as one of the packages previously described

Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91
  • What I'm trying to do specifically is automatically replace variable names with abbreviations as I type - I don't think an obfuscator would do that. Also, an obfuscator would make code difficult to read, and I want it to remain human-readable. – Anderson Green Dec 18 '12 at 06:24
  • To what end, @AndersonGreen? – hd1 Dec 18 '12 at 06:55
  • I'm basically trying to find an "autocorrect" feature that encourages the use of abbreviations instead of full variable names - it's easier to learn abbreviations when they're being automatically suggested to you as you type. – Anderson Green Dec 18 '12 at 07:00