Skip to content

Commit

Permalink
feat(units): restructure /src, add quantities/constants
Browse files Browse the repository at this point in the history
- add /src/constants:
  - velocities
  - DIN paper sizes
- move all unit presets to /src/units
- update pkg exports
  • Loading branch information
postspectacular committed Mar 15, 2023
1 parent 94ded3e commit 1374162
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 79 deletions.
96 changes: 55 additions & 41 deletions packages/units/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,71 +83,85 @@
},
"files": [
"./*.js",
"./*.d.ts"
"./*.d.ts",
"constants",
"units"
],
"exports": {
".": {
"default": "./index.js"
},
"./accel": {
"default": "./accel.js"
},
"./angle": {
"default": "./angle.js"
},
"./api": {
"default": "./api.js"
},
"./area": {
"default": "./area.js"
"./constants/din-sizes": {
"default": "./constants/din-sizes.js"
},
"./data": {
"default": "./data.js"
"./constants/velocities": {
"default": "./constants/velocities.js"
},
"./electric": {
"default": "./electric.js"
"./unit": {
"default": "./unit.js"
},
"./energy": {
"default": "./energy.js"
"./units/accel": {
"default": "./units/accel.js"
},
"./force": {
"default": "./force.js"
"./units/angle": {
"default": "./units/angle.js"
},
"./frequency": {
"default": "./frequency.js"
"./units/area": {
"default": "./units/area.js"
},
"./length": {
"default": "./length.js"
"./units/data": {
"default": "./units/data.js"
},
"./luminous": {
"default": "./luminous.js"
"./units/density": {
"default": "./units/density.js"
},
"./mass": {
"default": "./mass.js"
"./units/electric": {
"default": "./units/electric.js"
},
"./power": {
"default": "./power.js"
"./units/energy": {
"default": "./units/energy.js"
},
"./pressure": {
"default": "./pressure.js"
"./units/force": {
"default": "./units/force.js"
},
"./speed": {
"default": "./speed.js"
"./units/frequency": {
"default": "./units/frequency.js"
},
"./substance": {
"default": "./substance.js"
"./units/length": {
"default": "./units/length.js"
},
"./temperature": {
"default": "./temperature.js"
"./units/luminous": {
"default": "./units/luminous.js"
},
"./time": {
"default": "./time.js"
"./units/mass": {
"default": "./units/mass.js"
},
"./unit": {
"default": "./unit.js"
"./units/parts": {
"default": "./units/parts.js"
},
"./units/power": {
"default": "./units/power.js"
},
"./units/pressure": {
"default": "./units/pressure.js"
},
"./units/substance": {
"default": "./units/substance.js"
},
"./units/temperature": {
"default": "./units/temperature.js"
},
"./units/time": {
"default": "./units/time.js"
},
"./units/velocity": {
"default": "./units/velocity.js"
},
"./volume": {
"default": "./volume.js"
"./units/volume": {
"default": "./units/volume.js"
}
},
"thi.ng": {
Expand Down
12 changes: 12 additions & 0 deletions packages/units/src/constants/din-sizes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { mm } from "../units/length.js";
import { quantity } from "../unit.js";

export const DIN_A0 = quantity([841, 1189], mm);
export const DIN_A1 = quantity([594, 841], mm);
export const DIN_A2 = quantity([420, 594], mm);
export const DIN_A3 = quantity([297, 420], mm);
export const DIN_A4 = quantity([210, 297], mm);
export const DIN_A5 = quantity([148, 210], mm);
export const DIN_A6 = quantity([105, 148], mm);
export const DIN_A7 = quantity([74, 105], mm);
export const DIN_A8 = quantity([52, 74], mm);
14 changes: 14 additions & 0 deletions packages/units/src/constants/velocities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { quantity } from "../unit.js";
import { m_s } from "../units/velocity.js";

export const SPEED_OF_LIGHT = quantity(299792458, m_s);

/**
* At 20 degree celsius
*/
export const SPEED_OF_SOUND_IN_AIR = quantity(343.14, m_s);

/**
* At 20 degree celsius
*/
export const SPEED_OF_SOUND_IN_WATER = quantity(1482, m_s);
42 changes: 23 additions & 19 deletions packages/units/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
export * from "./api.js";
export * from "./unit.js";

export * from "./accel.js";
export * from "./angle.js";
export * from "./area.js";
export * from "./data.js";
export * from "./electric.js";
export * from "./energy.js";
export * from "./force.js";
export * from "./frequency.js";
export * from "./length.js";
export * from "./luminous.js";
export * from "./mass.js";
export * from "./parts.js";
export * from "./power.js";
export * from "./pressure.js";
export * from "./speed.js";
export * from "./substance.js";
export * from "./temperature.js";
export * from "./time.js";
export * from "./volume.js";
export * from "./units/accel.js";
export * from "./units/angle.js";
export * from "./units/area.js";
export * from "./units/data.js";
export * from "./units/density.js";
export * from "./units/electric.js";
export * from "./units/energy.js";
export * from "./units/force.js";
export * from "./units/frequency.js";
export * from "./units/length.js";
export * from "./units/luminous.js";
export * from "./units/mass.js";
export * from "./units/parts.js";
export * from "./units/power.js";
export * from "./units/pressure.js";
export * from "./units/substance.js";
export * from "./units/temperature.js";
export * from "./units/time.js";
export * from "./units/velocity.js";
export * from "./units/volume.js";

export * from "./constants/din-sizes.js";
export * from "./constants/velocities.js";
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { rad } from "./angle.js";
import { ft, m } from "./length.js";
import { s } from "./time.js";
import { defUnit, div, mul, pow } from "./unit.js";
import { defUnit, div, mul, pow } from "../unit.js";

const s2 = pow(s, 2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defUnit, dimensionless, mul } from "./unit.js";
import { defUnit, dimensionless, mul } from "../unit.js";

const PI = Math.PI;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cm, ft, inch, km, m, mi, mm } from "./length.js";
import { defUnit, mul, pow } from "./unit.js";
import { defUnit, mul, pow } from "../unit.js";

export const m2 = defUnit("m2", "square meter", pow(m, 2));
export const mm2 = defUnit("mm2", "square millimeter", pow(mm, 2));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defUnit, dimensionless, mul, prefix } from "./unit.js";
import { defUnit, dimensionless, mul, prefix } from "../unit.js";

export const bit = defUnit("bit", "bit", dimensionless(1, 0, true));
export const kbit = defUnit("kbit", "kilobit", prefix("k", bit));
Expand Down
8 changes: 8 additions & 0 deletions packages/units/src/units/density.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { inch } from "./length.js";
import { kg } from "./mass.js";
import { defUnit, div, reciprocal } from "../unit.js";
import { m3 } from "./volume.js";

export const kg_m3 = defUnit("kg/m3", "kilogram per cubic meter", div(kg, m3));

export const dpi = defUnit("dpi", "dots per inch", reciprocal(inch));
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { m2 } from "./area.js";
import { J } from "./energy.js";
import { h, s } from "./time.js";
import { coherent, defUnit, div, mul, prefix } from "./unit.js";
import { coherent, defUnit, div, mul, prefix } from "../unit.js";

export const A = defUnit("A", "ampere", coherent(3));
export const mA = defUnit("mA", "milliampere", prefix("m", A));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { N } from "./force.js";
import { m } from "./length.js";
import { defUnit, mul, prefix } from "./unit.js";
import { defUnit, mul, prefix } from "../unit.js";

export const J = defUnit("J", "joule", mul(N, m, true));
export const kJ = defUnit("kJ", "kilojoule", prefix("k", J));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { m_s2 } from "./accel.js";
import { kg } from "./mass.js";
import { defUnit, mul } from "./unit.js";
import { defUnit, mul } from "../unit.js";

export const N = defUnit("N", "newton", mul(kg, m_s2, true));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { s } from "./time.js";
import { defUnit, div, mul, prefix, reciprocal } from "./unit.js";
import { defUnit, div, mul, prefix, reciprocal } from "../unit.js";

export const Hz = defUnit("Hz", "hertz", reciprocal(s, true));
export const kHz = defUnit("kHz", "kilohertz", prefix("k", Hz));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { coherent, defUnit, mul, prefix } from "./unit.js";
import { coherent, defUnit, mul, prefix } from "../unit.js";

export const m = defUnit("m", "meter", coherent(1));
export const km = defUnit("km", "kilometer", prefix("k", m));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sr } from "./angle.js";
import { m2 } from "./area.js";
import { coherent, defUnit, div, mul } from "./unit.js";
import { coherent, defUnit, div, mul } from "../unit.js";

export const cd = defUnit("cd", "candela", coherent(6));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defUnit, mul, prefix, unit } from "./unit.js";
import { defUnit, mul, prefix, unit } from "../unit.js";

export const g = defUnit("g", "gram", unit(0, 1e-3, 0, true));
export const kg = defUnit("kg", "kilogram", prefix("k", g));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defUnit, dimensionless } from "./unit.js";
import { defUnit, dimensionless } from "../unit.js";

export const percent = defUnit("%", "percent", dimensionless(1e-2));
export const permille = defUnit("‰", "permille", dimensionless(1e-3));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { J } from "./energy.js";
import { h, s } from "./time.js";
import { defUnit, div, mul, prefix } from "./unit.js";
import { defUnit, div, mul, prefix } from "../unit.js";

export const W = defUnit("W", "watt", div(J, s, true));
export const mW = defUnit("mW", "milliwatt", prefix("m", W));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { m2 } from "./area.js";
import { N } from "./force.js";
import { defUnit, div, mul, prefix } from "./unit.js";
import { defUnit, div, mul, prefix } from "../unit.js";

export const Pa = defUnit("Pa", "pascal", div(N, m2, true));
export const kPa = defUnit("kPa", "kilopascal", prefix("k", Pa));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { coherent, defUnit } from "./unit.js";
import { coherent, defUnit } from "../unit.js";

export const mol = defUnit("mol", "mole", coherent(5));
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { coherent, defUnit, unit } from "./unit.js";
import { coherent, defUnit, unit } from "../unit.js";

export const K = defUnit("K", "kelvin", coherent(4));
export const celsius = defUnit("℃", "degree celsius", unit(4, 1, 273.15));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { coherent, defUnit, mul, prefix } from "./unit.js";
import { coherent, defUnit, mul, prefix } from "../unit.js";

export const s = defUnit("s", "second", coherent(2));
export const ms = defUnit("ms", "millisecond", prefix("m", s));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defUnit, div } from "./unit.js";
import { defUnit, div } from "../unit.js";
import { ft, km, m, mi, nmi } from "./length.js";
import { h, s } from "./time.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cm, km, m, mm } from "./length.js";
import { defUnit, mul, pow, prefix } from "./unit.js";
import { defUnit, mul, pow, prefix } from "../unit.js";

export const m3 = defUnit("m3", "cubic meter", pow(m, 3));
export const mm3 = defUnit("mm3", "cubic millimeter", pow(mm, 3));
Expand Down

0 comments on commit 1374162

Please sign in to comment.