Skip to content

Commit daa9b05

Browse files
authored
Docs update for 2.0 (#191)
1 parent 7eadf67 commit daa9b05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+750
-537
lines changed

docs/builtin.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Built-in Units
22

3-
While users can define their own unit system (see [Defining Quantities](defining-quantities.html)), Safe Units also comes with a large collection of built-in quantities and units based on the SI unit system.
3+
While users can [define their own unit system](unit-systems.html), Safe Units also comes with a large collection of built-in quantities and units based on the SI unit system.
44

55
All quantities provided are generic and of the form `type Quantity<N = number>` so all quantity types will default to using `number` as the numeric type but may be passed another numeric type.
66

@@ -28,31 +28,17 @@ While plane and solid angles are defined as dimensionless in the SI specificatio
2828

2929
The base units are only provided for `number` measures, but users can create the base units for a given measure type through:
3030

31-
```ts
32-
type SpecialNumber = /* ... */;
33-
const numericOps: NumericOperations<SpecialNumber> = {/* ... */};
34-
const SpecialMeasure = createMeasureType(numericOps);
35-
const {
36-
meters,
37-
kilograms,
38-
seconds,
39-
amperes,
40-
kelvin,
41-
moles,
42-
candelas,
43-
radians,
44-
steradians,
45-
bits,
46-
} = createBaseUnits(SpecialMeasure);
31+
```example
32+
builtinBaseUnits.ts
4733
```
4834

49-
These versions of `meters`, `kilograms`, etc. will all operator on measures whose numeric types are `SpecialNumber`.
35+
These versions of `meters`, `kilograms`, etc. will all operate on measures whose numeric types are `BigNumber`.
5036

5137
## Provided Units
5238

5339
A large number of built in quantities (e.g. distance, velocity, force and magnetic flux density) are provided. See [here](https://github.com/jscheiny/safe-units/blob/master/src/unit/quantities.ts) for a full list.
5440

55-
Many units are provided from the metric, Imperial and U.S. customary unit systems. See [here](https://github.com/jscheiny/safe-units/tree/master/src/unit) for all of the units.
41+
Many units are provided from the SI, Imperial and U.S. customary unit systems. See [here](https://github.com/jscheiny/safe-units/tree/master/src/unit) for all of the units.
5642

5743
Prefix functions are provided for both standard [SI prefixes](https://github.com/jscheiny/safe-units/blob/master/src/unit/metric.ts) and for [memory prefixes](https://github.com/jscheiny/safe-units/blob/master/src/unit/memory.ts).
5844

docs/defining-quantities.md

Lines changed: 0 additions & 109 deletions
This file was deleted.

docs/examples/builtinBaseUnits.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Length, NumericOperations, createMeasureType, createSIBaseUnits } from "safe-units";
2+
3+
class BigNumber {
4+
constructor(public value: string) {}
5+
}
6+
declare const numericOps: NumericOperations<BigNumber>;
7+
8+
// START
9+
const BigNumberMeasure = createMeasureType<BigNumber>(numericOps);
10+
const { meters, kilograms, seconds, ...rest } = createSIBaseUnits(BigNumberMeasure);
11+
12+
const bigValue = new BigNumber("3.14");
13+
const bigLength: Length<BigNumber> = BigNumberMeasure.of(bigValue, meters);
14+
// END
15+
16+
console.log(bigLength, meters, kilograms, seconds, rest);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// START
2+
class WrappedNumber {
3+
constructor(public readonly value: number) {}
4+
5+
declare foo: () => WrappedNumber;
6+
// ...
7+
}
8+
9+
function wrap(value: number) {
10+
return new WrappedNumber(value);
11+
}
12+
// END
13+
14+
export { WrappedNumber, wrap };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { WrappedNumber, wrap } from "./genericMeasureIntro";
2+
3+
// START
4+
import { createMeasureType, GenericMeasure, Unit } from "safe-units";
5+
6+
type WrappedMeasure<B, U extends Unit<B>> = GenericMeasure<WrappedNumber, B, U>;
7+
const WrappedMeasure = createMeasureType<WrappedNumber>({
8+
one: () => wrap(1),
9+
neg: x => wrap(-x.value),
10+
add: (x, y) => wrap(x.value + y.value),
11+
sub: (x, y) => wrap(x.value - y.value),
12+
mult: (x, y) => wrap(x.value * y.value),
13+
div: (x, y) => wrap(x.value / y.value),
14+
reciprocal: x => wrap(1 / x.value),
15+
compare: (x, y) => x.value - y.value,
16+
format: x => `${x}`,
17+
});
18+
// END
19+
20+
console.log(WrappedMeasure);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GenericMeasure, Mass, NumericOperations, Unit, createMeasureType, wrapUnaryFn } from "safe-units";
2+
3+
class WrappedNumber {}
4+
type WrappedMeasure<B, U extends Unit<B>> = GenericMeasure<WrappedNumber, B, U>;
5+
declare const numericOperations: NumericOperations<WrappedNumber>;
6+
7+
// START
8+
declare function foo(value: WrappedNumber): WrappedNumber;
9+
declare const mass: Mass<WrappedNumber>;
10+
11+
const WrappedMeasure = createMeasureType(numericOperations, {
12+
foo: wrapUnaryFn(foo),
13+
});
14+
15+
WrappedMeasure.foo(mass);
16+
// END

docs/examples/intro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ console.log(velocity.toString()); // 2 m * s^-1
1313
const error: Velocity = length.times(time);
1414
// END
1515

16-
// Ensure that error is used
16+
// Ensure that variables are used
1717
console.log(error);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { kilograms, liters, Mass, Measure, Volume } from "safe-units";
2+
3+
const VolumeDensity = Mass.over(Volume);
4+
type VolumeDensity = typeof VolumeDensity;
5+
6+
const mass = Measure.of(30, kilograms);
7+
const volume = Measure.of(3, liters);
8+
9+
const density: VolumeDensity = mass.over(volume);
10+
11+
console.log(density.toString()); // 10 kg * m^-3

docs/examples/introNamingUnits.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { days, Measure, mega, micro, miles, speedOfLight, yards } from "safe-units";
2+
3+
const furlongs = Measure.of(220, yards, "fur");
4+
5+
Measure.of(8, furlongs).in(miles); // "1 mi"
6+
Measure.of(1, miles).in(furlongs); // "8 fur"
7+
8+
const fortnights = Measure.of(14, days, "ftn");
9+
const megafurlong = mega(furlongs);
10+
const microfortnight = micro(fortnights);
11+
const mfPerUFtn = megafurlong.per(microfortnight).withSymbol("Mfur/µftn");
12+
13+
speedOfLight.in(mfPerUFtn); // "1.8026174997852542 Mfur/µftn"

docs/examples/introTypeErrors.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Force, hours, Length, Measure, meters, seconds, Time } from "safe-units";
2+
3+
const length: Length = Measure.of(10, meters);
4+
const time: Time = Measure.of(10, seconds);
5+
6+
// @ts-expect-error - Measures of different units cannot be added
7+
length.plus(time);
8+
9+
// @ts-expect-error - Measures of different units cannot be compared
10+
length.eq(time);
11+
12+
// @ts-expect-error - Measure of m/s is not assigneable to measure of kg*m/s^2
13+
const force: Force = length.over(time);
14+
15+
// @ts-expect-error - Cannot convert length measure to time measure
16+
length.in(hours);

0 commit comments

Comments
 (0)