From eb63d04d380a9f21e54b5dadeffdc2791ac64653 Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 20:45:23 +0200 Subject: [PATCH 01/30] + added AMD and CommonJS capabilities # explicitly injecting window object into immediate function # smarter injection of mJsNamespace variable --- measurement.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/measurement.js b/measurement.js index 294df91..4dfeea7 100644 --- a/measurement.js +++ b/measurement.js @@ -11,9 +11,9 @@ * @param {Object} namespace * @returns {undefined} */ -(function(namespace) { +(function(win, namespace) { "use strict"; - window.measurement = MeasurementJs; + win.measurement = MeasurementJs; MeasurementJs.Unit = { Speed: { @@ -313,11 +313,20 @@ } if (typeof namespace !== 'undefined') { - window.MeasurementJs = undefined; + win.MeasurementJs = undefined; namespace.measurement = MeasurementJs; namespace.mJs = namespace.MeasurementJs; namespace.measurement.Converter = MeasurementConverter; } -})(typeof mJsNamespace !== 'undefined' ? mJsNamespace : undefined); + // AMD definition - i.e. for require.js + if (typeof win.define === "function" && win.define.amd) { + define("measurement", [], function() { + "use strict"; + return MeasurementJs; + }); + } else if (win.module !== undefined && win.module.exports) { + win.module.exports.measurementjs = MeasurementJs; + } +})(window, window.mJsNamespace); From c20d94904b126ada4faa88c6bafebec7f97e00ec Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 22:47:28 +0200 Subject: [PATCH 02/30] + add Karma :) --- .gitignore | 4 ++- README.md | 11 +++++--- karma.conf.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 +++++- test-main.js | 24 ++++++++++++++++++ 5 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 karma.conf.js create mode 100644 test-main.js diff --git a/.gitignore b/.gitignore index 14bc68c..6c39d04 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/nbproject/private/ \ No newline at end of file +/nbproject/private/ +/node_modules/ +/coverage/ \ No newline at end of file diff --git a/README.md b/README.md index 9d83f7e..721c7b1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # MeasurementJs -[![Build Status](https://travis-ci.org/Philzen/measurement.js.svg?branch=master)](https://travis-ci.org/Philzen/measurement.js) [![devDependency Status](https://david-dm.org/philzen/measurement.js/dev-status.svg?theme=shields.io)](https://david-dm.org/philzen/measurement.js#info=devDependencies) [![Code Climate](https://codeclimate.com/github/Philzen/measurement.js.png)](https://codeclimate.com/github/Philzen/measurement.js) +[![Build Status](https://travis-ci.org/Philzen/measurement.js.svg?branch=master)](https://travis-ci.org/Philzen/measurement.js) +[![NPM devDependencies Status](https://david-dm.org/philzen/measurement.js/dev-status.svg?theme=shields.io)](https://david-dm.org/philzen/measurement.js#info=devDependencies) +[![Code Climate](https://codeclimate.com/github/Philzen/measurement.js.png)](https://codeclimate.com/github/Philzen/measurement.js) Nice unit of measure conversion, featuring: - __Simplicity__: an easy to-use, Behaviour driven API @@ -29,13 +31,14 @@ measurement('Speed').convert(10) The API definition and all conversion operations are covered by jasmine tests. The test suite can be executed straightaway and easily, for example: - - In the Browser: + + - In the Browser: **currently broken** Simply open test/index.html in the browser environment you want to test - CLI-based (using npm) -`npm test` +`npm test` (which is just a shorthand for `node_modules/.bin/karma start` - - CLI-based (w/o npm, you will need to ensure phantomjs is available yourself) + - CLI-based (w/o npm & karma, you will need to ensure phantomjs is available yourself) **currently broken** `phantomjs test/phantomRunner.js test/index.html` ### Roadmap diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..fd8607a --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,69 @@ +// Karma configuration +// Generated on Tue Apr 01 2014 17:31:44 GMT+0200 (CEST) + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['jasmine', 'requirejs'], + + + // list of files / patterns to load in the browser + files: [ + 'test-main.js', + {pattern: '*.js', included: false}, + {pattern: 'test/**/*Spec.js', included: false} + ], + + + // list of files to exclude + exclude: [ + + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ['PhantomJS'], + + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false + }); +}; diff --git a/package.json b/package.json index 11a88cc..cda5eba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "measurementjs", - "version": "0.1.0", + "version": "0.1.1", "description": "Nice unit-of-measure conversion", "readmeFilename": "README.md", "main": "measurement.js", @@ -14,6 +14,11 @@ "test": "phantomjs test/phantomRunner.js test/index.html" }, "devDependencies": { + "karma": "^0.12.2", + "karma-cli": "0.0.4", + "karma-coverage": "^0.2.1", + "karma-jasmine": "^0.1.5", + "karma-phantomjs-launcher": "^0.1.2", "phantomjs": "^1.9.7-1" } } diff --git a/test-main.js b/test-main.js new file mode 100644 index 0000000..ad7c639 --- /dev/null +++ b/test-main.js @@ -0,0 +1,24 @@ +var allTestFiles = []; +var TEST_REGEXP = /(spec|test)\.js$/i; + +var pathToModule = function(path) { + return path.replace(/^\/base\//, '').replace(/\.js$/, ''); +}; + +Object.keys(window.__karma__.files).forEach(function(file) { + if (TEST_REGEXP.test(file)) { + // Normalize paths to RequireJS module names. + allTestFiles.push(pathToModule(file)); + } +}); + +require.config({ + // Karma serves files under /base, which is the basePath from your config file + baseUrl: '/base', + + // dynamically load all test files + deps: allTestFiles, + + // we have to kickoff jasmine, as it is asynchronous + callback: window.__karma__.start +}); From 04c5b40734051832612fc549c1e49547080a9db3 Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 22:57:16 +0200 Subject: [PATCH 03/30] # all tests passing with Karma+RequireJs # renamed test files to include 'spec' --- test/unit/mJs.ApiSpec.js | 154 +++++++++--------- test/unit/mJs.CustomNamespaceSpec.js | 37 ++++- test/unit/mJs.convert.Distance.js | 37 ----- test/unit/mJs.convert.DistanceSpec.js | 38 +++++ ...uration.js => mJs.convert.DurationSpec.js} | 2 + ...ressure.js => mJs.convert.PressureSpec.js} | 2 + ...vert.Speed.js => mJs.convert.SpeedSpec.js} | 3 + ...ture.js => mJs.convert.TemperatureSpec.js} | 4 + 8 files changed, 161 insertions(+), 116 deletions(-) delete mode 100644 test/unit/mJs.convert.Distance.js create mode 100644 test/unit/mJs.convert.DistanceSpec.js rename test/unit/{mJs.convert.Duration.js => mJs.convert.DurationSpec.js} (95%) rename test/unit/{mJs.convert.Pressure.js => mJs.convert.PressureSpec.js} (94%) rename test/unit/{mJs.convert.Speed.js => mJs.convert.SpeedSpec.js} (97%) rename test/unit/{mJs.convert.Temperature.js => mJs.convert.TemperatureSpec.js} (97%) diff --git a/test/unit/mJs.ApiSpec.js b/test/unit/mJs.ApiSpec.js index 281af7d..96b6bf8 100644 --- a/test/unit/mJs.ApiSpec.js +++ b/test/unit/mJs.ApiSpec.js @@ -1,76 +1,82 @@ 'use strict'; -describe("MeasurementJs API: ", function() { - - it("first call (i.e. `measurementJs('Speed')` can be kept as reference", function() { - expect(typeof measurement('Speed')).toBe('object'); - }); - - it("has a convert() function", function() { - expect(typeof measurement().convert).toBe('function'); - }); - - describe("measurement().convert(someValue)", function() { - var testUnitType = 'Distance', - testValue = 42, - testValue2 = 666, - convertFunction = measurement(testUnitType).convert(testValue), - convertFunction2 = measurement(testUnitType).convert(testValue2), - someUnit = 'km', - someOtherUnit = 'm'; - - it("returns an Object, which", function() { - expect(typeof convertFunction).toBe('object'); - }); - - it("has a from() and a to() function", function() { - expect(convertFunction.from).toBeDefined(); - expect(convertFunction.to).toBeDefined(); - }); - - describe("from()", function() { - it("returns the same object on first chained call", function() { - expect(convertFunction.from()).toBe(convertFunction); - }); - }); - - describe("from(someUnit)", function() { - var fromSomeUnitReturn = convertFunction.from(someUnit); - it("returns the same object on first chained call", function() { - expect(fromSomeUnitReturn).toBe(convertFunction); - }); - - it("returns a numeric value when to(someOtherUnit) is called on the returned object", function() { - // TODO find out why we need to do this again in this scope - BDD doesn't seem scrope safe here - var fromSomeUnitReturn = convertFunction.from(someUnit); - - var returnedOnSecondChainedCall = fromSomeUnitReturn.to(someOtherUnit); - expect(typeof returnedOnSecondChainedCall).toBe('number'); - }); - - it("returns *the same* value when to(someUnit) - the same unit - is called on the returned object", function() { - expect(measurement(testUnitType).convert(testValue).from(someUnit).to(someUnit)).toBe(testValue); - }); - - }); - - - describe("to()", function() { - it("returns the same object on first chained call", function() { - expect(convertFunction2.to()).toBe(convertFunction2); - }); - }); - - - describe("to(someUnit)", function() { - var toSomeUnitReturn = convertFunction2.to(someUnit); - it("returns the same object on first chained call", function() { - expect(toSomeUnitReturn).toBe(convertFunction2); - }); - - it("returns a value when from(someOtherUnit) is called on the returned object", function() { - expect(convertFunction2.to(someUnit).from(someOtherUnit)).toBeDefined(); - }); - }); - }); -}); \ No newline at end of file +define([ 'measurement' ], function( measurement ) { + + describe("MeasurementJs API: ", function() { + + it("first call (i.e. `measurementJs('Speed')` can be kept as reference", function() { + expect(typeof measurement('Speed')).toBe('object'); + }); + + it("has a convert() function", function() { + expect(typeof measurement().convert).toBe('function'); + }); + + describe("measurement().convert(someValue)", function() { + var testUnitType = 'Distance', + testValue = 42, + testValue2 = 666, + convertFunction = measurement(testUnitType).convert(testValue), + convertFunction2 = measurement(testUnitType) + .convert(testValue2), + someUnit = 'km', + someOtherUnit = 'm'; + + it("returns an Object, which", function() { + expect(typeof convertFunction).toBe('object'); + }); + + it("has a from() and a to() function", function() { + expect(convertFunction.from).toBeDefined(); + expect(convertFunction.to).toBeDefined(); + }); + + describe("from()", function() { + it("returns the same object on first chained call", function() { + expect(convertFunction.from()).toBe(convertFunction); + }); + }); + + describe("from(someUnit)", function() { + var fromSomeUnitReturn = convertFunction.from(someUnit); + it("returns the same object on first chained call", function() { + expect(fromSomeUnitReturn).toBe(convertFunction); + }); + + it("returns a numeric value when to(someOtherUnit) is called on the returned object", function() { + // TODO find out why we need to do this again in this scope - BDD doesn't seem scrope safe here + var fromSomeUnitReturn = convertFunction.from(someUnit); + + var returnedOnSecondChainedCall = fromSomeUnitReturn.to(someOtherUnit); + expect(typeof returnedOnSecondChainedCall).toBe('number'); + }); + + it("returns *the same* value when to(someUnit) - the same unit - is called on the returned object", function() { + expect(measurement(testUnitType).convert(testValue) + .from(someUnit).to(someUnit)).toBe(testValue); + }); + + }); + + + describe("to()", function() { + it("returns the same object on first chained call", function() { + expect(convertFunction2.to()).toBe(convertFunction2); + }); + }); + + + describe("to(someUnit)", function() { + var toSomeUnitReturn = convertFunction2.to(someUnit); + it("returns the same object on first chained call", function() { + expect(toSomeUnitReturn).toBe(convertFunction2); + }); + + it("returns a value when from(someOtherUnit) is called on the returned object", function() { + expect(convertFunction2.to(someUnit).from(someOtherUnit)) + .toBeDefined(); + }); + }); + }); + }); +}); diff --git a/test/unit/mJs.CustomNamespaceSpec.js b/test/unit/mJs.CustomNamespaceSpec.js index 24d44e9..3613fba 100644 --- a/test/unit/mJs.CustomNamespaceSpec.js +++ b/test/unit/mJs.CustomNamespaceSpec.js @@ -1,7 +1,34 @@ "use strict"; -describe("Assign a custom namespace for MeasurementJs:", function() { - it("just set window.mJsNamespace = YourNamespace before measurement.js include", function(){ - expect(OurCoolCustomNamespace.changeIT.measurement).toBeDefined(); - }); -}); \ No newline at end of file +define([ 'measurement' ], function( measurement ) { + + // inject namespace + var scriptNs = document.createElement('script'); + scriptNs.innerHTML = "var OurCoolCustomNamespace = { changeIT: {}};window.mJsNamespace = OurCoolCustomNamespace.changeIT;"; + document.getElementsByTagName('head')[0].appendChild(scriptNs); + + // load measurement.js once more + var script = document.createElement('script'); + script.onload = function() { + console.log("Reloaded measurement.js with custom namespace option"); + }; + script.src = "base/measurement.js"; + script.async = false; + document.getElementsByTagName('head')[0].appendChild(script); + + waitsFor(function() { + return typeof window.OurCoolCustomNamespace.changeIT.measurement !== 'undefined'; + }, "measurementJs to be loaded into custom namespace"); + + describe("Assign a custom namespace for MeasurementJs:", function() { + + + it("just set window.mJsNamespace = YourNamespace before measurement.js include", function() { + expect(OurCoolCustomNamespace.changeIT.measurement) + .toBeDefined(); + }); + + }); +}); + + diff --git a/test/unit/mJs.convert.Distance.js b/test/unit/mJs.convert.Distance.js deleted file mode 100644 index a8d0710..0000000 --- a/test/unit/mJs.convert.Distance.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -describe("measurement.Unit.Distance", function() { - expect(measurement.Unit.Distance).toBeDefined(); - - /** - * dc Distance Constants - * @type @exp;measurement@pro;Unit@pro;Distance - */ - var dc = measurement.Unit.Distance; - it('is an object', function() { - expect(typeof dc).toBe('object'); - }); - - it('which contains string keys that we can use as constants', function() { - for (var i in dc) { - expect(typeof dc[i]).toBe('string'); - expect(dc[i].length).toBeGreaterThan(0); - } - }); - - describe("convert():", function() { - - it('1 Distance.KILOMETRE equals 1000 Distance.METRE', function(){ - expect(measurement('Distance').convert(1).from(dc.KILOMETRE).to(dc.METRE) ).toBe(1000); - }); - - it('1000 Distance.MILLIMETRES equal 1 Distance.METRES', function(){ - expect(measurement('Distance').convert(1000).from(dc.MILLIMETRE).to(dc.METRE) ).toBe(1); - }); - - it('254 Distance.MILLIMETRES equal 10 Distance.INCH', function(){ - expect(measurement('Distance').convert(254).from(dc.MILLIMETRE).to(dc.INCH) ).toBe(10); - }); - - }); -}); diff --git a/test/unit/mJs.convert.DistanceSpec.js b/test/unit/mJs.convert.DistanceSpec.js new file mode 100644 index 0000000..16625ae --- /dev/null +++ b/test/unit/mJs.convert.DistanceSpec.js @@ -0,0 +1,38 @@ +'use strict'; + +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Distance", function() { + expect(measurement.Unit.Distance).toBeDefined(); + + var dc = measurement.Unit.Distance; + it('is an object', function() { + expect(typeof dc).toBe('object'); + }); + + it('which contains string keys that we can use as constants', function() { + for (var i in dc) { + expect(typeof dc[i]).toBe('string'); + expect(dc[i].length).toBeGreaterThan(0); + } + }); + + describe("convert():", function() { + + it('1 Distance.KILOMETRE equals 1000 Distance.METRE', function() { + expect(measurement('Distance').convert(1).from(dc.KILOMETRE) + .to(dc.METRE)).toBe(1000); + }); + + it('1000 Distance.MILLIMETRES equal 1 Distance.METRES', function() { + expect(measurement('Distance').convert(1000) + .from(dc.MILLIMETRE).to(dc.METRE)).toBe(1); + }); + + it('254 Distance.MILLIMETRES equal 10 Distance.INCH', function() { + expect(measurement('Distance').convert(254).from(dc.MILLIMETRE) + .to(dc.INCH)).toBe(10); + }); + + }); + }); +}); diff --git a/test/unit/mJs.convert.Duration.js b/test/unit/mJs.convert.DurationSpec.js similarity index 95% rename from test/unit/mJs.convert.Duration.js rename to test/unit/mJs.convert.DurationSpec.js index 045d2e9..035bf52 100644 --- a/test/unit/mJs.convert.Duration.js +++ b/test/unit/mJs.convert.DurationSpec.js @@ -1,5 +1,6 @@ "use strict"; +define([ 'measurement' ], function( measurement ) { describe("measurement.Unit.Duration", function() { expect(measurement.Unit.Duration).toBeDefined(); @@ -37,3 +38,4 @@ describe("measurement.Unit.Duration", function() { }); }); +}); \ No newline at end of file diff --git a/test/unit/mJs.convert.Pressure.js b/test/unit/mJs.convert.PressureSpec.js similarity index 94% rename from test/unit/mJs.convert.Pressure.js rename to test/unit/mJs.convert.PressureSpec.js index f0a2a64..3b1b62f 100644 --- a/test/unit/mJs.convert.Pressure.js +++ b/test/unit/mJs.convert.PressureSpec.js @@ -1,5 +1,6 @@ 'use strict'; +define([ 'measurement' ], function( measurement ) { describe("measurement.Unit.Pressure", function() { expect(measurement.Unit.Pressure).toBeDefined(); @@ -31,3 +32,4 @@ describe("measurement.Unit.Pressure", function() { }); }); }); +}); \ No newline at end of file diff --git a/test/unit/mJs.convert.Speed.js b/test/unit/mJs.convert.SpeedSpec.js similarity index 97% rename from test/unit/mJs.convert.Speed.js rename to test/unit/mJs.convert.SpeedSpec.js index 58f6689..62f06ae 100644 --- a/test/unit/mJs.convert.Speed.js +++ b/test/unit/mJs.convert.SpeedSpec.js @@ -1,5 +1,7 @@ "use strict"; +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Speed", function() { expect(measurement.Unit.Speed).toBeDefined(); @@ -67,3 +69,4 @@ describe("measurement.Unit.Speed", function() { }); }); +}); diff --git a/test/unit/mJs.convert.Temperature.js b/test/unit/mJs.convert.TemperatureSpec.js similarity index 97% rename from test/unit/mJs.convert.Temperature.js rename to test/unit/mJs.convert.TemperatureSpec.js index 4796f86..f3691cd 100644 --- a/test/unit/mJs.convert.Temperature.js +++ b/test/unit/mJs.convert.TemperatureSpec.js @@ -1,5 +1,7 @@ 'use strict'; +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Temperature", function() { expect(measurement.Unit.Temperature).toBeDefined(); @@ -66,3 +68,5 @@ describe("measurement.Unit.Temperature", function() { }); }); }); + +}); \ No newline at end of file From 9dd1ce08b55bc87bd71d671c04325f8ab50bac93 Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 22:58:15 +0200 Subject: [PATCH 04/30] # make `npm test` work again --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cda5eba..2d12df7 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "license": "MIT", "author": "Philzen ", "scripts": { - "test": "phantomjs test/phantomRunner.js test/index.html" + "test": "./node_modules/.bin/karma start" }, "devDependencies": { "karma": "^0.12.2", From c3843443851948e65fe4a98bbe0858e9ec82f31f Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 22:59:34 +0200 Subject: [PATCH 05/30] # add tested platforms --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 721c7b1..56d2209 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,15 @@ Clone from [Github](https://github.com/Philzen/measurement.js/) or install via N ![NPM](https://nodei.co/npm/measurementjs.png?downloads=true&stars=true) +### Currently tested platforms + +Please feel free to add your own test results. + + PhantomJS 1.9.7 (Linux): Executed 46 of 46 SUCCESS (0.027 secs / 0.022 secs) + Chrome 18.0.1025 (Linux): Executed 46 of 46 SUCCESS (0.06 secs / 0.035 secs) + Firefox 28.0.0 (Ubuntu): Executed 46 of 46 SUCCESS (0.037 secs / 0.026 secs) + + ### Inspiring projects - http://www.codeproject.com/Articles/23087/Measurement-Unit-Conversion-Library (a C# / XML based approach) From 8d4aa0e151b1ac5e425521547cee7d53ec64a4a1 Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 23:13:40 +0200 Subject: [PATCH 06/30] # add missing karma-requirejs plugin --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 2d12df7..ce63af3 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "karma-coverage": "^0.2.1", "karma-jasmine": "^0.1.5", "karma-phantomjs-launcher": "^0.1.2", + "karma-requirejs": "^0.2.1", "phantomjs": "^1.9.7-1" } } From 9c66da1b5987a569ce6a5a240a546c8b9934f5ff Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 23:14:31 +0200 Subject: [PATCH 07/30] # add build instructions --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56d2209..b728dee 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,13 @@ For current and future state of affairs, have a peek at the [Roadmap](ROADMAP.md ### Get it -Clone from [Github](https://github.com/Philzen/measurement.js/) or install via NPM: +master: Clone from [Github](https://github.com/Philzen/measurement.js/), then run `npm install` in the project root + +latest release - install via NPM: ![NPM](https://nodei.co/npm/measurementjs.png?downloads=true&stars=true) + ### Currently tested platforms Please feel free to add your own test results. From 0ef5254cbd1d8f1ec5af018982a3c8b4ef37d51e Mon Sep 17 00:00:00 2001 From: Philzen Date: Tue, 1 Apr 2014 23:21:48 +0200 Subject: [PATCH 08/30] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7dea91e..7332a9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1 @@ -script: phantomjs test/phantomRunner.js test/index.html \ No newline at end of file +script: npm test From f3d07266ee2ab88a6c5738ac3ecd3ca0323fb93b Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 00:11:44 +0200 Subject: [PATCH 09/30] # enable coverage reports # enable single mode --- karma.conf.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index fd8607a..ab983df 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -5,7 +5,7 @@ module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) - basePath: '', + basePath: './', // frameworks to use @@ -23,13 +23,17 @@ module.exports = function(config) { // list of files to exclude exclude: [ - + '.git', 'node_modules' ], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { + // source files, that you wanna generate coverage for + // do not include tests or libraries + // (these files will be instrumented by Istanbul via Ibrik) + './*.js': ['coverage'] }, @@ -37,7 +41,7 @@ module.exports = function(config) { // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress'], + reporters: ['coverage', 'progress'], // web server port @@ -64,6 +68,15 @@ module.exports = function(config) { // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits - singleRun: false + singleRun: true, + + coverageReporter: { + dir : './coverage/', + reporters:[ + {type: 'html'}, + {type: 'lcov'}, + {type: 'text-summary'} + ] + } }); }; From 5eab03d838765126f85e052a7f96fad635436367 Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 01:09:52 +0200 Subject: [PATCH 10/30] # include coveralls --- karma.conf.js | 23 ++++++++++++----------- package.json | 4 +++- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index ab983df..655bf3b 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -33,39 +33,31 @@ module.exports = function(config) { // source files, that you wanna generate coverage for // do not include tests or libraries // (these files will be instrumented by Istanbul via Ibrik) - './*.js': ['coverage'] - + 'measurement.js': ['coverage'] }, - // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['coverage', 'progress'], - // web server port port: 9876, - // enable / disable colors in the output (reporters and logs) colors: true, - // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, - // enable / disable watching file and executing tests whenever any file changes autoWatch: true, - // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: ['PhantomJS'], - // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: true, @@ -73,10 +65,19 @@ module.exports = function(config) { coverageReporter: { dir : './coverage/', reporters:[ - {type: 'html'}, {type: 'lcov'}, - {type: 'text-summary'} + {type: 'text', file: 'coverage.txt'}, + {type: 'text-summary', file: 'coverage-summary.txt'} ] + }, + + coveralls: { + options: { + debug: false, + coverage_dir: './coverage/*', + dryRun: false, + force: true + } } }); }; diff --git a/package.json b/package.json index ce63af3..5af2c57 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,11 @@ "license": "MIT", "author": "Philzen ", "scripts": { - "test": "./node_modules/.bin/karma start" + "karma": "./node_modules/.bin/karma start", + "test": "npm run-script karma && cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "devDependencies": { + "coveralls": "^2.10.0", "karma": "^0.12.2", "karma-cli": "0.0.4", "karma-coverage": "^0.2.1", From e6f93b70d1f7307166e9f38c6f4c3e5d5362eebd Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 01:11:07 +0200 Subject: [PATCH 11/30] Switch to nodejs engine --- .travis.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7332a9c..ed05f88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1,6 @@ -script: npm test +language: node_js +node_js: + - "0.11" + - "0.10" + - "0.8" + - "0.6" From ed97cbc22f05917fe605247c926039de0ab21050 Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 01:15:39 +0200 Subject: [PATCH 12/30] # remove node 0.6 build --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ed05f88..4a83e22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,3 @@ node_js: - "0.11" - "0.10" - "0.8" - - "0.6" From e5e5acf22ce45851b3d0fdef0c656200b7070087 Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 01:48:12 +0200 Subject: [PATCH 13/30] + Coveralls.io < lcov.info < travis CI < Karma :) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b728dee..06a70a8 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Nice unit of measure conversion, featuring: - __Simplicity__: an easy to-use, Behaviour driven API -- __Sophistication__: full test coverage from project day one +- __Sophistication__: [![Coverage Status](https://coveralls.io/repos/Philzen/measurement.js/badge.png?branch=master)](https://coveralls.io/r/Philzen/measurement.js?branch=master) full test coverage from project day one - __Quality__: aiming at high performance whilst maintaining a fair trade-off between accuracy - __Adaptability__: Easy to extend for new measurement types (incl. i18n tables) From 9e7e15eb42b65b0117db54992f6968b81b7f0061 Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 17:43:47 +0200 Subject: [PATCH 14/30] # updated devDependencies + include jslint preprocessor --- package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5af2c57..8e0a686 100644 --- a/package.json +++ b/package.json @@ -16,12 +16,13 @@ }, "devDependencies": { "coveralls": "^2.10.0", - "karma": "^0.12.2", - "karma-cli": "0.0.4", + "karma": "^0.12.3", + "karma-cli": "^0.0.4", "karma-coverage": "^0.2.1", "karma-jasmine": "^0.1.5", - "karma-phantomjs-launcher": "^0.1.2", "karma-requirejs": "^0.2.1", + "karma-jshint-preprocessor": "^0.0.2", + "karma-phantomjs-launcher": "^0.1.3", "phantomjs": "^1.9.7-1" } } From 3df32e4b70348e766f3d035b0218c6d5a0cb3b59 Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 20:29:16 +0200 Subject: [PATCH 15/30] + added Karma JSLint --- karma.conf.js | 24 +++++++++++++++++++++++- package.json | 4 ++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 655bf3b..81596a0 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -33,7 +33,7 @@ module.exports = function(config) { // source files, that you wanna generate coverage for // do not include tests or libraries // (these files will be instrumented by Istanbul via Ibrik) - 'measurement.js': ['coverage'] + 'measurement.js': ['jshint','coverage'] }, // test results reporter to use @@ -62,6 +62,28 @@ module.exports = function(config) { // if true, Karma captures browsers, runs the tests and exits singleRun: true, + jshint: { + options: { +// reporter: require('jshint-stylish'), + curly: true, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + boss: true, + devel: true, + eqnull: true, + browser: true, + globals: { + cordova: true, + jQuery: true + } + } + }, + coverageReporter: { dir : './coverage/', reporters:[ diff --git a/package.json b/package.json index 8e0a686..a4f99f5 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "karma-coverage": "^0.2.1", "karma-jasmine": "^0.1.5", "karma-requirejs": "^0.2.1", - "karma-jshint-preprocessor": "^0.0.2", + "karma-jshint": "philzen/karma-jshint.git", "karma-phantomjs-launcher": "^0.1.3", - "phantomjs": "^1.9.7-1" + "phantomjs": "^1.9.7-3" } } From e6066be1dffe9ac2f22a174aaa52edd56850af42 Mon Sep 17 00:00:00 2001 From: Philzen Date: Wed, 2 Apr 2014 22:24:12 +0200 Subject: [PATCH 16/30] # corrected run instructions --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b728dee..d1cd7ac 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,9 @@ The test suite can be executed straightaway and easily, for example: Simply open test/index.html in the browser environment you want to test - CLI-based (using npm) -`npm test` (which is just a shorthand for `node_modules/.bin/karma start` +`npm run-script karma` +That command just a shorthand for `node_modules/.bin/karma start`. If you have +`karma-cli` already available on your system, you can also just do `karma start` - CLI-based (w/o npm & karma, you will need to ensure phantomjs is available yourself) **currently broken** `phantomjs test/phantomRunner.js test/index.html` From d4f690f14d99856965b67f8b3ef6c91b1b1d55db Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 00:41:33 +0200 Subject: [PATCH 17/30] + make `npm test` safe against accidental abuse (normally reserved for Travis CI) --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a4f99f5..b0f104a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "author": "Philzen ", "scripts": { "karma": "./node_modules/.bin/karma start", - "test": "npm run-script karma && cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" + "test": "echo $TRAVIS$CI | grep truetrue>/dev/null && npm run-script travis-publish || npm run-script karma", + "travis-publish": "npm run-script karma && cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "devDependencies": { "coveralls": "^2.10.0", From d57544d28e9ac1a83c7e8b2970d0f4d82e39791d Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 06:55:05 +0200 Subject: [PATCH 18/30] # fix spec to work with or without require() and pass in browser as well --- test/unit/mJs.CustomNamespaceSpec.js | 40 +++++++++++----------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/test/unit/mJs.CustomNamespaceSpec.js b/test/unit/mJs.CustomNamespaceSpec.js index 3613fba..b542d80 100644 --- a/test/unit/mJs.CustomNamespaceSpec.js +++ b/test/unit/mJs.CustomNamespaceSpec.js @@ -1,34 +1,26 @@ "use strict"; -define([ 'measurement' ], function( measurement ) { +describe("Assign a custom namespace for MeasurementJs:", function() { - // inject namespace +// inject namespace var scriptNs = document.createElement('script'); scriptNs.innerHTML = "var OurCoolCustomNamespace = { changeIT: {}};window.mJsNamespace = OurCoolCustomNamespace.changeIT;"; document.getElementsByTagName('head')[0].appendChild(scriptNs); - - // load measurement.js once more - var script = document.createElement('script'); - script.onload = function() { - console.log("Reloaded measurement.js with custom namespace option"); - }; - script.src = "base/measurement.js"; - script.async = false; - document.getElementsByTagName('head')[0].appendChild(script); - - waitsFor(function() { - return typeof window.OurCoolCustomNamespace.changeIT.measurement !== 'undefined'; - }, "measurementJs to be loaded into custom namespace"); - - describe("Assign a custom namespace for MeasurementJs:", function() { - - - it("just set window.mJsNamespace = YourNamespace before measurement.js include", function() { + console.info('Injected namespace command into page'); + + it("just set window.mJsNamespace = YourNamespace before measurement.js include", function() { + var script = document.createElement('script'); + var basePath = '..'; + if (window.__karma__ !== undefined) { + basePath = 'base'; + } + script.src = basePath + "/measurement.js"; + script.async = false; + script.onload = function() { + console.info("CustomNamespaceSpec.js: Dynamically loaded measurement.js with custom namespace option"); expect(OurCoolCustomNamespace.changeIT.measurement) .toBeDefined(); - }); - + }; + document.getElementsByTagName('head')[0].appendChild(script); }); }); - - From be47a3dc6b1271ab0f4ceaf18a4d707e6c459ea0 Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 06:56:19 +0200 Subject: [PATCH 19/30] # remove obsolete entries from grunt-jshint --- karma.conf.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 81596a0..7cb3c0e 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -78,8 +78,8 @@ module.exports = function(config) { eqnull: true, browser: true, globals: { - cordova: true, - jQuery: true + cordova: true, + jQuery: true } } }, @@ -87,19 +87,10 @@ module.exports = function(config) { coverageReporter: { dir : './coverage/', reporters:[ - {type: 'lcov'}, - {type: 'text', file: 'coverage.txt'}, - {type: 'text-summary', file: 'coverage-summary.txt'} + {type: 'lcov'}, + {type: 'text', file: 'coverage.txt'}, + {type: 'text-summary', file: 'coverage-summary.txt'} ] - }, - - coveralls: { - options: { - debug: false, - coverage_dir: './coverage/*', - dryRun: false, - force: true - } } }); }; From 9559b53f4c7815654b707e3f0691600bbf6fd8f7 Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 06:57:45 +0200 Subject: [PATCH 20/30] # specs passing again with simple browser runner --- test/browserRunner.js | 61 +++++++++++++++++++++++++++++++++++++++ test/index.html | 67 ++++++++----------------------------------- 2 files changed, 73 insertions(+), 55 deletions(-) create mode 100644 test/browserRunner.js diff --git a/test/browserRunner.js b/test/browserRunner.js new file mode 100644 index 0000000..ae71b31 --- /dev/null +++ b/test/browserRunner.js @@ -0,0 +1,61 @@ +require.config({ + baseUrl: 'unit', + urlArgs: 'cb=' + Math.random(), + paths: { + jquery: 'lib/jquery', + measurement: '../../measurement', + jasmine: '../lib/jasmine/jasmine', + 'jasmine-html': '../lib/jasmine/jasmine-html' + }, + shim: { + jasmine: { + exports: 'jasmine' + }, + 'jasmine-html': { + deps: [ 'jasmine' ], + exports: 'jasmine' + } + } +}); + +require([ 'jasmine-html' ], function() { + (function() { + var jasmineEnv = jasmine.getEnv(); + jasmineEnv.updateInterval = 250; + var htmlReporter = new jasmine.HtmlReporter(); + jasmineEnv.addReporter(htmlReporter); + + jasmineEnv.specFilter = function( spec ) { + return htmlReporter.specFilter(spec); + }; + var specs = [ + 'mJs.ApiSpec', + 'mJs.CustomNamespaceSpec', + 'mJs.convert.DistanceSpec', 'mJs.convert.DurationSpec', + 'mJs.convert.PressureSpec', 'mJs.convert.SpeedSpec', + 'mJs.convert.TemperatureSpec' + ]; + + function execJasmine( specs ) { + require(specs, function() { + jasmineEnv.execute(); + }); + } + + var currentWindowOnload = window.onload; + function onLoad() { + if (currentWindowOnload) { + currentWindowOnload(); + } + document.querySelector('.version').innerHTML = jasmineEnv.versionString(); + console.log('Executing Tests...'); + execJasmine(specs); + } + + if (document.readyState === "complete") + return onLoad(); + + return window.onload = onLoad; + + })(); +}); diff --git a/test/index.html b/test/index.html index a7f2da0..0c28d26 100644 --- a/test/index.html +++ b/test/index.html @@ -1,58 +1,15 @@ - - - - - - - - - - - - - - - - - - - - - -
-
- + + + + + + + +
+
+ + From 16dc119d38dd204a6fb1d72dbde8f40216902b9a Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 07:08:56 +0200 Subject: [PATCH 21/30] + add secure key for automated travis release --- .travis.yml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a83e22..2728d75 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,12 @@ language: node_js node_js: - - "0.11" - - "0.10" - - "0.8" +- '0.11' +- '0.10' +- '0.8' +deploy: + provider: npm + api_key: + secure: iHcy2J1AKnPb/LzaGCIr6ziELKqXL4jPTmGpgQfzng79bsfDIi7QhSXL6IANzlqswU/VKB2eUrFBlvEC/MJ5YmbdovzX+uELexhp5GCeOBv3lSJQWVgFU3NKFM6vje4iBcBjqUg+IG4gZs0NI17YWd9zfDAY9AKvl2UoCEo9R18= + on: + tags: true + node: 0.10 From 0531e85b1c977f69d22026fda4056816b9c39a7f Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 07:09:54 +0200 Subject: [PATCH 22/30] # remove superfluous "use strict" --- measurement.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/measurement.js b/measurement.js index 4dfeea7..ef47495 100644 --- a/measurement.js +++ b/measurement.js @@ -321,9 +321,8 @@ // AMD definition - i.e. for require.js if (typeof win.define === "function" && win.define.amd) { - define("measurement", [], function() { - "use strict"; - return MeasurementJs; + win.define("measurement", [], function() { + return MeasurementJs; }); } else if (win.module !== undefined && win.module.exports) { win.module.exports.measurementjs = MeasurementJs; From 3fd10dc24cbcf1f53d29151f9acdf9a7b016768a Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 07:10:24 +0200 Subject: [PATCH 23/30] # updated to start browser test runner --- nbproject/project.properties | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/nbproject/project.properties b/nbproject/project.properties index 916dd65..64edc66 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,14 +1,20 @@ +auxiliary.org-netbeans-modules-css-prep.less_2e_compiler_2e_options= +auxiliary.org-netbeans-modules-css-prep.less_2e_enabled=false +auxiliary.org-netbeans-modules-css-prep.less_2e_mappings=/less:/css +auxiliary.org-netbeans-modules-css-prep.sass_2e_compiler_2e_options= +auxiliary.org-netbeans-modules-css-prep.sass_2e_enabled=false +auxiliary.org-netbeans-modules-css-prep.sass_2e_mappings=/scss:/css +auxiliary.org-netbeans-modules-web-clientproject-api.js_2e_libs_2e_folder=node_modules/karma/node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/test/unit/compress/expected browser.autorefresh.SL__Browsers_ChromeBrowser=true browser.highlightselection.SL__Browsers_ChromeBrowser=true -config.folder=${file.reference.measurement.js-test} -external.project.url= +config.folder= +file.reference.4theworld-measurementjs=. file.reference.lib-measurement.js=. -file.reference.measurement.js-test=test -file.reference.test-config=test/config +file.reference.measurement.js-test=test/unit +file.reference.test-config=${file.reference.lib-measurement.js} file.reference.test-unit=test/unit files.encoding=UTF-8 -server=INTERNAL site.root.folder=${file.reference.lib-measurement.js} -start.file=index.html +start.file=test/index.html test.folder=${file.reference.test-unit} -web.context.root=/measurement +web.context.root=/measurement/ From 9b327d8788b2e73ddeac605a7a4dd6c25dd633f0 Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 07:15:27 +0200 Subject: [PATCH 24/30] # Browser-Driver no longer broken :) --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b0bc83f..7ab4113 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,15 @@ measurement('Speed').convert(10) The API definition and all conversion operations are covered by jasmine tests. The test suite can be executed straightaway and easily, for example: +- Test directly in the browser +Simply open test/index.html in the browser - - In the Browser: **currently broken** -Simply open test/index.html in the browser environment you want to test - - - CLI-based (using npm) + - With Karma test driver (CLI-based using npm) `npm run-script karma` That command just a shorthand for `node_modules/.bin/karma start`. If you have -`karma-cli` already available on your system, you can also just do `karma start` +`karma-cli` already installed globally, you can also just do `karma start` + 1. now open any browser you like and open http://localhost:9876 + 2. observe the output on the test console - CLI-based (w/o npm & karma, you will need to ensure phantomjs is available yourself) **currently broken** `phantomjs test/phantomRunner.js test/index.html` From 3daa6055a94676074b8bb4cfb6f9151eb554abea Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 09:07:44 +0200 Subject: [PATCH 25/30] # remove unused comment --- karma.conf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/karma.conf.js b/karma.conf.js index 7cb3c0e..a64cd98 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -62,9 +62,9 @@ module.exports = function(config) { // if true, Karma captures browsers, runs the tests and exits singleRun: true, + // options for karma-jshint jshint: { options: { -// reporter: require('jshint-stylish'), curly: true, eqeqeq: true, immed: true, From 2c31bc6926d3a55b203fe2580abdce9c31b7bb73 Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 09:52:38 +0200 Subject: [PATCH 26/30] # fix jshint compaints --- measurement.js | 100 ++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/measurement.js b/measurement.js index ef47495..d23cef5 100644 --- a/measurement.js +++ b/measurement.js @@ -8,14 +8,13 @@ * @version 0.1 * @example text measurementJs.convert(3.5).from(DISTANCE.KMH).to(DISTANCE.M); or measurementJs.convert(3.5).from(DISTANCE.KMH).to(DISTANCE.M); * - * @param {Object} namespace + * @param {Object} ns * @returns {undefined} */ -(function(win, namespace) { +(function(win, ns) { "use strict"; - win.measurement = MeasurementJs; - MeasurementJs.Unit = { + var UNIT = { Speed: { MILES_PER_HOUR: 'mph', KILOMETRE_PER_HOUR: 'km/h', @@ -47,33 +46,31 @@ } }; - var speedUnit = MeasurementJs.Unit.Speed, - pressureUnit = MeasurementJs.Unit.Pressure, - DEFINITIONS = { + var DEFINITIONS = { Speed: { 'mph': { - key: speedUnit.MILES_PER_HOUR, - base: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.MILES_PER_HOUR, + base: UNIT.Speed.KILOMETRE_PER_HOUR, factor: 1.609344 }, 'km/h': { - key: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.KILOMETRE_PER_HOUR, base: null }, 'm/s': { - key: speedUnit.METRE_PER_SECOND, - base: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.METRE_PER_SECOND, + base: UNIT.Speed.KILOMETRE_PER_HOUR, factor: 3.6 }, 'kn': { - key: speedUnit.KNOT, - base: speedUnit.KILOMETRE_PER_HOUR, + key: UNIT.Speed.KNOT, + base: UNIT.Speed.KILOMETRE_PER_HOUR, factor: 1.852 } }, Distance: { 'km': { - key: MeasurementJs.Unit.Distance.KILOMETRE, + key: UNIT.Distance.KILOMETRE, base: 'm', factor: 1000, name: { @@ -87,7 +84,7 @@ } }, 'm': { - key: MeasurementJs.Unit.Distance.METRE, + key: UNIT.Distance.METRE, base: null, // equals factor of 1 name: { de: 'Meter', @@ -100,19 +97,19 @@ } }, 'mm': { - key: MeasurementJs.Unit.Distance.MILLIMETRE, + key: UNIT.Distance.MILLIMETRE, base: 'm', factor: 0.001 }, 'in': { - key: MeasurementJs.Unit.Distance.INCH, + key: UNIT.Distance.INCH, base: 'm', factor: 0.0254 } }, Pressure: { 'hPa': { - key: pressureUnit.HECTOPASCAL, + key: UNIT.Pressure.HECTOPASCAL, base: 'Pa', factor: 100, name: { @@ -125,7 +122,7 @@ } }, 'Pa': { - key: pressureUnit.PASCAL, + key: UNIT.Pressure.PASCAL, base: null, name: { de: 'Pascal', @@ -137,7 +134,7 @@ } }, 'bar': { - key: pressureUnit.BAR, + key: UNIT.Pressure.BAR, base: 'Pa', factor: 1000000, name: { @@ -152,22 +149,23 @@ }, Temperature: { 'c': { - key: MeasurementJs.Unit.Temperature.CELSIUS, + key: UNIT.Temperature.CELSIUS, base: null }, 'f': { - key: MeasurementJs.Unit.Temperature.FAHRENHEIT, - base: MeasurementJs.Unit.Temperature.CELSIUS, + key: UNIT.Temperature.FAHRENHEIT, + base: UNIT.Temperature.CELSIUS, factor: function(value, reverse) { - if (reverse) + if (reverse) { return value * 1.8 + 32; + } return (value - 32) * 5 / 9; } }, 'k': { - key: MeasurementJs.Unit.Temperature.KELVIN, - base: MeasurementJs.Unit.Temperature.CELSIUS, + key: UNIT.Temperature.KELVIN, + base: UNIT.Temperature.CELSIUS, factor: function(value, reverse) { /** * Really strange rounding error: @@ -185,17 +183,17 @@ }, Duration: { 'h': { - key: MeasurementJs.Unit.Duration.HOUR, + key: UNIT.Duration.HOUR, base: 's', factor: 3600 }, 'm': { - key: MeasurementJs.Unit.Duration.MINUTE, + key: UNIT.Duration.MINUTE, base: 's', factor: 60 }, 's': { - key: MeasurementJs.Unit.Duration.SECOND, + key: UNIT.Duration.SECOND, base: null, factor: 1 } @@ -216,13 +214,15 @@ if (inputDef && outputDef) { if (inputDef.base === outputUnit) { - if (typeof inputDef.factor === 'function') + if (typeof inputDef.factor === 'function') { return inputDef.factor(value); + } return value * inputDef.factor; } else if (inputDef.key === outputDef.base) { - if (typeof outputDef.factor === 'function') + if (typeof outputDef.factor === 'function') { return outputDef.factor(value, true); + } return value / outputDef.factor; @@ -234,19 +234,21 @@ * vs. larger configuration array/file size */ var baseType = inputDef.base || outputDef.base, baseValue; - if (typeof baseType === 'undefined') + if (typeof baseType === 'undefined') { return false; + } if (baseType === inputDef.base) { - baseValue = MeasurementJs(unitType).convert(value).from(inputDef.key).to(inputDef.base); + baseValue = mJs(unitType).convert(value).from(inputDef.key).to(inputDef.base); inputUnit = inputDef.base; } else if (baseType === outputDef.base) { - baseValue = MeasurementJs(unitType).convert(value).from(outputDef.key).to(outputDef.base); + baseValue = mJs(unitType).convert(value).from(outputDef.key).to(outputDef.base); inputUnit = outputDef.base; } - if (baseType === MeasurementJs.Unit.Temperature.CELSIUS) + if (baseType === UNIT.Temperature.CELSIUS) { return parseFloat(self.convert(baseValue).toFixed(10)); + } return self.convert(baseValue); } @@ -273,7 +275,7 @@ }; } - function MeasurementJs(UnitType) { + var mJs = function MeasurementJs(UnitType) { var self = this; /** * @@ -291,15 +293,17 @@ var easyApiConverter = { from: function(inputUnit) { converter.setInputUnit(inputUnit); - if (readyToConvert()) + if (readyToConvert()) { return converter.convert(valueToConvert); + } return this; }, to: function(outputUnit) { converter.setOutputUnit(outputUnit); - if (readyToConvert()) + if (readyToConvert()) { return converter.convert(valueToConvert); + } return this; } @@ -307,22 +311,26 @@ return easyApiConverter; }; + return { convert: convert }; } + + mJs.Unit = UNIT; - if (typeof namespace !== 'undefined') { - win.MeasurementJs = undefined; - namespace.measurement = MeasurementJs; - namespace.mJs = namespace.MeasurementJs; - namespace.measurement.Converter = MeasurementConverter; - } + if (ns === undefined) { + win.measurement = mJs; + } else { + ns.measurement = mJs; + ns.mJs = mJs; + ns.measurement.Converter = MeasurementConverter; + } // AMD definition - i.e. for require.js if (typeof win.define === "function" && win.define.amd) { win.define("measurement", [], function() { - return MeasurementJs; + return mJs; }); } else if (win.module !== undefined && win.module.exports) { win.module.exports.measurementjs = MeasurementJs; From dc3f8b156558d718ea386dd9849e846738973860 Mon Sep 17 00:00:00 2001 From: Philzen Date: Thu, 3 Apr 2014 15:12:09 +0200 Subject: [PATCH 27/30] # use summary-option of karma-jshint --- karma.conf.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index a64cd98..a8155fd 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -64,24 +64,25 @@ module.exports = function(config) { // options for karma-jshint jshint: { - options: { - curly: true, - eqeqeq: true, - immed: true, - latedef: true, - newcap: true, - noarg: true, - sub: true, - undef: true, - boss: true, - devel: true, - eqnull: true, - browser: true, - globals: { - cordova: true, - jQuery: true - } + options: { + curly: true, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + boss: true, + devel: true, + eqnull: true, + browser: true, + globals: { + cordova: true, + jQuery: true } + }, + summary: true }, coverageReporter: { From 6db9defe7f5d4b1ab1d0227f48979dede9e8fd6a Mon Sep 17 00:00:00 2001 From: Philzen Date: Fri, 4 Apr 2014 02:21:19 +0200 Subject: [PATCH 28/30] add karma-jshint from npm --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index b0f104a..bb37622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "measurementjs", - "version": "0.1.1", + "version": "0.1.1-alpha", "description": "Nice unit-of-measure conversion", "readmeFilename": "README.md", "main": "measurement.js", @@ -22,8 +22,8 @@ "karma-coverage": "^0.2.1", "karma-jasmine": "^0.1.5", "karma-requirejs": "^0.2.1", - "karma-jshint": "philzen/karma-jshint.git", + "karma-jshint": ">=0.1", "karma-phantomjs-launcher": "^0.1.3", - "phantomjs": "^1.9.7-3" + "phantomjs": "<2" } } From f65dd5ae8ec84fb97b991a7f31f8cbdfb22d3d52 Mon Sep 17 00:00:00 2001 From: Philzen Date: Fri, 4 Apr 2014 02:31:01 +0200 Subject: [PATCH 29/30] # cleanup remaining jslint complaints --- measurement.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/measurement.js b/measurement.js index d23cef5..6869208 100644 --- a/measurement.js +++ b/measurement.js @@ -174,10 +174,10 @@ * Following workarounds: */ if (reverse) { - return parseFloat((value + 273 + .15).toFixed(10)); + return parseFloat((value + 273 + 0.15).toFixed(10)); } - return (value - 273) - .15; + return (value - 273) - 0.15; } } }, @@ -239,10 +239,10 @@ } if (baseType === inputDef.base) { - baseValue = mJs(unitType).convert(value).from(inputDef.key).to(inputDef.base); + baseValue = mjs(unitType).convert(value).from(inputDef.key).to(inputDef.base); inputUnit = inputDef.base; } else if (baseType === outputDef.base) { - baseValue = mJs(unitType).convert(value).from(outputDef.key).to(outputDef.base); + baseValue = mjs(unitType).convert(value).from(outputDef.key).to(outputDef.base); inputUnit = outputDef.base; } @@ -275,7 +275,7 @@ }; } - var mJs = function MeasurementJs(UnitType) { + function MeasurementJs(UnitType) { var self = this; /** * @@ -316,24 +316,25 @@ convert: convert }; } - - mJs.Unit = UNIT; + + var mjs = MeasurementJs; + mjs.Unit = UNIT; if (ns === undefined) { - win.measurement = mJs; + win.measurement = mjs; } else { - ns.measurement = mJs; - ns.mJs = mJs; + ns.measurement = mjs; + ns.mJs = mjs; ns.measurement.Converter = MeasurementConverter; } // AMD definition - i.e. for require.js if (typeof win.define === "function" && win.define.amd) { win.define("measurement", [], function() { - return mJs; + return mjs; }); } else if (win.module !== undefined && win.module.exports) { - win.module.exports.measurementjs = MeasurementJs; + win.module.exports.measurementjs = mjs; } })(window, window.mJsNamespace); From 126e4b923214190e1f0a00ad2da920795cdfd558 Mon Sep 17 00:00:00 2001 From: Philzen Date: Fri, 4 Apr 2014 02:43:44 +0200 Subject: [PATCH 30/30] reordered tests --- test/browserRunner.js | 4 +-- test/unit/mJs.CustomNamespaceSpec.js | 44 ++++++++++++++++------------ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/test/browserRunner.js b/test/browserRunner.js index ae71b31..8f2c4ed 100644 --- a/test/browserRunner.js +++ b/test/browserRunner.js @@ -30,10 +30,10 @@ require([ 'jasmine-html' ], function() { }; var specs = [ 'mJs.ApiSpec', - 'mJs.CustomNamespaceSpec', 'mJs.convert.DistanceSpec', 'mJs.convert.DurationSpec', 'mJs.convert.PressureSpec', 'mJs.convert.SpeedSpec', - 'mJs.convert.TemperatureSpec' + 'mJs.convert.TemperatureSpec', + 'mJs.CustomNamespaceSpec' ]; function execJasmine( specs ) { diff --git a/test/unit/mJs.CustomNamespaceSpec.js b/test/unit/mJs.CustomNamespaceSpec.js index b542d80..cd31e9a 100644 --- a/test/unit/mJs.CustomNamespaceSpec.js +++ b/test/unit/mJs.CustomNamespaceSpec.js @@ -1,26 +1,32 @@ "use strict"; -describe("Assign a custom namespace for MeasurementJs:", function() { +define([ 'measurement' ], function( measurement ) { + describe("Assign a custom namespace for MeasurementJs:", function() { // inject namespace - var scriptNs = document.createElement('script'); - scriptNs.innerHTML = "var OurCoolCustomNamespace = { changeIT: {}};window.mJsNamespace = OurCoolCustomNamespace.changeIT;"; - document.getElementsByTagName('head')[0].appendChild(scriptNs); - console.info('Injected namespace command into page'); + var scriptNs = document.createElement('script'); + scriptNs.innerHTML = "var OurCoolCustomNamespace = { changeIT: {}};window.mJsNamespace = OurCoolCustomNamespace.changeIT;"; + document.getElementsByTagName('head')[0].appendChild(scriptNs); + console.info('Injected namespace command into page'); - it("just set window.mJsNamespace = YourNamespace before measurement.js include", function() { - var script = document.createElement('script'); - var basePath = '..'; - if (window.__karma__ !== undefined) { - basePath = 'base'; - } - script.src = basePath + "/measurement.js"; - script.async = false; - script.onload = function() { - console.info("CustomNamespaceSpec.js: Dynamically loaded measurement.js with custom namespace option"); - expect(OurCoolCustomNamespace.changeIT.measurement) - .toBeDefined(); - }; - document.getElementsByTagName('head')[0].appendChild(script); + it("just set window.mJsNamespace = YourNamespace before measurement.js include", function() { + var script = document.createElement('script'); + var basePath = '..'; + if (window.__karma__ !== undefined) { + basePath = 'base'; + } + script.src = basePath + "/measurement.js"; + script.async = false; + script.onload = function() { + console.info("CustomNamespaceSpec.js: Dynamically loaded measurement.js with custom namespace option"); + expect(OurCoolCustomNamespace.changeIT.measurement) + .toBeDefined(); + }; + document.getElementsByTagName('head')[0].appendChild(script); + + waitsFor(function() { + return window.OurCoolCustomNamespace.changeIT.measurement !== undefined; + }); + }); }); });