v0.0.4 Overview

Inches.js is a simple, lightweight and lightning fast js class that converts chaotic imperial dimension strings into objects that support addition and subtraction and boast a number of formatting options. Easy to use and accurate. See the .parts section of the docs for information about imperial and metric accuracy. Type anything you want into the demo below and try to break it! While using the demo, open your browser console and look for the named timestamps created with the ⧉ console.time() method for an idea on it's performance.


Demo


var inches = new Inches( input = '', inc = , max_inches = , mm_mode = );


inches.add('');


inches.less('');



Docs

.constructor(input = '', inc = 16, max_inches = 11, mm_mode = false)

The constructor takes up to four arguments. The first argument is a dimension of some sort. any of the following input arguments can be processed the Inches class:
"1ft", "1 foot", "1f"
(1'-0")
"12in", "12 inches", "12"
(1'-0")
"-0 1/2" or "-1/2"
(-0 1/2")
"25m", "25 m", "2500cm" or "25M"
(25000mm)
'1/16"' or even '1/15"'
(saved as X/2048)
0.125 or '0.125'
(str or float, 256/2048")
'1-6 1/4"', '1\'-6-1/4' or '1\' 6-1/4"'
(1'-6 1/4")
"1ft-6 1/4in", "1 6 1/4" and "1foot 6 1/4"
(1'-6 1/4")
"1 6 1 4"
(1'-6 1/4") four space seperated numbers treated as whole feet, whole inches, numerator and denominator.
"6 1 4"
(6 1/4") three space seperated numbers treated as whole inches, numerator and denominator.
"1 foot 1/4 inches"
(1'-0 1/4") zero full inches infered.
"-1ft"
(-1'-0") leading minus sign always creates negative result.
etc.
The second argument is the inc or 'increment'. This sets the .inc property which can also be set with the .set_inc() method without using the constructor and overwriting your instance.

The third argument is max_inches which sets the .max_inches property.

The fourth argument sets the .mm_mode property to true or false.

.inc property

This determines the fraction accuracy of the formatted strings returned by the .str() method. This can be set using the second argument of the constructor or by calling the .set_inc() method. If this is not 1,2,4,6,8,16,32 or 64, the constructor and .set_inc() method will ignore it.

.max_inches property

This determines the max length of a dimension that can be output by the .str() method without forcing inch values to feet values. For example, if set to 100, the .str() will return '100"' for an instance with a value of 100 inches and '8'-5"' for an instance with a value of 101 inches. This can be set using the third argument of the constructor or by calling the .set_max_inches() method.

.input property

This property is set to the raw input of the ._process() utility method prior to processing for reference, trouble-shooting and error-reporting purposes.

.err_msg property

This property will store an error message generated by a method. Default is false.

.mm_mode property

This property simply tells the Inches class instance that all integers, floats and numeric strings passed to the constructor, .add(), .less() and .update() methods are understood to be decimal mm values instead of decimal inch values. If this set to false, metric values can still be passed to these methods using metric strings:
new Inches('1200mm');
new Inches('120cm');
new Inches('0.12m');
This can be set using the fourth argument of the constructor or by setting the .mm_mode manually AFTER the constructor is called.

.parts property

This is an plain object containing the actual numeric value of the dimension represented by an Inches instance. This object has four properties:
{
  f: // number of feet
  i: // number of inches
  num: // fraction numerator (this is understood to have a denominator of 2048)
  mm: // the value of the dimension in mm,
}
The numerator is understood to have a denominator of 2048 allowing this library to add multiple objects together with far fewer rounding errors than using the .inc property as the understood denominator.

The actual inch dimension is equal to:
parts.f/12 + parts.i + (parts.num/2048)
while the mm value is equal to:
parts.mm
Note that .mm value is a parallel value to the imperial value represented by the .f, .i and .num properties together. The reason that Inches.parts stores the mm equivalent of the imperial dimension is that binary decimal precision would start distorting metric dimensions if they were stored as decimal inches. For example, you should get the string '100.0000976575mm' if you run the following code in your browser console while this page is loaded.
console.log(new Inches(new Inches('100mm').dec(0,10)).mm(1,10));
The highlighted section creates a new instance of Inches with an exact mm value of 100 and the outputs a decimal inch value accurate to 10 decimal places with the .dec() method...
console.log(new Inches(new Inches('100mm').dec(0,10)).mm(1,10));
...which is passed to the constructor again, creating an instance with an inch value of 3.9370117188...
console.log(new Inches(new Inches('100mm').dec(0,10)).mm(1,10));
...on which the .mm() method is called returning 100.0000976575mm...
console.log(new Inches(new Inches('100mm').dec(0,10)).mm(1,10));
This library can be used as an accurate metric library but it is advised that the Inches.mm_mode be set to true so that integers passed to the constructor, .add(), .less() and .update() methods will be saved as integer mm values instead of integer inch values.

.set_inc(inc = 0) method

.set_max_inches(max_inches = 11) method

.update(input = {}) method

Updates the dimension value of an Inches instance on which it is called without having to save a new instance to the existing variable the constructor. The input may be another Inches instance, an Inches.parts formatted object or chaotic dimension input like that passed to the constructor. If an object is passed, the mm property is ignored and reset based on the .f, .i and .num values passed.

.add(input = {}) method

Increases the dimension value of an Inches instance on which it is called. The input may be another Inches instance, an Inches.parts object or chaotic dimension input like that passed to the constructor.

.less(input = {}) method

Reduces the dimension value of an Inches instance on which it is called. The input may be another Inches instance, an Inches.parts object or chaotic dimension input like that passed to the constructor.

.dec(str = 0, accuracy = 2, o = {}) method

Returns a dimension in decimal inches. The following examples are from an input of "1 foot 6 and 11/16 inches".
inches.eng(0)
// returns "18.69" (default accuracy 2)
inches.eng(0,0)
// returns "19" (accuracy zero)
inches.eng(0,4)
// returns "18.6875"
inches.eng(1,2)
// returns "18.69in"
inches.eng(2,2)
// returns "18.69 in"
inches.eng(3,2)
// returns "18.69IN"
inches.eng(4,2)
// returns "18.69 IN"
inches.eng(5,2)
// returns "18.69\""
inches.eng(6,2)
// returns "18.69inches" ("inch" if singular)
inches.eng(7,2)
// returns "18.69 inches" ("inch" if singular)"
inches.eng(8,2)
// returns "18.69INCHES" ("INCH" if singular)"
inches.eng(9,2)
// returns "18.69 INCHES" ("INCH" if singular)"
inches.eng('string',2)
// returns "1.52string"

.eng(str = 0, accuracy = 4) method

Returns a dimension in decimal feet (aka engineering units). The following examples are from an input of "1 foot 6 and 1/4 inches".
inches.eng(0)
// returns "1.5208" (default accuracy 4)
inches.eng(0,0)
// returns "2" (accuracy zero)
inches.eng(0,2)
// returns "1.52"
inches.eng(1,2)
// returns "1.52ft"
inches.eng(2,2)
// returns "1.52 ft"
inches.eng(3,2)
// returns "1.52FT"
inches.eng(4,2)
// returns "1.52 FT"
inches.eng(5,2)
// returns "1.52'"
inches.eng(6,2)
// returns "1.52feet" ("foot" if singular)
inches.eng(7,2)
// returns "1.52 feet" ("foot" if singular)"
inches.eng(8,2)
// returns "1.52FEET" ("FOOT" if singular)"
inches.eng(9,2)
// returns "1.52 FEET" ("FOOT" if singular)"
inches.eng('string',2)
// returns "1.52string"

.str(long_form = false) method

Returns an imperial dimension string in one of two uniform formats. The .parts property will be evaluated and any fraction will be represented using a denominator less than or equal to the .inc property.
inches.str()
// 1'-6 1/4"
inches.str(true)
// 1ft-6 1/4in

.mm(str = 0, accuracy = 1) method

Returns a metric dimension string in centimeters. Accuracy will determine the number of decimal places in the output. The str argument can be an integer option or overriding string.
inches.mm(0,0)
// returns "3"
inches.mm(1,0)
// returns "3mm"
inches.mm(2,0)
// returns "3 mm"
inches.mm(3,0)
// returns "3MM"
inches.mm(4,0)
// returns "3 MM"
inches.mm('string',0)
// returns "3string"

.cm(str = 0, accuracy = 2) method

Returns a metric dimension string in centimeters. Accuracy will determine the number of decimal places in the output. The str argument can be an integer option or overriding string.
inches.cm(0,0)
// returns "3"
inches.cm(1,0)
// returns "3cm"
inches.cm(2,0)
// returns "3 cm"
inches.cm(3,0)
// returns "3CM"
inches.cm(4,0)
// returns "3 CM"
inches.cm('string',0)
// returns "3string"

.m(str = 0, accuracy = 4) method

Returns a metric dimension string in meters. Accuracy will determine the number of decimal places in the output. The str argument can be an integer option or overriding string.
inches.m(0,0)
// returns "3"
inches.m(1,0)
// returns "3m"
inches.m(2,0)
// returns "3 m"
inches.m(3,0)
// returns "3M"
inches.m(4,0)
// returns "3 M"
inches.m('string',0)
// returns "3string"

utility methods (use at your own risk)

._process(input = '')Evaluates chaotic imperial dimension strings and returns a plain object that must still be processed by the ._carry() utility.
._to_pos_zero(zero)Returns positive zero if input is ⧉ negative zero, otherwise returns input.
._accurate_dec(val = 0, accuracy = 0)Returns an accurate decimal with correct number of decimal places and never a negative zero.
._mm_obj(arg)Returns an object with parallel .mm and .i properties. Needs to be processed by the ._carry() method.
._increments()Returns array [1,2,4,6,8,16,32,64] which are the acceptable denominators for the .str() method.
._carry()Updates Inches.parts so that parts.num<2048, parts.mm value is correct and parts.i<Inches.max_inches.

Download



⧉ Inches.js 16KB

⧉ Inches.min.js 6KB