From 72d11de57aa2e3faf5eff1cc2d4f520a29a45446 Mon Sep 17 00:00:00 2001 From: Paulius Varna Date: Thu, 25 Jul 2019 13:23:14 +0300 Subject: [PATCH] Annotate (#9) * annotated unit keys, fixes #7 * annotated Measure and measure.to * annotated measureFrom --- README.md | 73 ++++++++ __tests__/index.ts | 28 +-- src/findUnit.ts | 4 +- src/measure.ts | 39 +++-- src/measureSelector.ts | 26 ++- src/unitKeys.ts | 33 ---- src/units.ts | 381 ----------------------------------------- src/units/Unit.ts | 22 +++ src/units/area.ts | 59 +++++++ src/units/index.ts | 80 +++++++++ src/units/length.ts | 109 ++++++++++++ src/units/mass.ts | 129 ++++++++++++++ src/units/volume.ts | 143 ++++++++++++++++ 13 files changed, 679 insertions(+), 447 deletions(-) delete mode 100644 src/unitKeys.ts delete mode 100644 src/units.ts create mode 100644 src/units/Unit.ts create mode 100644 src/units/area.ts create mode 100644 src/units/index.ts create mode 100644 src/units/length.ts create mode 100644 src/units/mass.ts create mode 100644 src/units/volume.ts diff --git a/README.md b/README.md index 8c35049..fcf27e5 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,79 @@ measure(10000) // 10_000 || "1 ha" measure(1000000) // 1_000_000 || "1 km²" ``` +## Units + +### SI prefixes + +Meters (normal, square and cubic), grams and litres are prefixable with [SI prefixes](https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes). Examples: + +| Key | Unit | Value | +| --- | ---------------- | --------------- | +| km | kilometre | 1 \* 1000 | +| cm | centimetre | 1 / 100 | +| ml | mililitre | 1 / 1000 | +| km2 | square kilometre | 1 _ 1000 _ 1000 | + +### Length + +| Key | Name | Value | +| --- | ------------- | --------------- | +| m | Meter (meter) | 1 | +| M | Nautical Mile | Metre \* 1852 | +| in | Inch | Foot / 12 | +| ft | Foot | Yard / 3 | +| yd | Yard | Metre \* 0.9144 | +| ch | Chain | Yard \* 22 | +| fur | Furlong | Chain \* 10 | +| mi | Mile | Furlong \* 8 | +| lea | League | Mile \* 3 | + +### Area + +Square length units can be used to measure area, i.e. "m2". + +| Key | Name | Value | +| --- | ------- | ----------------- | +| a | Are | Metre \*_ 2 _ 100 | +| ha | Hectare | Are \* 100 | +| ac | Acre | Yard \*_ 2 _ 4840 | + +### Volume + +Cubic length units can be used to measure volume, i.e. "m3". + +I'm not sure how imperial volume units "really" work (in practise), so create an issue or PR to fix anything that is a miss. + +| Key | Name | Value | +| ----- | ---------------------- | -------------------------- | +| l | Litre | Metre \*\* 3 / 1000 | +| pint | Pint (Imperial) | Litre \* 0.568 | +| gal | Gallon (American) | Inch \*\* 3 / 231 | +| qt | Quart (American) | Gallon / 4 | +| pt | Pint (American) | Gallon / 8 | +| gi | Gill (American) | Gallon / 32 | +| fl oz | Fluid Ounce (American) | Gallon / 128 | +| peck | Peck | Litre \* 8.809_768 | +| bu | Bushel | Litre \* 35.239_070_166_88 | + +### Mass (weight) + +| Key | Name | Value | +| ------- | ------------------------------ | ------------------ | +| kg | Kilogram | 1 | +| g | Gram | Kilogram / 1000 | +| lb | Pound | Gram \* 453.592_37 | +| t | Tonne (Metric Ton) | Kilogram \* 1000 | +| ton | American Ton (short) | Pound \* 2000 | +| longton | Imperial Ton (long) | Pound \* 2240 | +| cwt | American Hundredweight (short) | Pound \* 100 | +| longcwt | Imperial Hundredweight (long) | Pound \* 112 | +| qr | Quarter | Pound \* 28 | +| st | Stone | Pound \* 14 | +| oz | Ounce | Pound / 16 | +| dr | Drachm | Pound / 256 | +| gr | Grain | Pound / 7000 | + ## Roadmap - [Suggest units](https://stackoverflow.com/questions/56947641/generating-union-string-type) diff --git a/__tests__/index.ts b/__tests__/index.ts index 2b7a5ae..88a82ca 100644 --- a/__tests__/index.ts +++ b/__tests__/index.ts @@ -262,10 +262,10 @@ describe('Volume units', () => { expect(String(res)).toBe('1 pt') }) - test('pt', () => { + test('pint', () => { const measure = new Measure(0.000568261) - const res = measure.to('pint_long') - expect(String(res)).toBe('1 pt') + const res = measure.to('pint') + expect(String(res)).toBe('1 pint') }) test('fl oz', () => { @@ -301,16 +301,16 @@ describe('Weight units', () => { const res = measure.to('t') expect(String(res)).toBe('1 t') }) - test('t US', () => { + test('ton', () => { const measure = new Measure(907.185) - const res = measure.to('ton_short') - expect(String(res)).toBe('1 t') + const res = measure.to('ton') + expect(String(res)).toBe('1 ton') }) - test('t UK', () => { + test('longton', () => { const measure = new Measure(1016.05) - const res = measure.to('ton_long') - expect(String(res)).toBe('1 t') + const res = measure.to('longton') + expect(String(res)).toBe('1 longton') }) test('lb', () => { @@ -349,15 +349,15 @@ describe('Weight units', () => { expect(String(res)).toBe('1 qr') }) - test('cwt UK', () => { + test('longcwt', () => { const measure = new Measure(50.8) - const res = measure.to('hundredweight_long') - expect(String(res)).toBe('1 cwt') + const res = measure.to('longcwt') + expect(String(res)).toBe('1 longcwt') }) - test('cwt US', () => { + test('cwt', () => { const measure = new Measure(45.36) - const res = measure.to('hundredweight_short') + const res = measure.to('cwt') expect(String(res)).toBe('1 cwt') }) }) diff --git a/src/findUnit.ts b/src/findUnit.ts index 1bf7202..10fcc2e 100644 --- a/src/findUnit.ts +++ b/src/findUnit.ts @@ -1,11 +1,11 @@ -import units from './units' +import units, { UnitKey } from './units' import siPrefixes from './SIPrefixes' const SQUARE_SYMBOLS = ['²', '2'] const CUBE_SYMBOLS = ['³', '3'] const findUnit = ( - unitString: string + unitString: UnitKey ): { unit: string prefix: string diff --git a/src/measure.ts b/src/measure.ts index 9a21488..cc532db 100644 --- a/src/measure.ts +++ b/src/measure.ts @@ -1,9 +1,14 @@ -// import siPrefixes from './SIPrefixes' -// import units, { LENGTH } from './units' import findUnit from './findUnit' +import { UnitKey } from './units' /** - * Measure space in meters. + * Measure of value with unit. + * Length, area, volume and mass units. + * SI, metric and imperial units. + * Squared and cubed length units (m2, m3, km2, m3, ft3, mi2). + * SI prefixes (da, h, k, M, G, T, P, E, Z, Y, d, c, m, μ, n, p, f, a, z, y) for (m, m2, m3, l, g) i.e. (km2, kg, cm, ml). + * + * @returns {Number|String} `Measure.valueOf(): Number` or `Measure.toString(): String` depending on context */ export default class Measure extends Number implements Number { public precision = 12 @@ -14,10 +19,14 @@ export default class Measure extends Number implements Number { public suffix = '' public round = 2 - constructor(value: number | string | Measure, unitString?: string) { + /** + * @param value value/quantity of unit + * @param unitKey key representation of unit + */ + constructor(value: number | string | Measure, unitKey?: UnitKey) { super(value) - if (unitString) { - const { unit, prefix, suffix, ratio } = findUnit(unitString.trim()) + if (unitKey) { + const { unit, prefix, suffix, ratio } = findUnit(unitKey) this.symbol = unit this.prefix = prefix this.suffix = suffix @@ -25,11 +34,13 @@ export default class Measure extends Number implements Number { } } - public to = (unitString: string): Measure => { - // check if squared or cubed - let trimmed = unitString.trim() - - const { unit, prefix, suffix, ratio } = findUnit(trimmed) + /** + * Convert Measure to another Measure with different Unit. + * + * @param unitKey key representation of unit + */ + public to = (unitKey: UnitKey): Measure => { + const { unit, prefix, suffix, ratio } = findUnit(unitKey) this.symbol = unit this.prefix = prefix this.suffix = suffix @@ -41,6 +52,12 @@ export default class Measure extends Number implements Number { return this.clone(newValue) } + /** + * Create a copy of of Measure with new value. + * Why? Because value of Number that we extend is immutable. + * + * @param value new value + */ public clone(value: number) { const measure = new Measure(value) measure.ratio = this.ratio diff --git a/src/measureSelector.ts b/src/measureSelector.ts index 7dd5f9c..85cdc28 100644 --- a/src/measureSelector.ts +++ b/src/measureSelector.ts @@ -1,5 +1,6 @@ import findUnit from './findUnit' import Measure from './measure' +import { UnitKey } from './units' const toMeasure = ( { @@ -14,18 +15,29 @@ const toMeasure = ( value: number ) => { const unitString = prefix + unit + suffix - return new Measure(value).to(unitString) + return new Measure(value).to(unitString) } -const createMeasureSelector = (...args: string[]) => { - const units = args.map(findUnit).sort((a, b) => b.ratio - a.ratio) +/** + * Creates a function that finds best looking unit for value from a list of provided units. + * @param unitKeys an Array of unit identifiers. + * @returns {(value: number) => Measure} Finds best looking unit for pretty printing and returns Measure with it. + */ +const createMeasureSelector = (...unitKeys: UnitKey[]) => { + const units = unitKeys.map(findUnit).sort((a, b) => b.ratio - a.ratio) - return function(value: number) { + /** + * Finds best looking unit for pretty printing and returns Measure with it. + * + * @param value quantity/value of measure. + * @returns {Measure} new Measure(value, bestLookingUnit) + */ + const finder = (value: number) => { const found = units.reduce( // find smallest unit larger than 1 (previousUnit, currentUnit) => { - const currentQuantity = Number(toMeasure(currentUnit, value)) //currentUnit.quantity(value) - const previousQuantity = Number(toMeasure(previousUnit, value)) // previousUnit.quantity(value) + const currentQuantity = Number(toMeasure(currentUnit, value)) + const previousQuantity = Number(toMeasure(previousUnit, value)) return previousQuantity < 1 || currentQuantity < previousQuantity ? currentUnit @@ -41,6 +53,8 @@ const createMeasureSelector = (...args: string[]) => { return foundMeasure } } + + return finder } export default createMeasureSelector diff --git a/src/unitKeys.ts b/src/unitKeys.ts deleted file mode 100644 index dd80fcc..0000000 --- a/src/unitKeys.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { stringEnum } from './stringEnum' -import siPrefixes from './SIPrefixes' -import units, { LENGTH } from './units' - -const keys = [] -const prefixes = Object.values(siPrefixes).map(prefix => prefix.symbol) - -Object.values(units).forEach(({ isPrefixable, type, name, symbol }) => { - let unitKeys = [symbol] - if (type === LENGTH) { - ;['²', '2', '³', '3'].forEach(suffix => { - unitKeys.push(symbol + suffix) - }) - } - if (isPrefixable) { - prefixes.forEach(prefix => { - unitKeys.forEach(unitKey => { - keys.push(prefix + unitKey) - }) - }) - } else { - // keys = [...keys, ...unitKeys] - } - keys.push(name) -}) - -/** Create a K:V */ -const UnitString = stringEnum(keys) - -/** Create a Type */ -export type UnitString = keyof typeof UnitString - -// const test: UnitString = '' diff --git a/src/units.ts b/src/units.ts deleted file mode 100644 index 7343673..0000000 --- a/src/units.ts +++ /dev/null @@ -1,381 +0,0 @@ -export const LENGTH = 'length' -export const AREA = 'area' -export const VOLUME = 'volume' -export const MASS = 'mass' - -export type MeasureType = 'length' | 'area' | 'volume' | 'mass' -export type Unit = { - name: string - symbol: string - alternativeSymbol?: string - ratio: number - type: MeasureType - isPrefixable?: true -} - -/** - * Length - SI - */ -export const metre: Unit = { - name: 'metre', - symbol: 'm', - ratio: 1, - type: LENGTH, - isPrefixable: true -} - -/** - * Length - Imperial - * Since 1959 it is by international agreement standardized as exactly 0.9144 meters. - * https://en.wikipedia.org/wiki/International_yard_and_pound - */ -export const yard: Unit = { - name: 'yard', - symbol: 'yd', - ratio: metre.ratio * 0.9144, - type: LENGTH -} - -export const foot: Unit = { - name: 'foot', - symbol: 'ft', - alternativeSymbol: '′', - ratio: yard.ratio / 3, - type: LENGTH -} - -export const inch: Unit = { - name: 'inch', - symbol: 'in', - alternativeSymbol: '″', - ratio: foot.ratio / 12, - type: LENGTH -} - -export const chain: Unit = { - name: 'chain', - symbol: 'ch', - ratio: yard.ratio * 22, - type: LENGTH -} - -export const furlong: Unit = { - name: 'furlong', - symbol: 'fur', - ratio: chain.ratio * 10, - type: LENGTH -} - -export const mile: Unit = { - name: 'mile', - symbol: 'mi', - ratio: furlong.ratio * 8, - type: LENGTH -} - -export const league: Unit = { - name: 'league', - symbol: 'lea', - ratio: mile.ratio * 3, - type: LENGTH -} - -/** - * Nautical Mile (M) - * https://www.bipm.org/utils/common/pdf/si_brochure_8_en.pdf - */ -export const nautical_mile: Unit = { - name: 'nautical_mile', - symbol: 'M', - ratio: metre.ratio * 1852, - type: LENGTH -} - -/** - * Are - Common land area measuremnt unit - */ -export const are: Unit = { - name: 'are', - symbol: 'a', - ratio: metre.ratio * 100, - type: AREA -} - -/** - * Hectare - 100 are - */ -export const hectare: Unit = { - name: 'hectare', - symbol: 'ha', - ratio: are.ratio * 100, - type: AREA -} - -/** - * Acre - Imperial land area measuremnt unit - */ -export const acre: Unit = { - name: 'acre', - symbol: 'ac', - ratio: Math.pow(yard.ratio, 2) * 4840, - type: AREA -} - -// /** -// * The board foot or board-foot is a unit of measurement for the volume of lumber in the United States and Canada. -// */ -// export const board_foot: Unit = { -// name: 'board_foot', -// symbol: "FBM", -// ratio: Math.pow(inch.ratio * 144, 3), -// type: AREA, -// } - -/** - * Volume - not SI (cubic meters are SI unit for volume). - * Can use SI prefixes. - */ -export const litre: Unit = { - name: 'litre', - symbol: 'l', - alternativeSymbol: 'ℓ', - ratio: metre.ratio * 0.001, - isPrefixable: true, - type: VOLUME -} - -/** - * U.S. Gallon. (U.K. gallon is deprecated) - * 3.785411784 litres - */ -export const gallon: Unit = { - name: 'gallon', - symbol: 'gal', - ratio: Math.pow(inch.ratio, 3) * 231, - type: VOLUME -} - -export const quart: Unit = { - name: 'quart', - symbol: 'qt', - ratio: gallon.ratio / 4, - type: VOLUME -} - -/** - * Imperial (U.S.) Pint is 1/8 gallon - */ -export const pint: Unit = { - name: 'pint', - symbol: 'pt', - ratio: gallon.ratio / 8, - type: VOLUME -} - -/** - * Imperial (U.K.) Pint is 568ml - */ -export const pint_long: Unit = { - name: 'pint_long', - symbol: 'pt', - ratio: litre.ratio * 0.568, - type: VOLUME -} - -export const gill: Unit = { - name: 'gill', - symbol: 'gi', - ratio: gallon.ratio / 32, - type: VOLUME -} - -export const fluid_ounce: Unit = { - name: 'fluid_ounce', - symbol: 'fl oz', - ratio: gallon.ratio / 128, - type: VOLUME -} - -// /** -// * Dry Volume - a.k.a Farming units -// * 1 bushel = 2 kennings (obsolete) = 4 pecks = 8 dry gallons = 16 pint -// */ -// export const dry_pint: Unit = { -// name: 'dry_pint', -// symbol: 'pt', -// ratio: litre.ratio * 0.550_610_471_357_5 -// } -// export const dry_quart: Unit = { -// name: 'dry_quart', -// symbol: 'qt', -// ratio: litre.ratio * 1.101_221 -// } -// export const dry_gallon: Unit = { -// name: 'dry_gallon', -// symbol: 'gal', -// ratio: litre.ratio * 4.404_884 -// } - -export const peck: Unit = { - name: 'peck', - symbol: 'pk', - ratio: litre.ratio * 8.809_768, - type: VOLUME -} - -export const bushel: Unit = { - name: 'bushel', - symbol: 'bu', - ratio: litre.ratio * 35.239_070_166_88, - type: VOLUME -} - -// export const dry_barrel: Unit = { -// name: 'dry_barrel', -// symbol: 'bbl', -// ratio: litre.ratio * 115.6271 -// } - -/** - * Weight - SI - (well, actually kg is SI unit, not gram) - */ -export const gram: Unit = { - name: 'gram', - symbol: 'g', - ratio: 0.001, - isPrefixable: true, - type: MASS -} - -/** - * Weight - Common - */ -export const tonne: Unit = { - name: 'tonne', - symbol: 't', - ratio: gram.ratio * 1_000_000, - type: MASS -} - -/** - * Weight - Imperial - */ -export const pound: Unit = { - name: 'pound', - symbol: 'lb', - ratio: gram.ratio * 453.592_37, - type: MASS -} - -export const grain: Unit = { - name: 'grain', - symbol: 'gr', - ratio: pound.ratio / 7_000, - type: MASS -} - -export const drachm: Unit = { - name: 'drachm', - symbol: 'dr', - ratio: pound.ratio / 256, - type: MASS -} - -export const ounce: Unit = { - name: 'ounce', - symbol: 'oz', - ratio: pound.ratio / 16, - type: MASS -} - -export const stone: Unit = { - name: 'stone', - symbol: 'st', - ratio: pound.ratio * 14, - type: MASS -} - -export const quarter: Unit = { - name: 'quarter', - symbol: 'qr', - ratio: pound.ratio * 28, - type: MASS -} - -/** - * Hundredweight - Imperial - long (UK) - */ -export const hundredweight_long: Unit = { - name: 'hundredweight_long', - symbol: 'cwt', - ratio: pound.ratio * 112, - type: MASS -} - -/** - * Hundredweight - Imperial - short (US and Canadian) - */ -export const hundredweight_short: Unit = { - name: 'hundredweight_short', - symbol: 'cwt', - ratio: pound.ratio * 100, - type: MASS -} - -/** - * Ton - Imperial - long (UK) - */ -export const ton_long: Unit = { - name: 'ton_long', - symbol: 't', - ratio: pound.ratio * 2240, - type: MASS -} - -/** - * Ton - Imperial - short (US and Canadian) - */ -export const ton_short: Unit = { - name: 'ton_short', - symbol: 't', - ratio: pound.ratio * 2000, - type: MASS -} - -const units = { - metre, - yard, - foot, - inch, - chain, - furlong, - mile, - league, - nautical_mile, - are, - hectare, - acre, - litre, - gallon, - quart, - pint, - pint_long, - gill, - fluid_ounce, - peck, - bushel, - gram, - tonne, - pound, - grain, - drachm, - ounce, - stone, - quarter, - hundredweight_long, - hundredweight_short, - ton_long, - ton_short -} - -export default units diff --git a/src/units/Unit.ts b/src/units/Unit.ts new file mode 100644 index 0000000..f604d31 --- /dev/null +++ b/src/units/Unit.ts @@ -0,0 +1,22 @@ +export type MeasureType = + | 'time' // second (s) + | 'length' // metre (m) + | 'area' // metre (m) ** 2 + | 'volume' // metre (m) ** 3 + | 'mass' // kilogram (kg) + | 'electric current' // ampere (A) + | 'temperature' // kelvin (K) + | 'amount of substance' // mole (mol) + | 'luminous intensity' // candela (cd) + +type Unit = { + name: string + symbol: string + alternativeSymbol?: string + ratio: number + type: MeasureType + isPrefixable?: true + description?: string +} + +export default Unit diff --git a/src/units/area.ts b/src/units/area.ts new file mode 100644 index 0000000..ae63aa7 --- /dev/null +++ b/src/units/area.ts @@ -0,0 +1,59 @@ +import Unit, { MeasureType } from './Unit' +import { metre, yard } from './length' + +const type: MeasureType = 'area' + +export const are: Unit = { + name: 'are', + symbol: 'a', + ratio: metre.ratio * 100, + type +} + +export const hectare: Unit = { + name: 'hectare', + symbol: 'ha', + ratio: are.ratio * 100, + type +} + +export const acre: Unit = { + name: 'acre', + symbol: 'ac', + ratio: Math.pow(yard.ratio, 2) * 4840, + type +} + +export type AreaUnitKey = + | 'ha' + | 'a' + | 'ac' + | 'yd2' + | 'ft2' + | 'in2' + | 'ch2' + | 'fur2' + | 'mi2' + | 'lea2' + | 'M2' + | 'm2' + | 'Ym2' + | 'Zm2' + | 'Em2' + | 'Pm2' + | 'Tm2' + | 'Gm2' + | 'Mm2' + | 'km2' + | 'hm2' + | 'dam2' + | 'dm2' + | 'cm2' + | 'mm2' + | 'μm2' + | 'nm2' + | 'pm2' + | 'fm2' + | 'am2' + | 'zm2' + | 'ym2' diff --git a/src/units/index.ts b/src/units/index.ts new file mode 100644 index 0000000..6fa2fab --- /dev/null +++ b/src/units/index.ts @@ -0,0 +1,80 @@ +import { + LenghtUnitKey, + metre, + yard, + foot, + inch, + chain, + furlong, + mile, + league, + nautical_mile +} from './length' +import { AreaUnitKey, are, hectare, acre } from './area' +import { + VolumeUnitKey, + litre, + gallon, + quart, + pint, + pint_long, + gill, + fluid_ounce, + peck, + bushel +} from './volume' +import { + MassUnitKey, + gram, + tonne, + ton_short, + ton_long, + pound, + grain, + drachm, + ounce, + stone, + quarter, + hundredweight_long, + hundredweight_short +} from './mass' + +const units = { + metre, + yard, + foot, + inch, + chain, + furlong, + mile, + league, + nautical_mile, + are, + hectare, + acre, + litre, + gallon, + quart, + pint, + pint_long, + gill, + fluid_ounce, + peck, + bushel, + gram, + tonne, + pound, + grain, + drachm, + ounce, + stone, + quarter, + hundredweight_long, + hundredweight_short, + ton_long, + ton_short +} + +export default units + +export type UnitKey = LenghtUnitKey | AreaUnitKey | VolumeUnitKey | MassUnitKey diff --git a/src/units/length.ts b/src/units/length.ts new file mode 100644 index 0000000..fa965c2 --- /dev/null +++ b/src/units/length.ts @@ -0,0 +1,109 @@ +import Unit, { MeasureType } from './Unit' + +const type: MeasureType = 'length' + +export const metre: Unit = { + name: 'metre', + symbol: 'm', + ratio: 1, + type, + isPrefixable: true +} + +/** + * Yard - Imperial length base unit. + * Since 1959 it is by international agreement standardized as exactly 0.9144 meters. + * https://en.wikipedia.org/wiki/International_yard_and_pound + */ +export const yard: Unit = { + name: 'yard', + symbol: 'yd', + ratio: metre.ratio * 0.9144, + type +} + +export const foot: Unit = { + name: 'foot', + symbol: 'ft', + alternativeSymbol: '′', + ratio: yard.ratio / 3, + type +} + +export const inch: Unit = { + name: 'inch', + symbol: 'in', + alternativeSymbol: '″', + ratio: foot.ratio / 12, + type +} + +export const chain: Unit = { + name: 'chain', + symbol: 'ch', + ratio: yard.ratio * 22, + type +} + +export const furlong: Unit = { + name: 'furlong', + symbol: 'fur', + ratio: chain.ratio * 10, + type +} + +export const mile: Unit = { + name: 'mile', + symbol: 'mi', + ratio: furlong.ratio * 8, + type +} + +export const league: Unit = { + name: 'league', + symbol: 'lea', + ratio: mile.ratio * 3, + type +} + +/** + * Nautical Mile (M) + * https://www.bipm.org/utils/common/pdf/si_brochure_8_en.pdf + */ +export const nautical_mile: Unit = { + name: 'nautical_mile', + symbol: 'M', + ratio: metre.ratio * 1852, + type +} + +export type LenghtUnitKey = + | 'yd' + | 'ft' + | 'in' + | 'ch' + | 'fur' + | 'mi' + | 'lea' + | 'M' + | 'm' + | 'Ym' + | 'Zm' + | 'Em' + | 'Pm' + | 'Tm' + | 'Gm' + | 'Mm' + | 'km' + | 'hm' + | 'dam' + | 'dm' + | 'cm' + | 'mm' + | 'μm' + | 'nm' + | 'pm' + | 'fm' + | 'am' + | 'zm' + | 'ym' diff --git a/src/units/mass.ts b/src/units/mass.ts new file mode 100644 index 0000000..9853381 --- /dev/null +++ b/src/units/mass.ts @@ -0,0 +1,129 @@ +import Unit, { MeasureType } from './Unit' + +const type: MeasureType = 'mass' + +/** + * Base SI unit for mass is actually kg + * and its value will be used as base ratio + * but we will define only gram, + * because it is prefixable unit + */ +export const gram: Unit = { + name: 'gram', + symbol: 'g', + ratio: 0.001, + isPrefixable: true, + type +} + +export const tonne: Unit = { + name: 'tonne', + symbol: 't', + ratio: gram.ratio * 1_000_000, + description: 'Tonne (metric ton) - 1000 kg - SI accepted unit.', + type +} + +export const pound: Unit = { + name: 'pound', + symbol: 'lb', + ratio: gram.ratio * 453.592_37, + type +} + +export const grain: Unit = { + name: 'grain', + symbol: 'gr', + ratio: pound.ratio / 7_000, + type +} + +export const drachm: Unit = { + name: 'drachm', + symbol: 'dr', + ratio: pound.ratio / 256, + type +} + +export const ounce: Unit = { + name: 'ounce', + symbol: 'oz', + ratio: pound.ratio / 16, + type +} + +export const stone: Unit = { + name: 'stone', + symbol: 'st', + ratio: pound.ratio * 14, + type +} + +export const quarter: Unit = { + name: 'quarter', + symbol: 'qr', + ratio: pound.ratio * 28, + type +} + +export const hundredweight_long: Unit = { + name: 'hundredweight_long', + symbol: 'longcwt', + ratio: pound.ratio * 112, + type +} + +export const hundredweight_short: Unit = { + name: 'hundredweight_short', + symbol: 'cwt', + ratio: pound.ratio * 100, + type +} + +export const ton_long: Unit = { + name: 'ton_long', + symbol: 'longton', + ratio: pound.ratio * 2240, + type +} + +export const ton_short: Unit = { + name: 'ton_short', + symbol: 'ton', + ratio: pound.ratio * 2000, + type +} + +export type MassUnitKey = + | 'lb' + | 't' + | 'ton' + | 'longton' + | 'cwt' + | 'longcwt' + | 'qr' + | 'st' + | 'oz' + | 'dr' + | 'gr' + | 'g' + | 'Yg' + | 'Zg' + | 'Eg' + | 'Pg' + | 'Tg' + | 'Gg' + | 'Mg' + | 'kg' + | 'hg' + | 'dag' + | 'dg' + | 'cg' + | 'mg' + | 'μg' + | 'ng' + | 'pg' + | 'fg' + | 'ag' + | 'zg' + | 'yg' diff --git a/src/units/volume.ts b/src/units/volume.ts new file mode 100644 index 0000000..989497b --- /dev/null +++ b/src/units/volume.ts @@ -0,0 +1,143 @@ +import Unit, { MeasureType } from './Unit' +import { metre, inch } from './length' + +const type: MeasureType = 'volume' + +/** + * Litre is not SI unit (cubic meters are SI unit for volume). + * Can use SI prefixes. + */ +export const litre: Unit = { + name: 'litre', + symbol: 'l', + alternativeSymbol: 'ℓ', + ratio: metre.ratio * 0.001, + isPrefixable: true, + type +} + +/** + * U.S. Gallon. (U.K. gallon is deprecated?) + * 3.785411784 litres + */ +export const gallon: Unit = { + name: 'gallon', + symbol: 'gal', + ratio: Math.pow(inch.ratio, 3) * 231, + type +} + +export const quart: Unit = { + name: 'quart', + symbol: 'qt', + ratio: gallon.ratio / 4, + type +} + +/** + * Imperial (U.S.) Pint is 1/8 gallon + */ +export const pint: Unit = { + name: 'pint_short', + symbol: 'pt', + ratio: gallon.ratio / 8, + type +} + +/** + * Imperial (U.K.) Pint is 568ml + */ +export const pint_long: Unit = { + name: 'pint_long', + symbol: 'pint', + ratio: litre.ratio * 0.568, + type +} + +export const gill: Unit = { + name: 'gill', + symbol: 'gi', + ratio: gallon.ratio / 32, + type +} + +export const fluid_ounce: Unit = { + name: 'fluid_ounce', + symbol: 'fl oz', + ratio: gallon.ratio / 128, + type +} + +export const peck: Unit = { + name: 'peck', + symbol: 'pk', + ratio: litre.ratio * 8.809_768, + type +} + +export const bushel: Unit = { + name: 'bushel', + symbol: 'bu', + ratio: litre.ratio * 35.239_070_166_88, + type +} + +export type VolumeUnitKey = + | 'gal' + | 'qt' + | 'pt' + | 'pint' + | 'gi' + | 'fl oz' + | 'pk' + | 'bu' + | 'yd3' + | 'ft3' + | 'in3' + | 'ch3' + | 'fur3' + | 'mi3' + | 'lea3' + | 'M3' + | 'm3' + | 'Ym3' + | 'Zm3' + | 'Em3' + | 'Pm3' + | 'Tm3' + | 'Gm3' + | 'Mm3' + | 'km3' + | 'hm3' + | 'dam3' + | 'dm3' + | 'cm3' + | 'mm3' + | 'μm3' + | 'nm3' + | 'pm3' + | 'fm3' + | 'am3' + | 'zm3' + | 'ym3' + | 'l' + | 'Yl' + | 'Zl' + | 'El' + | 'Pl' + | 'Tl' + | 'Gl' + | 'Ml' + | 'kl' + | 'hl' + | 'dal' + | 'dl' + | 'cl' + | 'ml' + | 'μl' + | 'nl' + | 'pl' + | 'fl' + | 'al' + | 'zl' + | 'yl'