diff --git a/src/common/Converter.ts b/src/common/Converter.ts deleted file mode 100644 index d94e4bb..0000000 --- a/src/common/Converter.ts +++ /dev/null @@ -1 +0,0 @@ -export type Converter = (...args: any) => T; diff --git a/src/common/ConvertibleLengthUnit.ts b/src/common/ConvertibleLengthUnit.ts deleted file mode 100644 index 5cfa960..0000000 --- a/src/common/ConvertibleLengthUnit.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Converter } from 'common/Converter'; -import { Centimeter } from 'units/Centimeter'; -import { Pixel } from 'units/Pixel'; -import { Rem } from 'units/Rem'; -import { VH } from 'units/VH'; -import { VMax } from 'units/VMax'; -import { VMin } from 'units/VMin'; -import { VW } from 'units/VW'; - -export interface ConvertibleLengthUnit { - toCentimeter: Converter; - toPixel: Converter; - toRem: Converter; - toVW: Converter; - toVH: Converter; - toVMin: Converter; - toVMax: Converter; -} diff --git a/src/common/ConvertibleMultiplicationUnit.ts b/src/common/ConvertibleMultiplicationUnit.ts deleted file mode 100644 index f627fd2..0000000 --- a/src/common/ConvertibleMultiplicationUnit.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Converter } from 'common/Converter'; -import { Magnification } from 'units/Magnification'; -import { Percent } from 'units/Percent'; - -export interface ConvertibleMultiplicationUnit { - toMagnification: Converter; - toPercent: Converter; -} diff --git a/src/common/Unit.ts b/src/common/Unit.ts deleted file mode 100644 index e2c5ce1..0000000 --- a/src/common/Unit.ts +++ /dev/null @@ -1,7 +0,0 @@ -export abstract class Unit { - constructor( - public readonly value: number, - ) {} - - abstract toString(): string; -} diff --git a/src/common/helpers.ts b/src/common/helpers.ts deleted file mode 100644 index b1e56f9..0000000 --- a/src/common/helpers.ts +++ /dev/null @@ -1,59 +0,0 @@ -export const findRootFontSize = (): number|null => { - if (typeof document !== 'undefined') { - return parseFloat(getComputedStyle(document.documentElement).fontSize); - } - return null; -}; - -export const tryFindRootFontSize = (defaultSize?: number): number => { - const rootFontSize = findRootFontSize(); - if (typeof rootFontSize === 'number') { - return rootFontSize; - } - - if (typeof defaultSize === 'number') { - return defaultSize; - } - - throw new Error('The font size of the root element could not be obtained. Run in a browser or specify an default size.'); -}; - -export const findViewHeight = (): number|null => { - if (typeof window !== 'undefined') { - return window.parent.screen.height; - } - return null; -}; - -export const tryFindViewHeight = (defaultHeight?: number): number => { - const height = findViewHeight(); - if (typeof height === 'number') { - return height; - } - - if (typeof defaultHeight === 'number') { - return defaultHeight; - } - - throw new Error('The view height could not be obtained. Run it in a browser or specify a default size.'); -}; - -export const findViewWidth = (): number|null => { - if (typeof window !== 'undefined') { - return window.parent.screen.width; - } - return null; -}; - -export const tryFindViewWidth = (defaultWidth?: number): number => { - const width = findViewWidth(); - if (typeof width === 'number') { - return width; - } - - if (typeof defaultWidth === 'number') { - return defaultWidth; - } - - throw new Error('The view width could not be obtained. Run it in a browser or specify a default size.'); -}; diff --git a/src/defs.ts b/src/defs.ts new file mode 100644 index 0000000..aebef16 --- /dev/null +++ b/src/defs.ts @@ -0,0 +1,75 @@ +const AbsoluteLengthUnitSuffix = { + Pixel: 'px', + Centimeter: 'cm', + Millimeter: 'mm', + Quarter: 'Q', + Inch: 'in', + Pica: 'pc', + Point: 'pt', +} as const; +export type AbsoluteLengthUnitSuffix = typeof AbsoluteLengthUnitSuffix[keyof typeof AbsoluteLengthUnitSuffix]; + +const RelativeLengthUnitSuffix = { + REM: 'rem', + EM: 'em', + ViewWidth: 'vw', + ViewHeight: 'vh', + ViewMin: 'vmin', + ViewMax: 'vmax', +} as const; +export type RelativeLengthUnitSuffix = typeof RelativeLengthUnitSuffix[keyof typeof RelativeLengthUnitSuffix]; + +const MultiplicationUnitSuffix = { + Percent: '%', + Magnification: '', +} as const; +export type MultiplicationUnitSuffix = typeof MultiplicationUnitSuffix[keyof typeof MultiplicationUnitSuffix]; + +const LengthUnitSuffix = { + ...AbsoluteLengthUnitSuffix, + ...RelativeLengthUnitSuffix, +}; +export type LengthUnitSuffix = typeof LengthUnitSuffix[keyof typeof LengthUnitSuffix]; + +export const UnitSuffix = { + ...LengthUnitSuffix, + ...MultiplicationUnitSuffix, +}; +export type UnitSuffix = typeof UnitSuffix[keyof typeof UnitSuffix]; + +export type Unit = `${number}${Suffix}`; + +export type ConvertLengthUnitArgsOptions = { + rem?: Element | Unit<'px'>; + em?: Element | Unit<'px'>; + viewWidth?: Unit<'px'>; + viewHeight?: Unit<'px'>; +}; + +export type ConvertLengthUnitArgs< + FromUnitValue extends Unit, + ToUnitSuffix extends LengthUnitSuffix, + > = [ + fromUnitValue: FromUnitValue, + toUnitSuffix: ToUnitSuffix, + options?: ConvertLengthUnitArgsOptions, +]; + +export type ConvertMultiplicationUnitArgs< + FromUnitValue extends Unit, + ToUnitSuffix extends MultiplicationUnitSuffix, + > = [ + fromUnitValue: FromUnitValue, + toUnitSuffix: ToUnitSuffix, +]; + +export type ConvertArgs = + FromUnitValue extends Unit + ? ToUnitSuffix extends LengthUnitSuffix + ? ConvertLengthUnitArgs + : never + : FromUnitValue extends Unit + ? ToUnitSuffix extends MultiplicationUnitSuffix + ? ConvertMultiplicationUnitArgs + : never + : never diff --git a/src/index.ts b/src/index.ts index a5754e9..0052fbd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,18 +1,71 @@ -export { - findRootFontSize, - tryFindRootFontSize, - findViewHeight, - tryFindViewHeight, - findViewWidth, - tryFindViewWidth, -} from 'common/helpers'; -export { Unit } from 'common/Unit'; -export { Centimeter } from 'units/Centimeter'; -export { Magnification } from 'units/Magnification'; -export { Percent } from 'units/Percent'; -export { Pixel } from 'units/Pixel'; -export { Rem } from 'units/Rem'; -export { VH } from 'units/VH'; -export { VMax } from 'units/VMax'; -export { VMin } from 'units/VMin'; -export { VW } from 'units/VW'; +import { ConvertArgs, Unit, UnitSuffix } from 'defs'; +import { + getFontSizePixelValue, + getViewWidthPixelValue, + getViewHeightPixelValue, + converterMap, +} from 'internal'; + +export * from 'defs'; + +const UNIT_SPLIT_REGEXP = /^((?:[1-9]\d*|0)(?:\.\d+)?)(px|cm|mm|Q|in|pc|pt|rem|em|vw|vh|vmin|vmax|%|)$/; +export const splitUnitValue = (value: Unit): { value: number, unitSuffix: UnitSuffix } => { + const split = value.match(UNIT_SPLIT_REGEXP); + if (split === null || split.length < 3) { + throw new TypeError('Invalid value for value argument.'); + } + return { + value: parseFloat(split[1]), + unitSuffix: split[2] as UnitSuffix, + }; +}; + +export const convertUnits = < + FromUnitValue extends Unit, + ToUnitSuffix extends UnitSuffix, +>( + ...[ + fromUnitValue, + toUnitSuffix, + options, + ]: ConvertArgs +): Unit => { + const { value: fromValue, unitSuffix: fromUnitSuffix } = splitUnitValue(fromUnitValue) ?? {}; + + const converters = converterMap[fromUnitSuffix]; + if (converters === undefined) { + throw new TypeError('Invalid value for fromUnitValue argument.'); + } + + const converter = converters[toUnitSuffix]; + if (converter === undefined) { + throw new TypeError('Invalid value for toUnitSuffix argument'); + } + + const remPixel = typeof options?.rem === 'string' ? splitUnitValue(options?.rem) : undefined; + const remElement = options?.rem instanceof Element ? options?.rem : document.documentElement; + const remPixelValue = remPixel ? remPixel.value : getFontSizePixelValue(remElement); + if (!remPixelValue) { + throw new TypeError('Failed to get the font size of the root element. Please run it in the browser environment or specify the default size.'); + } + + const emPixel = typeof options?.em === 'string' ? splitUnitValue(options?.em) : undefined; + const emElement = options?.em instanceof Element ? options?.em : document.documentElement; + const emPixelValue = emPixel ? emPixel.value : getFontSizePixelValue(emElement); + if (!emPixelValue) { + throw new TypeError('Failed to get the font size of the element. Please run it in the browser environment or specify the default size.'); + } + + const viewWidthPixelValue = options?.viewWidth ?? getViewWidthPixelValue(); + if (!viewWidthPixelValue) { + throw new TypeError('Failed to get the screen width size. Please run it in the browser environment or specify the default size.'); + } + + const viewHeightPixelValue = options?.viewHeight ?? getViewHeightPixelValue(); + if (!viewHeightPixelValue) { + throw new TypeError('Failed to get the screen height size. Please run it in the browser environment or specify the default size.'); + } + + const toUnitValue = converter(fromValue, { remPixelValue, emPixelValue, viewWidthPixelValue, viewHeightPixelValue }); + return `${toUnitValue}${toUnitSuffix}` as Unit; +}; diff --git a/src/internal/constants.ts b/src/internal/constants.ts new file mode 100644 index 0000000..356a4e4 --- /dev/null +++ b/src/internal/constants.ts @@ -0,0 +1,6 @@ +export const INCH_PIXEL_VALUE = 96; +export const CENTIMETER_PIXEL_VALUE = INCH_PIXEL_VALUE / 2.54; +export const MILLIMETER_PIXEL_VALUE = CENTIMETER_PIXEL_VALUE / 10; +export const QUARTER_PIXEL_VALUE = CENTIMETER_PIXEL_VALUE / 40; +export const PICA_PIXEL_VALUE = INCH_PIXEL_VALUE / 6; +export const POINT_PIXEL_VALUE = PICA_PIXEL_VALUE / 12; diff --git a/src/internal/converters/centimeter.ts b/src/internal/converters/centimeter.ts new file mode 100644 index 0000000..d491ab1 --- /dev/null +++ b/src/internal/converters/centimeter.ts @@ -0,0 +1,68 @@ +import { UnitSuffix } from 'defs'; +import { CENTIMETER_PIXEL_VALUE } from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const centimeterToPixel = (value: number): number => { + return value * CENTIMETER_PIXEL_VALUE; +}; + +const centimeterToMillimeter = (value: number): number => { + return pixel.mm(centimeterToPixel(value)); +}; + +const centimeterToQuarter = (value: number): number => { + return pixel.Q(centimeterToPixel(value)); +}; + +const centimeterToInch = (value: number): number => { + return pixel.in(centimeterToPixel(value)); +}; + +const centimeterToPica = (value: number): number => { + return pixel.pc(centimeterToPixel(value)); +}; + +const centimeterToPoint = (value: number): number => { + return pixel.pt(centimeterToPixel(value)); +}; + +const centimeterToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(centimeterToPixel(value), options); +}; + +const centimeterToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(centimeterToPixel(value), options); +}; + +const centimeterToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(centimeterToPixel(value), options); +}; + +const centimeterToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(centimeterToPixel(value), options); +}; + +const centimeterToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(centimeterToPixel(value), options); +}; + +const centimeterToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(centimeterToPixel(value), options); +}; + +export const centimeter = { + [UnitSuffix.Pixel]: centimeterToPixel, + [UnitSuffix.Centimeter]: noConvert, + [UnitSuffix.Millimeter]: centimeterToMillimeter, + [UnitSuffix.Quarter]: centimeterToQuarter, + [UnitSuffix.Inch]: centimeterToInch, + [UnitSuffix.Pica]: centimeterToPica, + [UnitSuffix.Point]: centimeterToPoint, + [UnitSuffix.REM]: centimeterToREM, + [UnitSuffix.EM]: centimeterToEM, + [UnitSuffix.ViewWidth]: centimeterToViewWidth, + [UnitSuffix.ViewHeight]: centimeterToViewHeight, + [UnitSuffix.ViewMin]: centimeterToViewMin, + [UnitSuffix.ViewMax]: centimeterToViewMax, +}; diff --git a/src/internal/converters/em.ts b/src/internal/converters/em.ts new file mode 100644 index 0000000..e02f983 --- /dev/null +++ b/src/internal/converters/em.ts @@ -0,0 +1,67 @@ +import { UnitSuffix } from 'defs'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const emToPixel = (value: number, options: UnitConverterOptions): number => { + return value * options.emPixelValue; +}; + +const emToCentimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.cm(emToPixel(value, options)); +}; + +const emToMillimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.mm(emToPixel(value, options)); +}; + +const emToQuarter = (value: number, options: UnitConverterOptions): number => { + return pixel.Q(emToPixel(value, options)); +}; + +const emToInch = (value: number, options: UnitConverterOptions): number => { + return pixel.in(emToPixel(value, options)); +}; + +const emToPica = (value: number, options: UnitConverterOptions): number => { + return pixel.pc(emToPixel(value, options)); +}; + +const emToPoint = (value: number, options: UnitConverterOptions): number => { + return pixel.pt(emToPixel(value, options)); +}; + +const emToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(emToPixel(value, options), options); +}; + +const emToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(emToPixel(value, options), options); +}; + +const emToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(emToPixel(value, options), options); +}; + +const emToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(emToPixel(value, options), options); +}; + +const emToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(emToPixel(value, options), options); +}; + +export const em = { + [UnitSuffix.Pixel]: emToPixel, + [UnitSuffix.Centimeter]: emToCentimeter, + [UnitSuffix.Millimeter]: emToMillimeter, + [UnitSuffix.Quarter]: emToQuarter, + [UnitSuffix.Inch]: emToInch, + [UnitSuffix.Pica]: emToPica, + [UnitSuffix.Point]: emToPoint, + [UnitSuffix.REM]: emToREM, + [UnitSuffix.EM]: noConvert, + [UnitSuffix.ViewWidth]: emToViewWidth, + [UnitSuffix.ViewHeight]: emToViewHeight, + [UnitSuffix.ViewMin]: emToViewMin, + [UnitSuffix.ViewMax]: emToViewMax, +}; diff --git a/src/internal/converters/inch.ts b/src/internal/converters/inch.ts new file mode 100644 index 0000000..08a27ed --- /dev/null +++ b/src/internal/converters/inch.ts @@ -0,0 +1,68 @@ +import { UnitSuffix } from 'defs'; +import { INCH_PIXEL_VALUE } from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const inchToPixel = (value: number): number => { + return value * INCH_PIXEL_VALUE; +}; + +const inchToCentimeter = (value: number): number => { + return pixel.cm(inchToPixel(value)); +}; + +const inchToMillimeter = (value: number): number => { + return pixel.mm(inchToPixel(value)); +}; + +const inchToQuarter = (value: number): number => { + return pixel.Q(inchToPixel(value)); +}; + +const inchToPica = (value: number): number => { + return pixel.pc(inchToPixel(value)); +}; + +const inchToPoint = (value: number): number => { + return pixel.pt(inchToPixel(value)); +}; + +const inchToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(inchToPixel(value), options); +}; + +const inchToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(inchToPixel(value), options); +}; + +const inchToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(inchToPixel(value), options); +}; + +const inchToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(inchToPixel(value), options); +}; + +const inchToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(inchToPixel(value), options); +}; + +const inchToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(inchToPixel(value), options); +}; + +export const inch = { + [UnitSuffix.Pixel]: inchToPixel, + [UnitSuffix.Centimeter]: inchToCentimeter, + [UnitSuffix.Millimeter]: inchToMillimeter, + [UnitSuffix.Quarter]: inchToQuarter, + [UnitSuffix.Inch]: noConvert, + [UnitSuffix.Pica]: inchToPica, + [UnitSuffix.Point]: inchToPoint, + [UnitSuffix.REM]: inchToREM, + [UnitSuffix.EM]: inchToEM, + [UnitSuffix.ViewWidth]: inchToViewWidth, + [UnitSuffix.ViewHeight]: inchToViewHeight, + [UnitSuffix.ViewMin]: inchToViewMin, + [UnitSuffix.ViewMax]: inchToViewMax, +}; diff --git a/src/internal/converters/index.ts b/src/internal/converters/index.ts new file mode 100644 index 0000000..06212bf --- /dev/null +++ b/src/internal/converters/index.ts @@ -0,0 +1,45 @@ +import { Unit, UnitSuffix } from 'defs'; +import { centimeter } from 'internal/converters/centimeter'; +import { em } from 'internal/converters/em'; +import { inch } from 'internal/converters/inch'; +import { magnification } from 'internal/converters/magnification'; +import { millimeter } from 'internal/converters/millimeter'; +import { percent } from 'internal/converters/percent'; +import { pica } from 'internal/converters/pica'; +import { pixel } from 'internal/converters/pixel'; +import { point } from 'internal/converters/point'; +import { quarter } from 'internal/converters/quarter'; +import { rem } from 'internal/converters/rem'; +import { viewHeight } from 'internal/converters/view-height'; +import { viewMax } from 'internal/converters/view-max'; +import { viewMin } from 'internal/converters/view-min'; +import { viewWidth } from 'internal/converters/view-width'; + +export type UnitConverterOptions = { + remPixelValue: number; + emPixelValue: number; + viewWidthPixelValue: number; + viewHeightPixelValue: number; +}; + +export const noConvert = (unitValue: Unit): Unit => { + return unitValue; +}; + +export const converterMap: { [key: string]: { [key: string]: Function | undefined } | undefined } = { + [UnitSuffix.Pixel]: pixel, + [UnitSuffix.Centimeter]: centimeter, + [UnitSuffix.Millimeter]: millimeter, + [UnitSuffix.Quarter]: quarter, + [UnitSuffix.Inch]: inch, + [UnitSuffix.Pica]: pica, + [UnitSuffix.Point]: point, + [UnitSuffix.REM]: rem, + [UnitSuffix.EM]: em, + [UnitSuffix.ViewWidth]: viewHeight, + [UnitSuffix.ViewHeight]: viewWidth, + [UnitSuffix.ViewMin]: viewMax, + [UnitSuffix.ViewMax]: viewMin, + [UnitSuffix.Magnification]: magnification, + [UnitSuffix.Percent]: percent, +}; diff --git a/src/internal/converters/magnification.ts b/src/internal/converters/magnification.ts new file mode 100644 index 0000000..6f168d5 --- /dev/null +++ b/src/internal/converters/magnification.ts @@ -0,0 +1,11 @@ +import { UnitSuffix } from 'defs'; +import { noConvert } from 'internal/converters'; + +const magnificationToPercent = (value: number): number => { + return value * 100; +}; + +export const magnification = { + [UnitSuffix.Percent]: magnificationToPercent, + [UnitSuffix.Magnification]: noConvert, +}; diff --git a/src/internal/converters/millimeter.ts b/src/internal/converters/millimeter.ts new file mode 100644 index 0000000..d1fc6fe --- /dev/null +++ b/src/internal/converters/millimeter.ts @@ -0,0 +1,68 @@ +import { UnitSuffix } from 'defs'; +import { MILLIMETER_PIXEL_VALUE } from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const millimeterToPixel = (value: number): number => { + return value * MILLIMETER_PIXEL_VALUE; +}; + +const millimeterToCentimeter = (value: number): number => { + return pixel.cm(millimeterToPixel(value)); +}; + +const millimeterToQuarter = (value: number): number => { + return pixel.Q(millimeterToPixel(value)); +}; + +const millimeterToInch = (value: number): number => { + return pixel.in(millimeterToPixel(value)); +}; + +const millimeterToPica = (value: number): number => { + return pixel.pc(millimeterToPixel(value)); +}; + +const millimeterToPoint = (value: number): number => { + return pixel.pt(millimeterToPixel(value)); +}; + +const millimeterToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(millimeterToPixel(value), options); +}; + +const millimeterToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(millimeterToPixel(value), options); +}; + +const millimeterToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(millimeterToPixel(value), options); +}; + +const millimeterToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(millimeterToPixel(value), options); +}; + +const millimeterToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(millimeterToPixel(value), options); +}; + +const millimeterToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(millimeterToPixel(value), options); +}; + +export const millimeter = { + [UnitSuffix.Pixel]: millimeterToPixel, + [UnitSuffix.Centimeter]: millimeterToCentimeter, + [UnitSuffix.Millimeter]: noConvert, + [UnitSuffix.Quarter]: millimeterToQuarter, + [UnitSuffix.Inch]: millimeterToInch, + [UnitSuffix.Pica]: millimeterToPica, + [UnitSuffix.Point]: millimeterToPoint, + [UnitSuffix.REM]: millimeterToREM, + [UnitSuffix.EM]: millimeterToEM, + [UnitSuffix.ViewWidth]: millimeterToViewWidth, + [UnitSuffix.ViewHeight]: millimeterToViewHeight, + [UnitSuffix.ViewMin]: millimeterToViewMin, + [UnitSuffix.ViewMax]: millimeterToViewMax, +}; diff --git a/src/internal/converters/percent.ts b/src/internal/converters/percent.ts new file mode 100644 index 0000000..9a1449d --- /dev/null +++ b/src/internal/converters/percent.ts @@ -0,0 +1,11 @@ +import { UnitSuffix } from 'defs'; +import { noConvert } from 'internal/converters'; + +const percentToMagnification = (value: number): number => { + return value / 100; +}; + +export const percent = { + [UnitSuffix.Percent]: noConvert, + [UnitSuffix.Magnification]: percentToMagnification, +}; diff --git a/src/internal/converters/pica.ts b/src/internal/converters/pica.ts new file mode 100644 index 0000000..42ab3ea --- /dev/null +++ b/src/internal/converters/pica.ts @@ -0,0 +1,68 @@ +import { UnitSuffix } from 'defs'; +import { PICA_PIXEL_VALUE } from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const picaToPixel = (value: number): number => { + return value * PICA_PIXEL_VALUE; +}; + +const picaToCentimeter = (value: number): number => { + return pixel.cm(picaToPixel(value)); +}; + +const picaToMillimeter = (value: number): number => { + return pixel.mm(picaToPixel(value)); +}; + +const picaToQuarter = (value: number): number => { + return pixel.Q(picaToPixel(value)); +}; + +const picaToInch = (value: number): number => { + return pixel.in(picaToPixel(value)); +}; + +const picaToPoint = (value: number): number => { + return pixel.pt(picaToPixel(value)); +}; + +const picaToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(picaToPixel(value), options); +}; + +const picaToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(picaToPixel(value), options); +}; + +const picaToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(picaToPixel(value), options); +}; + +const picaToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(picaToPixel(value), options); +}; + +const picaToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(picaToPixel(value), options); +}; + +const picaToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(picaToPixel(value), options); +}; + +export const pica = { + [UnitSuffix.Pixel]: picaToPixel, + [UnitSuffix.Centimeter]: picaToCentimeter, + [UnitSuffix.Millimeter]: picaToMillimeter, + [UnitSuffix.Quarter]: picaToQuarter, + [UnitSuffix.Inch]: picaToInch, + [UnitSuffix.Pica]: noConvert, + [UnitSuffix.Point]: picaToPoint, + [UnitSuffix.REM]: picaToREM, + [UnitSuffix.EM]: picaToEM, + [UnitSuffix.ViewWidth]: picaToViewWidth, + [UnitSuffix.ViewHeight]: picaToViewHeight, + [UnitSuffix.ViewMin]: picaToViewMin, + [UnitSuffix.ViewMax]: picaToViewMax, +}; diff --git a/src/internal/converters/pixel.ts b/src/internal/converters/pixel.ts new file mode 100644 index 0000000..330175d --- /dev/null +++ b/src/internal/converters/pixel.ts @@ -0,0 +1,74 @@ +import { UnitSuffix } from 'defs'; +import { + CENTIMETER_PIXEL_VALUE, + INCH_PIXEL_VALUE, + MILLIMETER_PIXEL_VALUE, + PICA_PIXEL_VALUE, + POINT_PIXEL_VALUE, + QUARTER_PIXEL_VALUE +} from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; + +const pixelToCentimeter = (value: number): number => { + return value / CENTIMETER_PIXEL_VALUE; +}; + +const pixelToMillimeter = (value: number): number => { + return value / MILLIMETER_PIXEL_VALUE; +}; + +const pixelToQuarter = (value: number): number => { + return value / QUARTER_PIXEL_VALUE; +}; + +const pixelToInch = (value: number): number => { + return value / INCH_PIXEL_VALUE; +}; + +const pixelToPica = (value: number): number => { + return value / PICA_PIXEL_VALUE; +}; + +const pixelToPoint = (value: number): number => { + return value / POINT_PIXEL_VALUE; +}; + +const pixelToREM = (value: number, options: UnitConverterOptions): number => { + return value / options.remPixelValue; +}; + +const pixelToEM = (value: number, options: UnitConverterOptions): number => { + return value / options.emPixelValue; +}; + +const pixelToViewWidth = (value: number, options: UnitConverterOptions): number => { + return value / options.viewWidthPixelValue * 100; +}; + +const pixelToViewHeight = (value: number, options: UnitConverterOptions): number => { + return value / options.viewHeightPixelValue * 100; +}; + +const pixelToViewMin = (value: number, options: UnitConverterOptions): number => { + return value / Math.min(options.viewWidthPixelValue, options.viewHeightPixelValue) * 100; +}; + +const pixelToViewMax = (value: number, options: UnitConverterOptions): number => { + return value / Math.max(options.viewWidthPixelValue, options.viewHeightPixelValue) * 100; +}; + +export const pixel = { + [UnitSuffix.Pixel]: noConvert, + [UnitSuffix.Centimeter]: pixelToCentimeter, + [UnitSuffix.Millimeter]: pixelToMillimeter, + [UnitSuffix.Quarter]: pixelToQuarter, + [UnitSuffix.Inch]: pixelToInch, + [UnitSuffix.Pica]: pixelToPica, + [UnitSuffix.Point]: pixelToPoint, + [UnitSuffix.REM]: pixelToREM, + [UnitSuffix.EM]: pixelToEM, + [UnitSuffix.ViewWidth]: pixelToViewWidth, + [UnitSuffix.ViewHeight]: pixelToViewHeight, + [UnitSuffix.ViewMin]: pixelToViewMin, + [UnitSuffix.ViewMax]: pixelToViewMax, +}; diff --git a/src/internal/converters/point.ts b/src/internal/converters/point.ts new file mode 100644 index 0000000..1a077e8 --- /dev/null +++ b/src/internal/converters/point.ts @@ -0,0 +1,68 @@ +import { UnitSuffix } from 'defs'; +import { POINT_PIXEL_VALUE } from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const pointToPixel = (value: number): number => { + return value * POINT_PIXEL_VALUE; +}; + +const pointToCentimeter = (value: number): number => { + return pixel.cm(pointToPixel(value)); +}; + +const pointToMillimeter = (value: number): number => { + return pixel.mm(pointToPixel(value)); +}; + +const pointToQuarter = (value: number): number => { + return pixel.Q(pointToPixel(value)); +}; + +const pointToInch = (value: number): number => { + return pixel.in(pointToPixel(value)); +}; + +const pointToPica = (value: number): number => { + return pixel.pc(pointToPixel(value)); +}; + +const pointToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(pointToPixel(value), options); +}; + +const pointToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(pointToPixel(value), options); +}; + +const pointToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(pointToPixel(value), options); +}; + +const pointToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(pointToPixel(value), options); +}; + +const pointToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(pointToPixel(value), options); +}; + +const pointToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(pointToPixel(value), options); +}; + +export const point = { + [UnitSuffix.Pixel]: pointToPixel, + [UnitSuffix.Centimeter]: pointToCentimeter, + [UnitSuffix.Millimeter]: pointToMillimeter, + [UnitSuffix.Quarter]: pointToQuarter, + [UnitSuffix.Inch]: pointToInch, + [UnitSuffix.Pica]: pointToPica, + [UnitSuffix.Point]: noConvert, + [UnitSuffix.REM]: pointToREM, + [UnitSuffix.EM]: pointToEM, + [UnitSuffix.ViewWidth]: pointToViewWidth, + [UnitSuffix.ViewHeight]: pointToViewHeight, + [UnitSuffix.ViewMin]: pointToViewMin, + [UnitSuffix.ViewMax]: pointToViewMax, +}; diff --git a/src/internal/converters/quarter.ts b/src/internal/converters/quarter.ts new file mode 100644 index 0000000..60d9eda --- /dev/null +++ b/src/internal/converters/quarter.ts @@ -0,0 +1,68 @@ +import { UnitSuffix } from 'defs'; +import { QUARTER_PIXEL_VALUE } from 'internal/constants'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const quarterToPixel = (value: number): number => { + return value * QUARTER_PIXEL_VALUE; +}; + +const quarterToCentimeter = (value: number): number => { + return pixel.cm(quarterToPixel(value)); +}; + +const quarterToMillimeter = (value: number): number => { + return pixel.mm(quarterToPixel(value)); +}; + +const quarterToInch = (value: number): number => { + return pixel.in(quarterToPixel(value)); +}; + +const quarterToPica = (value: number): number => { + return pixel.pc(quarterToPixel(value)); +}; + +const quarterToPoint = (value: number): number => { + return pixel.pt(quarterToPixel(value)); +}; + +const quarterToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(quarterToPixel(value), options); +}; + +const quarterToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(quarterToPixel(value), options); +}; + +const quarterToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(quarterToPixel(value), options); +}; + +const quarterToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(quarterToPixel(value), options); +}; + +const quarterToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(quarterToPixel(value), options); +}; + +const quarterToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(quarterToPixel(value), options); +}; + +export const quarter = { + [UnitSuffix.Pixel]: quarterToPixel, + [UnitSuffix.Centimeter]: quarterToCentimeter, + [UnitSuffix.Millimeter]: quarterToMillimeter, + [UnitSuffix.Quarter]: noConvert, + [UnitSuffix.Inch]: quarterToInch, + [UnitSuffix.Pica]: quarterToPica, + [UnitSuffix.Point]: quarterToPoint, + [UnitSuffix.REM]: quarterToREM, + [UnitSuffix.EM]: quarterToEM, + [UnitSuffix.ViewWidth]: quarterToViewWidth, + [UnitSuffix.ViewHeight]: quarterToViewHeight, + [UnitSuffix.ViewMin]: quarterToViewMin, + [UnitSuffix.ViewMax]: quarterToViewMax, +}; diff --git a/src/internal/converters/rem.ts b/src/internal/converters/rem.ts new file mode 100644 index 0000000..b9c82fd --- /dev/null +++ b/src/internal/converters/rem.ts @@ -0,0 +1,67 @@ +import { UnitSuffix } from 'defs'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const remToPixel = (value: number, options: UnitConverterOptions): number => { + return value * options.remPixelValue; +}; + +const remToCentimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.cm(remToPixel(value, options)); +}; + +const remToMillimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.mm(remToPixel(value, options)); +}; + +const remToQuarter = (value: number, options: UnitConverterOptions): number => { + return pixel.Q(remToPixel(value, options)); +}; + +const remToInch = (value: number, options: UnitConverterOptions): number => { + return pixel.in(remToPixel(value, options)); +}; + +const remToPica = (value: number, options: UnitConverterOptions): number => { + return pixel.pc(remToPixel(value, options)); +}; + +const remToPoint = (value: number, options: UnitConverterOptions): number => { + return pixel.pt(remToPixel(value, options)); +}; + +const remToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(remToPixel(value, options), options); +}; + +const remToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(remToPixel(value, options), options); +}; + +const remToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(remToPixel(value, options), options); +}; + +const remToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(remToPixel(value, options), options); +}; + +const remToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(remToPixel(value, options), options); +}; + +export const rem = { + [UnitSuffix.Pixel]: remToPixel, + [UnitSuffix.Centimeter]: remToCentimeter, + [UnitSuffix.Millimeter]: remToMillimeter, + [UnitSuffix.Quarter]: remToQuarter, + [UnitSuffix.Inch]: remToInch, + [UnitSuffix.Pica]: remToPica, + [UnitSuffix.Point]: remToPoint, + [UnitSuffix.REM]: noConvert, + [UnitSuffix.EM]: remToEM, + [UnitSuffix.ViewWidth]: remToViewWidth, + [UnitSuffix.ViewHeight]: remToViewHeight, + [UnitSuffix.ViewMin]: remToViewMin, + [UnitSuffix.ViewMax]: remToViewMax, +}; diff --git a/src/internal/converters/view-height.ts b/src/internal/converters/view-height.ts new file mode 100644 index 0000000..c9a5f8c --- /dev/null +++ b/src/internal/converters/view-height.ts @@ -0,0 +1,67 @@ +import { UnitSuffix } from 'defs'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const viewHeightToPixel = (value: number, options: UnitConverterOptions): number => { + return value * options.viewHeightPixelValue; +}; + +const viewHeightToCentimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.cm(viewHeightToPixel(value, options)); +}; + +const viewHeightToMillimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.mm(viewHeightToPixel(value, options)); +}; + +const viewHeightToQuarter = (value: number, options: UnitConverterOptions): number => { + return pixel.Q(viewHeightToPixel(value, options)); +}; + +const viewHeightToInch = (value: number, options: UnitConverterOptions): number => { + return pixel.in(viewHeightToPixel(value, options)); +}; + +const viewHeightToPica = (value: number, options: UnitConverterOptions): number => { + return pixel.pc(viewHeightToPixel(value, options)); +}; + +const viewHeightToPoint = (value: number, options: UnitConverterOptions): number => { + return pixel.pt(viewHeightToPixel(value, options)); +}; + +const viewHeightToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(viewHeightToPixel(value, options), options); +}; + +const viewHeightToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(viewHeightToPixel(value, options), options); +}; + +const viewHeightToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(viewHeightToPixel(value, options), options); +}; + +const viewHeightToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(viewHeightToPixel(value, options), options); +}; + +const viewHeightToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(viewHeightToPixel(value, options), options); +}; + +export const viewHeight = { + [UnitSuffix.Pixel]: viewHeightToPixel, + [UnitSuffix.Centimeter]: viewHeightToCentimeter, + [UnitSuffix.Millimeter]: viewHeightToMillimeter, + [UnitSuffix.Quarter]: viewHeightToQuarter, + [UnitSuffix.Inch]: viewHeightToInch, + [UnitSuffix.Pica]: viewHeightToPica, + [UnitSuffix.Point]: viewHeightToPoint, + [UnitSuffix.REM]: viewHeightToREM, + [UnitSuffix.EM]: viewHeightToEM, + [UnitSuffix.ViewWidth]: viewHeightToViewWidth, + [UnitSuffix.ViewHeight]: noConvert, + [UnitSuffix.ViewMin]: viewHeightToViewMin, + [UnitSuffix.ViewMax]: viewHeightToViewMax, +}; diff --git a/src/internal/converters/view-max.ts b/src/internal/converters/view-max.ts new file mode 100644 index 0000000..4037297 --- /dev/null +++ b/src/internal/converters/view-max.ts @@ -0,0 +1,67 @@ +import { UnitSuffix } from 'defs'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const viewMaxToPixel = (value: number, options: UnitConverterOptions): number => { + return value * Math.max(options.viewWidthPixelValue, options.viewHeightPixelValue) * 100; +}; + +const viewMaxToCentimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.cm(viewMaxToPixel(value, options)); +}; + +const viewMaxToMillimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.mm(viewMaxToPixel(value, options)); +}; + +const viewMaxToQuarter = (value: number, options: UnitConverterOptions): number => { + return pixel.Q(viewMaxToPixel(value, options)); +}; + +const viewMaxToInch = (value: number, options: UnitConverterOptions): number => { + return pixel.in(viewMaxToPixel(value, options)); +}; + +const viewMaxToPica = (value: number, options: UnitConverterOptions): number => { + return pixel.pc(viewMaxToPixel(value, options)); +}; + +const viewMaxToPoint = (value: number, options: UnitConverterOptions): number => { + return pixel.pt(viewMaxToPixel(value, options)); +}; + +const viewMaxToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(viewMaxToPixel(value, options), options); +}; + +const viewMaxToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(viewMaxToPixel(value, options), options); +}; + +const viewMaxToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(viewMaxToPixel(value, options), options); +}; + +const viewMaxToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(viewMaxToPixel(value, options), options); +}; + +const viewMaxToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(viewMaxToPixel(value, options), options); +}; + +export const viewMax = { + [UnitSuffix.Pixel]: viewMaxToPixel, + [UnitSuffix.Centimeter]: viewMaxToCentimeter, + [UnitSuffix.Millimeter]: viewMaxToMillimeter, + [UnitSuffix.Quarter]: viewMaxToQuarter, + [UnitSuffix.Inch]: viewMaxToInch, + [UnitSuffix.Pica]: viewMaxToPica, + [UnitSuffix.Point]: viewMaxToPoint, + [UnitSuffix.REM]: viewMaxToREM, + [UnitSuffix.EM]: viewMaxToEM, + [UnitSuffix.ViewWidth]: viewMaxToViewWidth, + [UnitSuffix.ViewHeight]: viewMaxToViewHeight, + [UnitSuffix.ViewMin]: viewMaxToViewMin, + [UnitSuffix.ViewMax]: noConvert, +}; diff --git a/src/internal/converters/view-min.ts b/src/internal/converters/view-min.ts new file mode 100644 index 0000000..7503bc0 --- /dev/null +++ b/src/internal/converters/view-min.ts @@ -0,0 +1,67 @@ +import { UnitSuffix } from 'defs'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const viewMinToPixel = (value: number, options: UnitConverterOptions): number => { + return value * Math.min(options.viewWidthPixelValue, options.viewHeightPixelValue) * 100; +}; + +const viewMinToCentimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.cm(viewMinToPixel(value, options)); +}; + +const viewMinToMillimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.mm(viewMinToPixel(value, options)); +}; + +const viewMinToQuarter = (value: number, options: UnitConverterOptions): number => { + return pixel.Q(viewMinToPixel(value, options)); +}; + +const viewMinToInch = (value: number, options: UnitConverterOptions): number => { + return pixel.in(viewMinToPixel(value, options)); +}; + +const viewMinToPica = (value: number, options: UnitConverterOptions): number => { + return pixel.pc(viewMinToPixel(value, options)); +}; + +const viewMinToPoint = (value: number, options: UnitConverterOptions): number => { + return pixel.pt(viewMinToPixel(value, options)); +}; + +const viewMinToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(viewMinToPixel(value, options), options); +}; + +const viewMinToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(viewMinToPixel(value, options), options); +}; + +const viewMinToViewWidth = (value: number, options: UnitConverterOptions): number => { + return pixel.vw(viewMinToPixel(value, options), options); +}; + +const viewMinToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(viewMinToPixel(value, options), options); +}; + +const viewMinToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(viewMinToPixel(value, options), options); +}; + +export const viewMin = { + [UnitSuffix.Pixel]: viewMinToPixel, + [UnitSuffix.Centimeter]: viewMinToCentimeter, + [UnitSuffix.Millimeter]: viewMinToMillimeter, + [UnitSuffix.Quarter]: viewMinToQuarter, + [UnitSuffix.Inch]: viewMinToInch, + [UnitSuffix.Pica]: viewMinToPica, + [UnitSuffix.Point]: viewMinToPoint, + [UnitSuffix.REM]: viewMinToREM, + [UnitSuffix.EM]: viewMinToEM, + [UnitSuffix.ViewWidth]: viewMinToViewWidth, + [UnitSuffix.ViewHeight]: viewMinToViewHeight, + [UnitSuffix.ViewMin]: noConvert, + [UnitSuffix.ViewMax]: viewMinToViewMax, +}; diff --git a/src/internal/converters/view-width.ts b/src/internal/converters/view-width.ts new file mode 100644 index 0000000..0c0e092 --- /dev/null +++ b/src/internal/converters/view-width.ts @@ -0,0 +1,67 @@ +import { UnitSuffix } from 'defs'; +import { UnitConverterOptions, noConvert } from 'internal/converters'; +import { pixel } from 'internal/converters/pixel'; + +const viewWidthToPixel = (value: number, options: UnitConverterOptions): number => { + return value * options.viewWidthPixelValue; +}; + +const viewWidthToCentimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.cm(viewWidthToPixel(value, options)); +}; + +const viewWidthToMillimeter = (value: number, options: UnitConverterOptions): number => { + return pixel.mm(viewWidthToPixel(value, options)); +}; + +const viewWidthToQuarter = (value: number, options: UnitConverterOptions): number => { + return pixel.Q(viewWidthToPixel(value, options)); +}; + +const viewWidthToInch = (value: number, options: UnitConverterOptions): number => { + return pixel.in(viewWidthToPixel(value, options)); +}; + +const viewWidthToPica = (value: number, options: UnitConverterOptions): number => { + return pixel.pc(viewWidthToPixel(value, options)); +}; + +const viewWidthToPoint = (value: number, options: UnitConverterOptions): number => { + return pixel.pt(viewWidthToPixel(value, options)); +}; + +const viewWidthToREM = (value: number, options: UnitConverterOptions): number => { + return pixel.rem(viewWidthToPixel(value, options), options); +}; + +const viewWidthToEM = (value: number, options: UnitConverterOptions): number => { + return pixel.em(viewWidthToPixel(value, options), options); +}; + +const viewWidthToViewHeight = (value: number, options: UnitConverterOptions): number => { + return pixel.vh(viewWidthToPixel(value, options), options); +}; + +const viewWidthToViewMin = (value: number, options: UnitConverterOptions): number => { + return pixel.vmin(viewWidthToPixel(value, options), options); +}; + +const viewWidthToViewMax = (value: number, options: UnitConverterOptions): number => { + return pixel.vmax(viewWidthToPixel(value, options), options); +}; + +export const viewWidth = { + [UnitSuffix.Pixel]: viewWidthToPixel, + [UnitSuffix.Centimeter]: viewWidthToCentimeter, + [UnitSuffix.Millimeter]: viewWidthToMillimeter, + [UnitSuffix.Quarter]: viewWidthToQuarter, + [UnitSuffix.Inch]: viewWidthToInch, + [UnitSuffix.Pica]: viewWidthToPica, + [UnitSuffix.Point]: viewWidthToPoint, + [UnitSuffix.REM]: viewWidthToREM, + [UnitSuffix.EM]: viewWidthToEM, + [UnitSuffix.ViewWidth]: noConvert, + [UnitSuffix.ViewHeight]: viewWidthToViewHeight, + [UnitSuffix.ViewMin]: viewWidthToViewMin, + [UnitSuffix.ViewMax]: viewWidthToViewMax, +}; diff --git a/src/internal/helpers.ts b/src/internal/helpers.ts new file mode 100644 index 0000000..d7ed955 --- /dev/null +++ b/src/internal/helpers.ts @@ -0,0 +1,18 @@ +export const isClient = (): boolean => { + return typeof document !== 'undefined'; +}; + +export const getFontSizePixelValue = (element: Element): number | undefined => { + if (!isClient()) return undefined; + return parseFloat(getComputedStyle(element).fontSize); +}; + +export const getViewWidthPixelValue = (): number | undefined => { + if (!isClient()) return undefined; + return window.parent.screen.width; +}; + +export const getViewHeightPixelValue = (): number | undefined => { + if (!isClient()) return undefined; + return window.parent.screen.height; +}; diff --git a/src/internal/index.ts b/src/internal/index.ts new file mode 100644 index 0000000..12971ee --- /dev/null +++ b/src/internal/index.ts @@ -0,0 +1,2 @@ +export * from 'internal/converters'; +export * from 'internal/helpers'; diff --git a/src/units/Centimeter.ts b/src/units/Centimeter.ts deleted file mode 100644 index c817b19..0000000 --- a/src/units/Centimeter.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Pixel } from 'units/Pixel'; -import { Rem } from 'units/Rem'; -import { VH } from 'units/VH'; -import { VMax } from 'units/VMax'; -import { VMin } from 'units/VMin'; -import { VW } from 'units/VW'; - -export class Centimeter extends Unit implements Omit { - public toString(): string { - return `${this.value}cm` - } - - public toPixel(): Pixel { - const pixel = this.value * (96 / 2.54); - return new Pixel(pixel); - } - - public toRem(rootFontSize: number): Rem { - const pixel = this.value * (96 / 2.54); - const rem = pixel / rootFontSize; - return new Rem(rem); - } - - public toVW(width: number): VW { - const pixel = this.value * (96 / 2.54); - const percentage = (pixel / width) * 100; - return new VW(percentage); - } - - public toVH(height: number): VH { - const pixel = this.value * (96 / 2.54); - const percentage = (pixel / height) * 100; - return new VH(percentage); - } - - public toVMin(width: number, height: number): VMin { - const minimum = Math.min(width, height); - const pixel = this.value * (96 / 2.54); - const percentage = (pixel / minimum) * 100; - return new VMin(percentage); - } - - public toVMax(width: number, height: number): VMax { - const maximum = Math.max(width, height); - const pixel = this.value * (96 / 2.54); - const percentage = (pixel / maximum) * 100; - return new VMax(percentage); - } -} diff --git a/src/units/Magnification.ts b/src/units/Magnification.ts deleted file mode 100644 index ebef89a..0000000 --- a/src/units/Magnification.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ConvertibleMultiplicationUnit } from 'common/ConvertibleMultiplicationUnit'; -import { Unit } from 'common/Unit'; -import { Percent } from 'units/Percent'; - -export class Magnification extends Unit implements Omit { - public toString(): string { - return `${this.value}`; - } - - public toPercent(): Percent { - const percent = this.value * 100; - return new Percent(percent); - } -} diff --git a/src/units/Percent.ts b/src/units/Percent.ts deleted file mode 100644 index d508251..0000000 --- a/src/units/Percent.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ConvertibleMultiplicationUnit } from 'common/ConvertibleMultiplicationUnit'; -import { Unit } from 'common/Unit'; -import { Magnification } from 'units/Magnification'; - -export class Percent extends Unit implements Omit { - public toString(): string { - return `${this.value}%` - } - - public toMagnification(): Magnification { - const magnification = this.value / 100; - return new Magnification(magnification); - } -} diff --git a/src/units/Pixel.ts b/src/units/Pixel.ts deleted file mode 100644 index 16e4c94..0000000 --- a/src/units/Pixel.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Centimeter } from 'units/Centimeter'; -import { Rem } from 'units/Rem'; -import { VH } from 'units/VH'; -import { VMax } from 'units/VMax'; -import { VMin } from 'units/VMin'; -import { VW } from 'units/VW'; - -export class Pixel extends Unit implements Omit { - public toString(): string { - return `${this.value}px` - } - - public toCentimeter(): Centimeter { - const centimeter = this.value / (96 / 2.54); - return new Centimeter(centimeter); - } - - public toRem(rootFontSize: number): Rem { - const rem = this.value / rootFontSize; - return new Rem(rem); - } - - public toVW(width: number): VW { - const percentage = (this.value / width) * 100; - return new VW(percentage); - } - - public toVH(height: number): VH { - const percentage = (this.value / height) * 100; - return new VH(percentage); - } - - public toVMin(width: number, height: number): VMin { - const minimum = Math.min(width, height); - const percentage = (this.value / minimum) * 100; - return new VMin(percentage); - } - - public toVMax(width: number, height: number): VMax { - const maximum = Math.max(width, height); - const percentage = (this.value / maximum) * 100; - return new VMax(percentage); - } -} diff --git a/src/units/Rem.ts b/src/units/Rem.ts deleted file mode 100644 index e1c2280..0000000 --- a/src/units/Rem.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Centimeter } from 'units/Centimeter'; -import { Pixel } from 'units/Pixel'; -import { VH } from 'units/VH'; -import { VMax } from 'units/VMax'; -import { VMin } from 'units/VMin'; -import { VW } from 'units/VW'; - -export class Rem extends Unit implements Omit { - public toString(): string { - return `${this.value}rem` - } - - public toCentimeter(rootFontSize: number): Centimeter { - const pixel = this.value * rootFontSize; - const centimeter = pixel / (96 / 2.54); - return new Centimeter(centimeter); - } - - public toPixel(rootFontSize: number): Pixel { - const pixel = this.value * rootFontSize; - return new Pixel(pixel); - } - - public toVW(rootFontSize: number, width: number): VW { - const pixel = this.value * rootFontSize; - const percentage = (pixel / width) * 100; - return new VW(percentage); - } - - public toVH(rootFontSize: number, height: number): VH { - const pixel = this.value * rootFontSize; - const percentage = (pixel / height) * 100; - return new VH(percentage); - } - - public toVMin(rootFontSize: number, width: number, height: number): VMin { - const pixel = this.value * rootFontSize; - const percentage = (pixel / Math.min(width, height)) * 100; - return new VMin(percentage); - } - - public toVMax(rootFontSize: number, width: number, height: number): VMax { - const pixel = this.value * rootFontSize; - const percentage = (pixel / Math.max(width, height)) * 100; - return new VMax(percentage); - } -} diff --git a/src/units/VH.ts b/src/units/VH.ts deleted file mode 100644 index 064d545..0000000 --- a/src/units/VH.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Centimeter } from 'units/Centimeter'; -import { Pixel } from 'units/Pixel'; -import { Rem } from 'units/Rem'; -import { VMax } from 'units/VMax'; -import { VMin } from 'units/VMin'; -import { VW } from 'units/VW'; - -export class VH extends Unit implements Omit { - public toString(): string { - return `${this.value}vh` - } - - public toCentimeter(height: number): Centimeter { - const pixel = height * (this.value / 100); - const centimeter = pixel / (96 / 2.54); - return new Centimeter(centimeter); - } - - public toPixel(height: number): Pixel { - const pixel = height * (this.value / 100); - return new Pixel(pixel); - } - - public toRem(height: number, rootFontSize: number): Rem { - const pixel = height * (this.value / 100); - const rem = pixel / rootFontSize; - return new Rem(rem); - } - - public toVW(height: number, width: number): VW { - const pixel = height * (this.value / 100); - const percentage = (pixel / width) * 100; - return new VW(percentage); - } - - public toVMin(height: number, width: number): VMin { - const pixel = height * (this.value / 100); - const percentage = (pixel / Math.min(width, height)) * 100; - return new VMin(percentage); - } - - public toVMax(height: number, width: number): VMax { - const pixel = height * (this.value / 100); - const percentage = (pixel / Math.max(width, height)) * 100; - return new VMax(percentage); - } -} diff --git a/src/units/VMax.ts b/src/units/VMax.ts deleted file mode 100644 index 12855fa..0000000 --- a/src/units/VMax.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Centimeter } from 'units/Centimeter'; -import { Pixel } from 'units/Pixel'; -import { Rem } from 'units/Rem'; -import { VH } from 'units/VH'; -import { VMin } from 'units/VMin'; -import { VW } from 'units/VW'; - -export class VMax extends Unit implements Omit { - public toString(): string { - return `${this.value}vmax` - } - - public toCentimeter(width: number, height: number): Centimeter { - const maximum = Math.max(width, height); - const pixel = maximum * (this.value / 100); - const centimeter = pixel / (96 / 2.54); - return new Centimeter(centimeter); - } - - public toPixel(width: number, height: number): Pixel { - const maximum = Math.max(width, height); - const pixel = maximum * (this.value / 100); - return new Pixel(pixel); - } - - public toRem(width: number, height: number, rootFontSize: number): Rem { - const maximum = Math.max(width, height); - const pixel = maximum * (this.value / 100); - const rem = pixel / rootFontSize; - return new Rem(rem); - } - - public toVW(width: number, height: number): VW { - const maximum = Math.max(width, height); - const pixel = maximum * (this.value / 100); - const percentage = (pixel / width) * 100; - return new VW(percentage); - } - - public toVH(width: number, height: number): VH { - const maximum = Math.max(width, height); - const pixel = maximum * (this.value / 100); - const percentage = (pixel / height) * 100; - return new VH(percentage); - } - - public toVMin(width: number, height: number): VMin { - const maximum = Math.max(width, height); - const minimum = Math.min(width, height); - const pixel = maximum * (this.value / 100); - const percentage = (pixel / minimum) * 100; - return new VMin(percentage); - } -} diff --git a/src/units/VMin.ts b/src/units/VMin.ts deleted file mode 100644 index 8d04016..0000000 --- a/src/units/VMin.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Centimeter } from 'units/Centimeter'; -import { Pixel } from 'units/Pixel'; -import { Rem } from 'units/Rem'; -import { VH } from 'units/VH'; -import { VMax } from 'units/VMax'; -import { VW } from 'units/VW'; - -export class VMin extends Unit implements Omit { - public toString(): string { - return `${this.value}vmin` - } - - public toCentimeter(width: number, height: number): Centimeter { - const minimum = Math.min(width, height); - const pixel = minimum * (this.value / 100); - const centimeter = pixel / (96 / 2.54); - return new Centimeter(centimeter); - } - - public toPixel(width: number, height: number): Pixel { - const minimum = Math.min(width, height); - const pixel = minimum * (this.value / 100); - return new Pixel(pixel); - } - - public toRem(width: number, height: number, rootFontSize: number): Rem { - const minimum = Math.min(width, height); - const pixel = minimum * (this.value / 100); - const rem = pixel / rootFontSize; - return new Rem(rem); - } - - public toVW(width: number, height: number): VW { - const minimum = Math.min(width, height); - const pixel = minimum * (this.value / 100); - const percentage = (pixel / width) * 100; - return new VW(percentage); - } - - public toVH(width: number, height: number): VH { - const minimum = Math.min(width, height); - const pixel = minimum * (this.value / 100); - const percentage = (pixel / height) * 100; - return new VH(percentage); - } - - public toVMax(width: number, height: number): VMax { - const maximum = Math.max(width, height); - const minimum = Math.min(width, height); - const pixel = minimum * (this.value / 100); - const percentage = (pixel / maximum) * 100; - return new VMax(percentage); - } -} diff --git a/src/units/VW.ts b/src/units/VW.ts deleted file mode 100644 index 9c92df3..0000000 --- a/src/units/VW.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { ConvertibleLengthUnit } from 'common/ConvertibleLengthUnit'; -import { Unit } from 'common/Unit'; -import { Centimeter } from 'units/Centimeter'; -import { Pixel } from 'units/Pixel'; -import { Rem } from 'units/Rem'; -import { VH } from 'units/VH'; -import { VMax } from 'units/VMax'; -import { VMin } from 'units/VMin'; - -export class VW extends Unit implements Omit { - public toString(): string { - return `${this.value}vw` - } - - public toCentimeter(width: number): Centimeter { - const pixel = width * (this.value / 100); - const centimeter = pixel / (96 / 2.54); - return new Centimeter(centimeter); - } - - public toPixel(width: number): Pixel { - const pixel = width * (this.value / 100); - return new Pixel(pixel); - } - - public toRem(width: number, rootFontSize: number): Rem { - const pixel = width * (this.value / 100); - const rem = pixel / rootFontSize; - return new Rem(rem); - } - - public toVH(width: number, height: number): VH { - const pixel = width * (this.value / 100); - const percentage = (pixel / height) * 100; - return new VH(percentage); - } - - public toVMin(width: number, height: number): VMin { - const pixel = width * (this.value / 100); - const percentage = (pixel / Math.min(width, height)) * 100; - return new VMin(percentage); - } - - public toVMax(width: number, height: number): VMax { - const pixel = width * (this.value / 100); - const percentage = (pixel / Math.max(width, height)) * 100; - return new VMax(percentage); - } -}