Permalink
Browse files

initial commit

  • Loading branch information...
thejh committed Jan 15, 2012
0 parents commit 02f9c45be468e83eb6ded5612a05be6bbcc23285
Showing with 80 additions and 0 deletions.
  1. +36 −0 README.md
  2. +31 −0 index.js
  3. +13 −0 package.json
@@ -0,0 +1,36 @@
+Switchable, modular, stackable features for your code. Turn on any transformations you'd like to use.
+
+Usage example:
+
+ [jann@Jann-PC tmp]$ cat test.xjs
+ // :X-FEATURE: destructure
+
+ var a, b, c;
+ var val = [{x: 5, y: 6}, 7];
+ [{x: a, y: b}, c] = val;
+ console.log('how it should be: a=5='+a+', b=6='+b+', c=7='+c);
+ [jann@Jann-PC tmp]$ node
+ > require('features')
+ {}
+ > require('./test')
+ how it should be: a=5=5, b=6=6, c=7=7
+ {}
+
+So, basically, for people who want to use such features:
+
+ - install `feature-<name>` modules for the features you want
+ - use `.xjs` as file extension
+ - in your main script, do this on top: `require('features')`
+ - in your scripts, put lines like this one **ON TOP** (no lines, even no empty ones, may be on top of them): `// :X-FEATURE: destructure`
+
+Note that your main script can't use such features - you might want to use a shim script.
+
+Also note that although this module should just work, the `destructure` feature doesn't do some important things as of 15.01.2012
+(most importantly, caching results).
+
+For people who want to make features:
+
+ - make a new npm package named `feature-<name>`
+ - in its main file, add a function `transformCodeString(code)` to the exports
+
+See https://github.com/thejh/node-feature-destructure/ for an example.
@@ -0,0 +1,31 @@
+function stripBOM(content) {
+ // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
+ // because the buffer-to-string conversion in `fs.readFileSync()`
+ // translates it to FEFF, the UTF-16 BOM.
+ if (content.charCodeAt(0) === 0xFEFF) {
+ content = content.slice(1);
+ }
+ return content;
+}
+
+if (require.extensions['.xjs']) return
+require.extensions['.xjs'] = function(module, filename) {
+ var content = require('fs').readFileSync(filename, 'utf8');
+ content = stripBOM(content)
+
+ var lines = content.split('\n')
+ for (var i=0; i<lines.length; i++) {
+ var line = lines[i].split(':').map(function(line) { return line.trim() })
+ if (line[1] !== 'X-FEATURE' || line.length !== 3) break
+ line = line[2]
+ try {
+ var transformation = module.parent.require('feature-'+line).transformCodeString
+ } catch (err) {
+ err.message = 'could not require feature-'+line+', maybe you have to npm install it?\n' + err.message
+ throw err
+ }
+ content = transformation(content)
+ }
+
+ module._compile(content, filename);
+}
@@ -0,0 +1,13 @@
+{
+ "author": "Jann Horn <jannhorn@googlemail.com>",
+ "name": "features",
+ "description": "per-file switchable code pre-transformations",
+ "version": "0.1.0",
+ "homepage": "https://github.com/thejh/node-features",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/thejh/node-features.git"
+ },
+ "dependencies": {},
+ "devDependencies": {}
+}

0 comments on commit 02f9c45

Please sign in to comment.