Skip to content

Commit

Permalink
feat(pressure): add psi units
Browse files Browse the repository at this point in the history
Closes #454
  • Loading branch information
jonahsnider committed Mar 29, 2022
1 parent 2812353 commit 24d4908
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/bundled-conversions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,11 @@ export const conversions = {
atmosphere: [8, 101325],
atmospheres: [8, 101325],
atm: [8, 101325],
'pound per square inch': [8, 6894.757],
'pounds per square inch': [8, 6894.757],
psi: [8, 6894.757],
'lbf/in2': [8, 6894.757],
'lbf/in²': [8, 6894.757],
kelvin: [9, 1],
kelvins: [9, 1],
K: [9, 1],
Expand Down Expand Up @@ -1079,7 +1084,7 @@ export const bestUnits = {
[1000000000000, 'TW'],
[1000000000000000, 'PW'],
],
[[1, 'Pa']],
[[1, 'psi']],
[[1, 'F']],
[
[1, 'ns'],
Expand Down
22 changes: 21 additions & 1 deletion packages/bundled-conversions/test/snapshots/index.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,14 @@ Generated by [AVA](https://avajs.dev).
4,
4.448222,
],
'lbf/in2': [
8,
6894.757,
],
'lbf/in²': [
8,
6894.757,
],
liter: [
11,
0.001,
Expand Down Expand Up @@ -3449,6 +3457,10 @@ Generated by [AVA](https://avajs.dev).
4,
4.448222,
],
'pound per square inch': [
8,
6894.757,
],
'pound-force': [
4,
4.448222,
Expand All @@ -3465,10 +3477,18 @@ Generated by [AVA](https://avajs.dev).
6,
453.59237,
],
'pounds per square inch': [
8,
6894.757,
],
ps: [
10,
1e-12,
],
psi: [
8,
6894.757,
],
pt: [
11,
0.000473176473,
Expand Down Expand Up @@ -4296,7 +4316,7 @@ Generated by [AVA](https://avajs.dev).
[
[
1,
'Pa',
'psi',
],
],
[
Expand Down
Binary file modified packages/bundled-conversions/test/snapshots/index.test.ts.snap
Binary file not shown.
5 changes: 4 additions & 1 deletion packages/conversions/src/conversions/pressure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Id} from '../types/index.js';

export const pressure: ReadonlyDeep<Family> = {
id: Id.Pressure,
best: ['Pa'],
best: {metric: ['Pa'], imperial: ['psi']},
conversions: [
{names: ['pascal', 'pascals'], symbols: ['Pa'], ratio: 1},
...expandMacro(Macros.si, {names: ['pascal', 'pascals'], symbols: ['Pa'], ratio: 1}),
Expand All @@ -18,5 +18,8 @@ export const pressure: ReadonlyDeep<Family> = {
{names: ['millitorr'], symbols: ['mTorr'], ratio: new BigNumber(101_325).div(760).div(1e3)},

{names: ['atmosphere', 'atmospheres'], symbols: ['atm'], ratio: 101_325},

// https://en.wikipedia.org/wiki/Pound_per_square_inch
{names: ['pound per square inch', 'pounds per square inch'], symbols: ['psi', 'lbf/in2', 'lbf/in²'], ratio: 6.894_757e3},
],
};
5 changes: 5 additions & 0 deletions packages/convert/src/conversions/pressure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {assertConversions} from '../../test/assert-conversion';

describe('conversions', () => {
assertConversions([{from: [1, 'psi'], to: [6.894_757 - Number.EPSILON * 3, 'kPa']}]);
});
9 changes: 8 additions & 1 deletion packages/optimized-conversions/src/optimizer/best.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert/strict';
import {combineIterables, Sort} from '@jonahsnider/util';
import type * as Conversions from 'conversions';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -34,7 +35,13 @@ export function optimizeBest(conversionFamilies: ReadonlyArray<Readonly<Conversi

const conversionFamilyBest = Array.isArray(conversionFamily.best) ? conversionFamily.best : conversionFamily.best[kind];

conversionFamilyBest.sort(Sort.ascending(bestUnit => lookup[bestUnit].toNumber()));
conversionFamilyBest.sort(
Sort.ascending(bestUnit => {
assert(lookup[bestUnit], `The unit "${bestUnit}" couldn't be found in conversion family ${conversionFamily.id} while optimizing best conversions`);

return lookup[bestUnit].toNumber();
}),
);

const firstValue = lookup[conversionFamilyBest[0]];

Expand Down

0 comments on commit 24d4908

Please sign in to comment.