Skip to content

Commit

Permalink
format: stop relying on array properties
Browse files Browse the repository at this point in the history
There are numerous libraries out there (sugarjs, prototypejs) that
extend the `Array` prototype. The `simplify` function in js-quantities
relies on setting/checking properties on an array instance, which means
that it's incompatible with such libraries. Add compatibility by using a
separate object for keeping track of the counters instead of using
properties on array instances.

Fix gentooboontoo#130.

Signed-off-by: Kyle Fazzari <[email protected]>
  • Loading branch information
kyrofa committed Jan 21, 2022
1 parent 2c5442d commit 6b7bfcc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
13 changes: 9 additions & 4 deletions build/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ SOFTWARE.
"<oersted>" : [["Oe","oersted","oersteds"], 250.0 / Math.PI, "magnetism", ["<ampere>"], ["<meter>"]],

/* energy */
"<joule>" : [["J","joule","Joule","joules"], 1.0, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<joule>" : [["J","joule","Joule","joules","Joules"], 1.0, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<erg>" : [["erg","ergs"], 1e-7, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<btu>" : [["BTU","btu","BTUs"], 1055.056, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<calorie>" : [["cal","calorie","calories"], 4.18400, "energy",["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
Expand Down Expand Up @@ -460,7 +460,9 @@ SOFTWARE.
"<dozen>" : [["doz","dz","dozen"],12.0,"prefix_only", ["<each>"]],
"<percent>": [["%","percent"], 0.01, "prefix_only", ["<1>"]],
"<ppm>" : [["ppm"],1e-6, "prefix_only", ["<1>"]],
"<ppt>" : [["ppt"],1e-9, "prefix_only", ["<1>"]],
"<ppb>" : [["ppb"],1e-9, "prefix_only", ["<1>"]],
"<ppt>" : [["ppt"],1e-12, "prefix_only", ["<1>"]],
"<ppq>" : [["ppq"],1e-15, "prefix_only", ["<1>"]],
"<gross>" : [["gr","gross"],144.0, "prefix_only", ["<dozen>","<dozen>"]],
"<decibel>" : [["dB","decibel","decibels"], 1.0, "logarithmic", ["<decibel>"]]
};
Expand Down Expand Up @@ -1991,10 +1993,13 @@ SOFTWARE.
function simplify(units) {
// this turns ['s','m','s'] into ['s2','m']

var counters = {};
var unitCounts = units.reduce(function(acc, unit) {
var unitCounter = acc[unit];
var unitCounter = counters[unit];
if (!unitCounter) {
acc.push(unitCounter = acc[unit] = [unit, 0]);
unitCounter = [unit, 0];
counters[unit] = unitCounter;
acc.push(unitCounter);
}

unitCounter[1]++;
Expand Down
13 changes: 9 additions & 4 deletions build/quantities.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ var UNITS = {
"<oersted>" : [["Oe","oersted","oersteds"], 250.0 / Math.PI, "magnetism", ["<ampere>"], ["<meter>"]],

/* energy */
"<joule>" : [["J","joule","Joule","joules"], 1.0, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<joule>" : [["J","joule","Joule","joules","Joules"], 1.0, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<erg>" : [["erg","ergs"], 1e-7, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<btu>" : [["BTU","btu","BTUs"], 1055.056, "energy", ["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
"<calorie>" : [["cal","calorie","calories"], 4.18400, "energy",["<meter>","<meter>","<kilogram>"], ["<second>","<second>"]],
Expand Down Expand Up @@ -454,7 +454,9 @@ var UNITS = {
"<dozen>" : [["doz","dz","dozen"],12.0,"prefix_only", ["<each>"]],
"<percent>": [["%","percent"], 0.01, "prefix_only", ["<1>"]],
"<ppm>" : [["ppm"],1e-6, "prefix_only", ["<1>"]],
"<ppt>" : [["ppt"],1e-9, "prefix_only", ["<1>"]],
"<ppb>" : [["ppb"],1e-9, "prefix_only", ["<1>"]],
"<ppt>" : [["ppt"],1e-12, "prefix_only", ["<1>"]],
"<ppq>" : [["ppq"],1e-15, "prefix_only", ["<1>"]],
"<gross>" : [["gr","gross"],144.0, "prefix_only", ["<dozen>","<dozen>"]],
"<decibel>" : [["dB","decibel","decibels"], 1.0, "logarithmic", ["<decibel>"]]
};
Expand Down Expand Up @@ -1985,10 +1987,13 @@ function getOutputNames(units) {
function simplify(units) {
// this turns ['s','m','s'] into ['s2','m']

var counters = {};
var unitCounts = units.reduce(function(acc, unit) {
var unitCounter = acc[unit];
var unitCounter = counters[unit];
if (!unitCounter) {
acc.push(unitCounter = acc[unit] = [unit, 0]);
unitCounter = [unit, 0];
counters[unit] = unitCounter;
acc.push(unitCounter);
}

unitCounter[1]++;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions spec/quantitiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,22 @@ describe("js-quantities", function() {
});
});

describe("initialization with extensions to Array prototype", function() {
beforeEach(function() {
Array.prototype["min"] = Math.min;
});

afterEach(function() {
delete Array.prototype["min"];
});

it("initialization should be agnostic to Array prototype extensions", function() {
var qty = Qty("100 gal/min");
expect(qty.scalar).toEqual(100);
expect(qty.units()).toEqual("gal/min");
});
});

describe("isCompatible", function() {
it("should return true with compatible quantities", function() {
var qty1 = Qty("1 m*kg/s");
Expand Down
7 changes: 5 additions & 2 deletions src/quantities/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,13 @@ function getOutputNames(units) {
function simplify(units) {
// this turns ['s','m','s'] into ['s2','m']

var counters = {};
var unitCounts = units.reduce(function(acc, unit) {
var unitCounter = acc[unit];
var unitCounter = counters[unit];
if (!unitCounter) {
acc.push(unitCounter = acc[unit] = [unit, 0]);
unitCounter = [unit, 0];
counters[unit] = unitCounter;
acc.push(unitCounter);
}

unitCounter[1]++;
Expand Down

0 comments on commit 6b7bfcc

Please sign in to comment.