diff --git a/.travis.yml b/.travis.yml index 5f240410f..bf24b85a1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,8 @@ before_install: - git config --global user.name "Dist Zilla Plugin TravisCI" - git config --global user.email $HOSTNAME":not-for-mail@travis-ci.org" install: - - npm install -g uglify-js handlebars mathjs jasmine + - npm install -g uglify-js handlebars jasmine-node + - npm install mathjs - cpanm --quiet --notest Dist::Zilla - dzil authordeps | grep -ve '^\W' | xargs -n 5 -P 10 cpanm --quiet --notest --mirror http://www.cpan.org/ --mirror http://duckpan.org - dzil listdeps | grep -ve '^\W' | cpanm --quiet --notest --mirror http://www.cpan.org/ --mirror http://duckpan.org @@ -24,5 +25,5 @@ language: perl perl: - 5.16 script: - - jasmine + - jasmine-node --test-dir spec/ - prove -lr -j1 t diff --git a/package.json b/package.json new file mode 100644 index 000000000..a49b20e21 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "zeroclickinfo-goodies", + "version": "0.0.0", + "description": "DuckDuckGo Goodies - Perl Backed IAs", + "main": "", + "repository": { + "type": "git", + "url": "git+https://github.com/duckduckgo/zeroclickinfo-goodies.git" + }, + "author": "DuckDuckGo", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/duckduckgo/zeroclickinfo-goodies/issues" + }, + "homepage": "https://github.com/duckduckgo/zeroclickinfo-goodies#readme" +} diff --git a/share/goodie/conversions/conversions.js b/share/goodie/conversions/conversions.js index a39b52c77..2b0edc7e0 100644 --- a/share/goodie/conversions/conversions.js +++ b/share/goodie/conversions/conversions.js @@ -1,7 +1,7 @@ DDH.conversions = DDH.conversions || {}; (function(DDH) { - "use strict"; + "use strict"; // flag variables for onShow functionality var localDOMInitialized = false; @@ -42,24 +42,32 @@ DDH.conversions = DDH.conversions || {}; leftValue: "", rightValue: "", + customUnits: [ + // CUSTOM ENERGY UNITS + {name: 'kilojoule', factor: '1000 joules'}, + {name: 'gramcalorie', factor: '4.184 joules' }, + {name: 'kilocalorie', factor: '4184 joules' }, + + // CUSTOM DIGITAL UNITS + {name: 'kbit', factor: '1000 b'}, + {name: 'mbit', factor: '1000000 b'}, + {name: 'gbit', factor: '1000000000 b'}, + {name: 'tbit', factor: '1000000000000 b'}, + {name: 'KB', factor: '1000 B'}, + {name: 'MB', factor: '1000000 B'}, + {name: 'GB', factor: '1000000000 B'}, + {name: 'TB', factor: '1000 GB'}, + {name: 'PB', factor: '1000 TB'} + ], + // custom units that are not supported by math.js setUpCustomUnits: function() { - - // CUSTOM ENERGY UNITS - math.createUnit('kilojoule', '1000 joules'); - math.createUnit('gramcalorie', '4.184 joules'); - math.createUnit('kilocalorie', '4184 joules'); - - // CUSTOM DIGITAL UNITS - math.createUnit('kbit', '1000 b'); - math.createUnit('mbit', '1000000 b'); - math.createUnit('gbit', '1000000000 b'); - math.createUnit('tbit', '1000000000000 b'); - math.createUnit('KB', '1000 B'); - math.createUnit('MB', '1000000 B'); - math.createUnit('GB', '1000000000 B'); - math.createUnit('TB', '1000 GB'); - math.createUnit('PB', '1000 TB'); + for (var i = 0 ; i < this.customUnits.length ; i++) { + math.createUnit( + this.customUnits[i].name, + this.customUnits[i].factor + ) + }; }, setValues: function() { @@ -320,7 +328,7 @@ DDH.conversions = DDH.conversions || {}; { symbol: 'torr', name: 'Torr' }, { symbol: 'mmHg', name: 'mmHg' }, { symbol: 'mmH2O', name: 'mmH2O' }, - { symbol: 'cmH20', name: 'cmH20' }, + { symbol: 'cmH2O', name: 'cmH2O' }, { symbol: 'bar', name: 'Bars' }, ], defaults: ['Pa', 'psi'] @@ -358,7 +366,7 @@ DDH.conversions = DDH.conversions || {}; } // Units DDH.conversions.build = function(ops) { - + // Defaults to length if no base is supported var startBase = ops.data.physical_quantity || 'length'; var leftUnit = ops.data.left_unit || Units[startBase].defaults[0]; @@ -446,4 +454,4 @@ DDH.conversions = DDH.conversions || {}; module.exports = Converter; } -})(DDH); \ No newline at end of file +})(DDH); diff --git a/share/goodie/conversions/triggers.yml b/share/goodie/conversions/triggers.yml index c79bdccbb..983f8e567 100755 --- a/share/goodie/conversions/triggers.yml +++ b/share/goodie/conversions/triggers.yml @@ -506,7 +506,7 @@ aliases: - cups - cup type: volume -unit: cup +unit: cups --- aliases: - metric tsp diff --git a/spec/conversions_spec.js b/spec/conversions_spec.js index d87955a49..1fd031420 100644 --- a/spec/conversions_spec.js +++ b/spec/conversions_spec.js @@ -1,74 +1,93 @@ GLOBAL.DDH = {}; -var math = require("mathjs"); - +var math = require('mathjs'); var Converter = require("../share/goodie/conversions/conversions.js"); +/** + * conversions_spec.js + * + * This file is for testing custom units defined in Converter.customUnits + */ + +// custom units that are not supported by math.js +function setUpCustomUnits() { + for (var i = 0 ; i < Converter.customUnits.length ; i++) { + math.createUnit( + Converter.customUnits[i].name, + Converter.customUnits[i].factor + ) + }; +} + +// removes the unit +function stripUnit( string ) { + return string.toString().split(" ")[0]; +} + describe("Conversion of Custom Units", function() { - // set up the custom units - Converter.setUpCustomUnits(); + setUpCustomUnits(); /** * CUSTOM DIGITAL UNITS */ it("should convert gb to mb", function() { - var conversion = Converter.eval("2GB to MB"); + var conversion = stripUnit(math.eval("2GB to MB")); expect(conversion).toEqual("2000"); }); it("should convert TB to MB", function() { - var conversion = Converter.eval("8TB to MB"); + var conversion = stripUnit(math.eval("8TB to MB")); expect(conversion).toEqual("8e+6"); }); it("should convert GB to TB", function() { - var conversion = Converter.eval("1000GB to TB"); + var conversion = stripUnit(math.eval("1000GB to TB")); expect(conversion).toEqual("1"); }); it("should convert GB to TB", function() { - var conversion = Converter.eval("100GB to TB"); + var conversion = stripUnit(math.eval("100GB to TB")); expect(conversion).toEqual("0.1"); }); it("should convert TB to GB", function() { - var conversion = Converter.eval("1TB to GB"); + var conversion = stripUnit(math.eval("1TB to GB")); expect(conversion).toEqual("1000"); }); it("should convert MB to TB", function() { - var conversion = Converter.eval("3500MB to TB"); + var conversion = stripUnit(math.eval("3500MB to TB")); expect(conversion).toEqual("0.0035"); }); it("should convert PB to TB", function() { - var conversion = Converter.eval("5PB to TB"); + var conversion = stripUnit(math.eval("5PB to TB")); expect(conversion).toEqual("5000"); }); it("should convert PB to GB", function() { - var conversion = Converter.eval("7PB to GB"); + var conversion = stripUnit(math.eval("7PB to GB")); expect(conversion).toEqual("7e+6"); }); it("should convert PB to GB", function() { - var conversion = Converter.eval("7PB to GB"); + var conversion = stripUnit(math.eval("7PB to GB")); expect(conversion).toEqual("7e+6"); }); it("should convert MB to gbits", function() { - var conversion = Converter.eval("6500MB to gbit"); + var conversion = stripUnit(math.eval("6500MB to gbit")); expect(conversion).toEqual("52"); }); it("should convert mbits to gbits", function() { - var conversion = Converter.eval("100000mbit to gbit"); + var conversion = stripUnit(math.eval("100000mbit to gbit")); expect(conversion).toEqual("100"); }); it("should convert tbits to mbits", function() { - var conversion = Converter.eval(".5tbit to mbit"); + var conversion = stripUnit(math.eval(".5tbit to mbit")); expect(conversion).toEqual("5e+5"); }); @@ -76,23 +95,23 @@ describe("Conversion of Custom Units", function() { * CUSTOM ENERGY UNITS */ it("should convert joules to kilojoules", function() { - var conversion = Converter.eval("8888joules to kilojoule"); + var conversion = stripUnit(math.eval("8888joules to kilojoule")); expect(conversion).toEqual("8.888"); }); it("should convert joules to kilojoules", function() { - var conversion = Converter.eval("34311joules to kilojoule"); + var conversion = stripUnit(math.eval("34311joules to kilojoule")); expect(conversion).toEqual("34.311"); }); it("should convert gram calories to kilojoules", function() { - var conversion = Converter.eval("34311323gramcalorie to kilojoule"); - expect(conversion).toEqual("1.43559e+5"); + var conversion = stripUnit(math.eval("34311323gramcalorie to kilojoule")); + expect(conversion).toEqual("1.43558575432e+5"); }); it("should convert kilocalorie to kilojoules", function() { - var conversion = Converter.eval("6543kilocalorie to kilojoule"); - expect(conversion).toEqual("27375.9"); + var conversion = stripUnit(math.eval("6543kilocalorie to kilojoule")); + expect(conversion).toEqual("27375.912"); }); -}); \ No newline at end of file +});