Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'js-quantities' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
it("should convert value", () => {
// TODO Same test but with m/h -> ft/s triggers rounding issue
// (For the sake of speed, converter does not check and fix rounding issues)
const converter = Qty.swiftConverter("m/h", "m/s");
expect(converter(2500)).toEqual(Qty("2500 m/h").to("m/s").scalar);
});
it("should convert temperatures to temperature degrees", () => {
// according to ruby-units, temp -> deg conversion always uses the 0 relative degrees
let qty = Qty("100 tempC");
expect(qty.to("degC").scalar).toBe(100);
qty = Qty("0 tempK");
expect(qty.to("degC").scalar).toBe(0);
qty = Qty("0 tempF");
expect(qty.to("degK").scalar).toBe(0);
qty = Qty("18 tempF");
expect(qty.to("degC").scalar).toBe(10);
qty = Qty("10 tempC");
expect(qty.to("degF").scalar).toBe(18);
});
it("should divide while trying to avoid numerical errors", () => {
expect(Qty.divSafe(0.000773, 0.000001)).toBe(773);
// TODO uncomment and fix
// expect(Qty.divSafe(24.5, 0.2777777777777778)).toBe(88.2);
});
});
it("should divide while trying to avoid numerical errors", () => {
expect(Qty.divSafe(0.000773, 0.000001)).toBe(773);
// TODO uncomment and fix
// expect(Qty.divSafe(24.5, 0.2777777777777778)).toBe(88.2);
});
});
Qty('100 tempC').div(qty); // throws error
Qty('100 tempC*unit'); // throws error
Qty('100 tempC/unit'); // throws error
Qty('100 unit/tempC'); // throws error
Qty('100 tempC').inverse(); // throws error
Qty('100 tempC').to('degC'); // => 100 degC
Qty('100 degC').to('tempC'); // => -173.15 tempC
Qty('0 tempC').add('100 degC'); // => 100 tempC
try {
// code triggering an error inside JS-quantities
} catch (e) {
if (e instanceof Qty.Error) {
// ...
} else {
// ...
}
}
// From project spec
describe("js-quantities", () => {
"use strict";
describe("initialization", () => {
it("should create unit only", () => {
const qty = Qty("m");
expect(qty.numerator).toEqual(["<meter>"]);
expect(qty.scalar).toBe(1);</meter>
Qty('100 tempC').div(qty); // throws error
Qty('100 tempC*unit'); // throws error
Qty('100 tempC/unit'); // throws error
Qty('100 unit/tempC'); // throws error
Qty('100 tempC').inverse(); // throws error
Qty('100 tempC').to('degC'); // => 100 degC
Qty('100 degC').to('tempC'); // => -173.15 tempC
Qty('0 tempC').add('100 degC'); // => 100 tempC
try {
// code triggering an error inside JS-quantities
} catch (e) {
if (e instanceof Qty.Error) {
// ...
} else {
// ...
}
}
// From project spec
describe("js-quantities", () => {
"use strict";
describe("initialization", () => {
it("should create unit only", () => {
const qty = Qty("m");
expect(qty.numerator).toEqual(["<meter>"]);
expect(qty.scalar).toBe(1);</meter>
qty = Qty('1 m^2/s^2');
qty = Qty('1 m^2 s^-2'); // negative powers
qty = Qty('1 m2 s-2'); // ^ is optional
qty = Qty('1 m^2 kg^2 J^2/s^2 A');
qty = Qty('1.5'); // unitless quantity
qty = Qty(1.5); // number as initializing value
qty = Qty('1 attoparsec/microfortnight');
let qtyCopy: Qty;
qtyCopy = Qty(qty); // quantity could be copied when used as initializing value
Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null
Qty.getKinds(); // => Array of names of every well-known kind of units
Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']
Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]
const qty1 = Qty('1m');
const qty2 = Qty('2m');
qty1.isCompatible(qty2); // => true or false
qty.kind(); // => 'length', 'area', etc...
qty = Qty('1 m^2/s^2');
qty = Qty('1 m^2 s^-2'); // negative powers
qty = Qty('1 m2 s-2'); // ^ is optional
qty = Qty('1 m^2 kg^2 J^2/s^2 A');
qty = Qty('1.5'); // unitless quantity
qty = Qty(1.5); // number as initializing value
qty = Qty('1 attoparsec/microfortnight');
let qtyCopy: Qty;
qtyCopy = Qty(qty); // quantity could be copied when used as initializing value
Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null
Qty.getKinds(); // => Array of names of every well-known kind of units
Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']
Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]
const qty1 = Qty('1m');
const qty2 = Qty('2m');
qty1.isCompatible(qty2); // => true or false
qty.kind(); // => 'length', 'area', etc...
qty = Qty(1.5); // number as initializing value
qty = Qty('1 attoparsec/microfortnight');
let qtyCopy: Qty;
qtyCopy = Qty(qty); // quantity could be copied when used as initializing value
Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null
Qty.getKinds(); // => Array of names of every well-known kind of units
Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']
Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]
const qty1 = Qty('1m');
const qty2 = Qty('2m');
qty1.isCompatible(qty2); // => true or false
qty.kind(); // => 'length', 'area', etc...
qty.isUnitless(); // => true or false
qty.isBase(); // => true if quantity is represented with base units
qty.toBase(); // converts to SI units (10 cm => 0.1 m) (new instance)
qty.toFloat(); // returns scalar of unitless quantity (otherwise throws error)
qty = Qty('1.5'); // unitless quantity
qty = Qty(1.5); // number as initializing value
qty = Qty('1 attoparsec/microfortnight');
let qtyCopy: Qty;
qtyCopy = Qty(qty); // quantity could be copied when used as initializing value
Qty.parse('1 m'); // => 1 meter
Qty.parse('foo'); // => null
Qty.getKinds(); // => Array of names of every well-known kind of units
Qty.getUnits('currency'); // => [ 'dollar', 'cents' ]
// Or all alphabetically sorted
Qty.getUnits(); // => [ 'acre','Ah','ampere','AMU','angstrom']
Qty.getAliases('m'); // => [ 'm', 'meter', 'meters', 'metre', 'metres' ]
const qty1 = Qty('1m');
const qty2 = Qty('2m');
qty1.isCompatible(qty2); // => true or false
qty.kind(); // => 'length', 'area', etc...
qty.isUnitless(); // => true or false
qty.isBase(); // => true if quantity is represented with base units
qty.toBase(); // converts to SI units (10 cm => 0.1 m) (new instance)