Options
All
  • Public
  • Public/Protected
  • All
Menu

The main class which contains a list of ranges and the user input.

Hierarchy

  • Base

Index

Constructors

constructor

  • Creates a new instance of Base given some user input to parse or an existing list of ranges to use instead.

    Parameters

    • input: RangesInput

      The input to parse if ranges is not provided.

    • Optional ranges: RangeList

      The already parsed ranges to use for this instance.

    Returns Base

Properties

input

The input parsed to generate this instance or the input passed from the parent instance when an operation was performed on it.

ranges

ranges: RangeList

The list of ranges in this instance.

Accessors

hasRanges

hasRanges:

Returns whether this instance has actual ranges. An actual range is where the minimum and maximum values differ.

see

Range.isRange

hasRates

hasRates:

Returns whether this instance has values or ranges that are rates.

see

Range.isRate

isFixed

isFixed:

Returns true if this instance has a single fixed value.

see

Range.isFixed

isRange

isRange:

Returns true if this instance has a single range.

see

Range.isRange

isRate

isRate:

Returns true if this instance has a single rate.

see

Range.isRate

isValid

isValid:

Returns whether this instance only has valid ranges. If any of the ranges in this instance are not valid false is returned, otherwise true.

see

Range.isValid

length

length:

Returns the number of ranges in this instance.

Methods

add

  • Adds the ranges of this instance and the given input together. When the ranges use the same units they are added together, otherwise they are added to the end of the range list.

    For example:

    uz('1pt').add('2pt, 1c'); // '3pt, 1c'
    uz('1pt').add('2pt, 1c', 2); // '5pt, 2c'
    
    see

    Base.operate

    see

    Range.add

    see

    Range.scale

    Parameters

    • input: BaseInput

      An instance or input which can be parsed into an instance.

    • Default value scale: number = 1

      A number to multiple the input by when adding it to this instance.

    Returns Base

    A new instance.

classes

  • Returns an array of the classes represented in this instance. If there are no classes in this instance then an empty array is returned.

    Returns Class[]

    An array of the classes in this instance.

compact

  • Joins all ranges of the same classes together and uses the largest unit to represent the sum for the class.

    For example:

    uz('1c, 1pt').compact(); // '1.5pt'
    
    see

    Transform

    see

    Core.globalTransform

    Parameters

    • Optional options: TransformInput

      Options to control which units and values are acceptable.

    Returns Base

    A new instance.

conversions

  • Joins all ranges of the same classes together and then calculates all equivalent ranges for each range for each valid group according to the given options.

    For example:

    uz('1.5pt').conversions(); // '3/16gal, 3/4qt, 1 1/2pt, 3c, 24floz, 48tbsp, 144tsp'
    uz('20celsius, 45deg'); // '68F, 20celsius, 45deg, 0.785rad'
    
    see

    Transform

    see

    Core.globalTransform

    see

    Value.conversions

    Parameters

    • Optional options: TransformInput

      Options to control which units and values are acceptable.

    Returns Base

    A new instance.

convert

  • convert(unit: string): Range
  • Converts the appropriate ranges in this instance into the desired unit and returns their converted sum. If the given unit does not map to a group then null is returned. If there are no ranges in this instance in the same class then the range returned is equivalent to zero.

    For example:

    uz('1in, 1m, 1ft').convert('cm'); // '133.02 cm'
    uz('60 mph').convert('miles per minute'); // '1 miles/minute'
    
    see

    Core.getGroup

    see

    Range.isZero

    Parameters

    • unit: string

      The unit to calculate the sum of.

    Returns Range

    A new range which is the sum of ranges in the same class converted to the desired unit.

each

  • each(iterate: function, reverse?: boolean): this
  • Iterates over each range in this instance in order or reversed and passes each one to the given iterate function. If the iterate function returns false the iteration will stop.

    Parameters

    • iterate: function

      The function to invoke with each range and it's index.

        • (range: Range, index: number): any
        • Parameters

          • range: Range

            The current range being iterated.

          • index: number

            The index of the current range in this instance.

          Returns any

    • Default value reverse: boolean = false

      Whether the iteration should be done forward or backward.

    Returns this

    The reference to this instance.

expand

  • Joins all ranges of the same classes together and then separates them into whole number ranges for better readability.

    For example:

    uz('1.5pt').expand(); // '1pt, 1c'
    uz('53in').expand(); // '4ft, 5in'
    uz('2ft, 29in').expand(); // '4ft, 5in'
    uz('6543mm').expand(); // '6 m, 54 cm, 3 mm'
    
    see

    Transform

    see

    Core.globalTransform

    Parameters

    • Optional options: TransformInput

      Options to control which units and values are acceptable.

    Returns Base

    A new instance.

filter

  • Removes the ranges from this instance that aren't valid according to the transform options provided taking into account the global options.

    For example:

    uz('1in, 2m').filter({system: Unitz.System.METRIC}); // '2m'
    
    see

    Transform

    see

    Core.globalTransform

    see

    Transform.isValidRange

    Parameters

    • Optional options: TransformInput

      Options to control which units and values are acceptable.

    Returns Base

    A new instance.

fractions

  • Converts each range to fractions if a denominator for the specified units yields a fraction close enough to the original value.

    For example:

    uz('1/2 cup').fractions(); // '1/2 cup'
    uz('0.3cm').fractions(); // '3/10 cm'
    uz('0.33 decades').fractions(); // '0.33 decades' closest is 3/10 but that's not close enough
    
    see

    Range.fractioned

    see

    Base.mutate

    Returns Base

    A new instance.

getScaleTo

  • getScaleTo(unitValue: string, rangeDelta?: number): number
  • Calculates what this instance would need to be scaled by so that the given value & unit pair is equal to the sum of ranges in this instance of the same class. If there are no ranges with the same class then zero is returned. If the sum of ranges with the same class results in an actual range (where min != max) then you can specify how to pick a value from the range with rangeDetla. A value of 0 uses the min, 1 uses the max, and 0.5 uses the average between them.

    For example:

    uz('1m, 2 - 3c').getScaleTo('6c'); // 2
    uz('1m, 2 - 3c').getScaleTo('6c', 0); // 3
    uz('1m, 2 - 3c').getScaleTo('6c', 0.5); // 2.4
    uz('1m, 2 - 3c').getScaleTo('45deg'); // 0
    
    see

    Base.convert

    see

    Parse.value

    Parameters

    • unitValue: string

      A value & unit pair to scale the ranges in this instance to.

    • Default value rangeDelta: number = 1

      When this instance contains ranges this value instructs how the scale factor is calculated. A value of 0 means it looks at the minimum, 1 is the maximum, and 0.5 is the average.

    Returns number

    A value to scale by or zero if this instance cannot match the input.

max

  • Flattens any ranges to their maximum values.

    For example:

    uz('1 - 3c, 5m').max(); // '3c, 5m'
    
    see

    Range.maxd

    see

    Base.mutate

    Returns Base

    A new instance or this if this instance has no ranges.

min

  • Flattens any ranges to their minimum values.

    For example:

    uz('1 - 3c, 5m').max(); // '1c, 5m'
    
    see

    Range.mind

    see

    Base.mutate

    Returns Base

    A new instance or this if this instance has no ranges.

mul

  • Scales the ranges in this instance by the given value and returns a new instance.

    For example:

    uz('1c, 3/5m').scale(Value.fromFraction(2, 3)); // '2/3c, 6/15m'
    
    see

    Range.mul

    see

    Base.mutate

    Parameters

    • amount: Value

      The value to scale the ranges in this instance by.

    Returns Base

    A new instance.

mutate

  • Executes the given function on each range in this instance and if the function returns a valid range its added to the result.

    For example:

    uz('1.5pt').mutate(r => r.scale(2)); // '3pt'
    

    Parameters

    • mutator: RangeMutator

      The function which may return a range.

    Returns Base

    A new instance.

negative

  • Drops positive ranges and modifies partially positive ranges so that all values are less than zero.

    For example:

    uz('0c, 2tbsp, -4tbsp').negative(); // '-4tbsp'
    uz('-2 - 3 in').negative(); // '-2 - 0in'
    
    see

    Range.negative

    see

    Base.mutate

    Returns Base

    A new instance.

nonzero

normalize

numbers

  • Converts each range to numbers if they are fractions.

    For example:

    uz('1/2 cup').fractions(); // '0.5 cup'
    uz('0.3cm').fractions(); // '0.3 cm'
    
    see

    Range.numbered

    see

    Base.mutate

    Returns Base

    A new instance.

operate

  • operate(input: BaseInput, operate: function, remainder: function): Base
  • Subtracts the given input from the ranges of this instance. When the ranges use the same units they are subtracted, otherwise they are added to the end of the range list and negated.

    For example:

    uz('3pt').sub('2pt, 1c'); // '1pt, -1c'
    uz('1pt').add('2pt, 1c', 2); // '-3pt, -2c'
    
    see

    Range.isExactMatch

    Parameters

    • input: BaseInput

      An instance or input which can be parsed into an instance.

    • operate: function

      A function to call when matching ranges are found and an operation should be performed between them. The range returned by this function ends up in the result.

    • remainder: function

      A function to call on a range that did not have a match in this instance where the range returned is added to the result.

    Returns Base

    A new instance.

output

  • Converts the ranges in this instance to a string with the given output options taking into account the global options.

    see

    Output

    Parameters

    • Optional options: OutputInput

      The options to override the global output options.

    Returns string

    The string representation of this instance.

positive

  • Drops negative ranges and modifies partially negative ranges so that all values are greater than or equal to zero.

    For example:

    uz('0c, 2tbsp, -4tbsp').positive(); // '0c, 2tbsp'
    uz('-2 - 3 in').positive(); // '0 - 3in'
    
    see

    Range.positive

    see

    Base.mutate

    Returns Base

    A new instance.

preferred

scale

  • scale(amount: number): Base
  • Scales the ranges in this instance by the given factor and returns a new instance.

    For example:

    uz('1c, 2.3m').scale(2); // '2c, 4.6m'
    
    see

    Range.scale

    see

    Base.mutate

    Parameters

    • amount: number

      The factor to scale the ranges in this instance by.

    Returns Base

    A new instance.

scaleTo

  • scaleTo(unitValue: string, rangeDelta?: number): Base
  • Scales the ranges in this instance up to some value with a unit and returns a new instance. Because this instance might contain ranges, a rangeDelta can be specified to instruct on which value (min or max) to use when calculating how much to scale by.

    For example:

    uz('1m, 2 - 3c').scaleTo('6c'); // '2m, 4 - 6c'
    uz('1m, 2 - 3c').scaleTo('6c', 0); // '3m, 6 - 9c'
    uz('1m, 2 - 3c').scaleTo('6c', 0.5); // '2.4m, 4.8 - 6c'
    
    see

    Base.getScaleTo

    see

    Base.scale

    Parameters

    • unitValue: string

      A value & unit pair to scale the ranges in this instance to.

    • Default value rangeDelta: number = 1

      When this instance contains ranges this value instructs how the scale factor is calculated. A value of 0 means it looks at the minimum, 1 is the maximum, and 0.5 is the average.

    Returns Base

    A new instance.

sort

  • Sorts the ranges in this instance based on the options provided taking into account the global options.

    For example:

    uz('1in, 3ft, 1.3yd, 1m').sort(); // 1.3yd, 1m, 3ft, 1in
    uz('1in, 3ft, 1.3yd, 1m').sort({ascending: true}); // 1in, 3ft, 1m, 1.3yd
    uz('1-3cups, 2-2.5cups, 4in').sort({
     type: Unitz.SortType.MIN,
     classes: {
      Volume: 1,
      Length: 2
     }
    }); // 4in, 2 - 2.5cups, 1 - 3cups
    
    see

    Sort

    see

    Core.globalSort

    Parameters

    • Optional options: SortInput

      Options to control how sorting is done.

    Returns Base

    A new instance.

sub

  • Subtracts the given input from the ranges of this instance. When the ranges use the same units they are subtracted, otherwise they are added to the end of the range list and negated.

    For example:

    uz('3pt').sub('2pt, 1c'); // '1pt, -1c'
    uz('1pt').add('2pt, 1c', 2); // '-3pt, -2c'
    
    see

    Base.operate

    see

    Range.sub

    see

    Range.scale

    Parameters

    • input: BaseInput

      An instance or input which can be parsed into an instance.

    • Default value scale: number = 1

      A number to multiple the input by when subtracting it from this instance.

    Returns Base

    A new instance.

test

  • test(expected: boolean, passed: boolean, tester: function): boolean
  • Performs a test on the ranges in this instance and returns whether the ranges passed the test. If the tester function returns something different than expected then the function ends early with !passed. If all tests pass then passed is returned.

    Parameters

    • expected: boolean

      The expected result of the tester.

    • passed: boolean

      The value to return if all ranges pass the test.

    • tester: function

      The function to test a range.

        • (range: Range): boolean
        • Parameters

          Returns boolean

    Returns boolean

    Return passed if all ranges return expected from tester.

to

  • to(unit: string): Range

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc