Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: stop relying on array properties #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>"]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These (beyond million) only make sense in the places like the US where 1 billion == 1 thousand million, not in places like the UK where 1 billion == 1 million million.

Copy link
Author

@kyrofa kyrofa Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore the generated files. I did not write this code, but they weren't .gitignored so I felt like I should include them. If you think there are issues here, you might consider logging one since these things were already here.

"<ppt>" : [["ppt"],1e-12, "prefix_only", ["<1>"]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another ambiguous term: parts per trillion or parts per thousand? (and if trillion: US trillion or UK trillion)

"<ppq>" : [["ppq"],1e-15, "prefix_only", ["<1>"]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quad? quint?

"<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