Skip to content

Commit

Permalink
feat(units): add coherent(), update unit defs
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 14, 2023
1 parent 22031e6 commit d2f7608
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 23 deletions.
16 changes: 5 additions & 11 deletions packages/units/src/accel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ import { ft, m } from "./length.js";
import { s } from "./time.js";
import { defUnit, div, mul, pow } from "./unit.js";

export const m_s2 = defUnit(
"m/s2",
"meter per second squared",
div(m, pow(s, 2))
);
const s2 = pow(s, 2);

export const ft_s2 = defUnit(
"ft/s2",
"foot per second squared",
div(ft, pow(s, 2))
);
export const m_s2 = defUnit("m/s2", "meter per second squared", div(m, s2));

export const ft_s2 = defUnit("ft/s2", "foot per second squared", div(ft, s2));

export const rad_s2 = defUnit(
"rad/s2",
"radian per second squared",
div(rad, pow(s, 2))
div(rad, s2)
);

export const g0 = defUnit("g0", "standard gravity", mul(m_s2, 9.80665));
4 changes: 2 additions & 2 deletions packages/units/src/electric.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { m2 } from "./area.js";
import { J } from "./energy.js";
import { h, s } from "./time.js";
import { defUnit, div, mul, prefix, unit } from "./unit.js";
import { coherent, defUnit, div, mul, prefix } from "./unit.js";

export const A = defUnit("A", "ampere", unit(3, 1, 0, true));
export const A = defUnit("A", "ampere", coherent(3));
export const mA = defUnit("mA", "milliampere", prefix("m", A));
export const mAh = defUnit("mAh", "milliampere-hour", mul(mA, h));

Expand Down
4 changes: 2 additions & 2 deletions packages/units/src/length.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defUnit, mul, prefix, unit } from "./unit.js";
import { coherent, defUnit, mul, prefix } from "./unit.js";

export const m = defUnit("m", "meter", unit(1, 1, 0, true));
export const m = defUnit("m", "meter", coherent(1));
export const cm = defUnit("cm", "centimeter", prefix("c", m));
export const mm = defUnit("mm", "millimeter", prefix("m", m));
export const µm = defUnit("µm", "micrometer", prefix("µ", m));
Expand Down
4 changes: 2 additions & 2 deletions packages/units/src/luminous.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sr } from "./angle.js";
import { m2 } from "./area.js";
import { defUnit, div, mul, unit } from "./unit.js";
import { coherent, defUnit, div, mul } from "./unit.js";

export const cd = defUnit("cd", "candela", unit(6, 1, 0, true));
export const cd = defUnit("cd", "candela", coherent(6));

export const lm = defUnit("lm", "lumen", mul(cd, sr));
export const lx = defUnit("lx", "lux", div(lm, m2));
4 changes: 2 additions & 2 deletions packages/units/src/substance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { defUnit, unit } from "./unit.js";
import { coherent, defUnit } from "./unit.js";

export const mol = defUnit("mol", "mole", unit(5, 1, 0, true));
export const mol = defUnit("mol", "mole", coherent(5));
4 changes: 2 additions & 2 deletions packages/units/src/temperature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defUnit, unit } from "./unit.js";
import { coherent, defUnit, unit } from "./unit.js";

export const K = defUnit("K", "kelvin", unit(4, 1));
export const K = defUnit("K", "kelvin", coherent(4));
export const celsius = defUnit("℃", "degree celsius", unit(4, 1, 273.15));
export const fahrenheit = defUnit(
"℉",
Expand Down
4 changes: 2 additions & 2 deletions packages/units/src/time.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defUnit, mul, prefix, unit } from "./unit.js";
import { coherent, defUnit, mul, prefix } from "./unit.js";

export const s = defUnit("s", "second", unit(2, 1, 0, true));
export const s = defUnit("s", "second", coherent(2));
export const ms = defUnit("ms", "millisecond", prefix("m", s));
export const µs = defUnit("µs", "microsecond", prefix("µ", s));
export const ns = defUnit("ns", "nanosecond", prefix("n", s));
Expand Down
7 changes: 7 additions & 0 deletions packages/units/src/unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export const unit = (
coherent,
});

/**
* Syntax sugar for defining coherent SI base units. See {@link unit}.
*
* @param dim
*/
export const coherent = (dim: Dimensions | number) => unit(dim, 1, 0, true);

/**
* Returns a new dimensionless unit (i.e. all SI dimensions are zero) with given
* `scale` factor.
Expand Down

0 comments on commit d2f7608

Please sign in to comment.