Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 2d34252600317b31b9f2c34edfb6a0188e9e6719 @hideo55 committed Nov 11, 2011
Showing with 127 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 .npmignore
  3. +5 −0 History.md
  4. +40 −0 Readme.md
  5. +2 −0 index.js
  6. +42 −0 lib/test-metrics.js
  7. +15 −0 package.json
  8. +6 −0 test.js
  9. +8 −0 test/test_sample1.js
5 .gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+node_modules
+*.sock
+.project
+.settings
4 .npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
5 History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
40 Readme.md
@@ -0,0 +1,40 @@
+
+# test-metrics
+
+ Test metrics of your code
+
+# Usage
+
+```javascript
+var testMetrics = require('test-metrics');
+assert.ok(!testMetrics.hasComplexCode('/path/to/sourcecode', 25));
+```
+
+# Issue
+
+Can't use it with some testing/spec framework cause of colision prototype modification.
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 Hideaki Ohno <hide.o.j55{at}gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/test-metrics');
42 lib/test-metrics.js
@@ -0,0 +1,42 @@
+/*!
+* test-metrics
+* Copyright(c) 2011 Hideaki Ohno <hide.o.j55{at}gmail.com>
+* MIT Licensed
+*/
+
+/**
+ * Library version.
+ */
+
+exports.version = '0.0.1';
+var tokens = require('jsmeter/tokens');
+tokens.setup();
+var complex = require('jsmeter/complexity').make_complexity();
+var parse = require('jsmeter/parse').make_parse();
+var fs = require('fs');
+var path = require('path');
+
+exports.hasComplexCode = function(src, threshold ) {
+ var jsonStr;
+ var res = {
+ write : function(t) {
+ jsonStr = t;
+ }
+ };
+ src = path.normalize(path.join(process.cwd(), src));
+
+ var source = fs.readFileSync(src, 'utf-8');
+
+ complex.complexity(parse(source), "code");
+ complex.renderStats(res, 'JSON');
+
+ var comp = JSON.parse(jsonStr);
+ var hasTooComplexCode = false;
+ comp.forEach(function(elm) {
+ if(elm.complexity > threshold) {
+ hasTooComplexCode = true;
+ }
+ });
+
+ return hasTooComplexCode;
+};
15 package.json
@@ -0,0 +1,15 @@
+{
+ "name": "test-metrics"
+ , "version": "0.0.1"
+ , "description": "Test metrics of your code"
+ , "keywords": []
+ , "author": "Hideaki Ohno <hide.o.j55{at}gmail.com>"
+ , "dependencies": {
+ "jsmeter": ""
+ }
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+ , "scripts" : {
+ "test" : "node test.js"
+ }
+}
6 test.js
@@ -0,0 +1,6 @@
+
+var testMetrics = require('./lib/test-metrics');
+var assert = require('assert');
+
+assert.ok(!testMetrics.hasComplexCode('./test/test_sample1.js',10));
+assert.ok(testMetrics.hasComplexCode('./test/test_sample1.js',1));
8 test/test_sample1.js
@@ -0,0 +1,8 @@
+function foo(bar) {
+ if(bar) {
+ (function(baz){
+ baz += 'qux';
+ console.log(baz);
+ })(bar);
+ }
+}

0 comments on commit 2d34252

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