difference-engine
Difference Engine
npm i -P difference-engine
Features are available as static methods on the class:
const { DifferenceEngine } = require('./lib')
And as functions exported from the module:
const {
default: DifferenceEngine,
inclusive,
exclusive
} = require('./lib/difference-engine')
Examples use the static form.
inclusive
Accepts two arrays. Returns an array containing items which appear in both arrays.
const alpha = ['A', 'B', 'C']
const omega = ['C', 'D', 'E']
const array = DifferenceEngine.inclusive(alpha, omega)
exclusive
Accepts two arrays. Returns an array containing items which appear in the first array but not the second.
const alpha = ['A', 'B', 'C']
const omega = ['C', 'D', 'E']
const array = DifferenceEngine.exclusive(alpha, omega)
ArrayEngine
Features are available as static methods on the class:
const { ArrayEngine } = require('./lib')
And as functions exported from the module:
const {
default: ArrayEngine,
indexOf,
iterateForward,
iterateReverse,
iterateBetween,
max,
min,
bite
} = require('./lib/array-engine')
Examples use the static form.
indexOf
const array = ['A', 'B', 'C', 'D', 'E']
const index = ArrayEngine.indexOf(array, 'E')
bite
Accepts an array and two indexes. Returns a slice from the array.
Positive
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 0, 0)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 0, 4)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 4, 0)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 4, 4)
Negative
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, -4, 0)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, -4, 4)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, -4, -4)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 4, -4)
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 0, -4)
iterateForward
Accepts an array and a function. Iterates from start to end.
const array = ['A', 'B', 'C', 'D', 'E']
ArrayEngine.iterateForward(array, () => {})
iterateReverse
Accepts an array and a function. Iterates from end to start.
const array = ['A', 'B', 'C', 'D', 'E']
ArrayEngine.iterateReverse(array, () => {})
iterateBetween
Accepts an array, a start index, an end index, and a function.
If the first index is less than the second index, it behaves as iterateForward
.
If the first index is greater than the second index, it behaves as iterateReverse
.
const array = ['A', 'B', 'C', 'D', 'E']
ArrayEngine.iterateBetween(array, 1, 3, () => {})
max
Accepts an array. Returns the largest item (when compared as a number).
const array = ['A', 'B', 'C', 'D', 'E']
const value = ArrayEngine.max(array)
const array = [1, 2, 3, 4, 5]
const value = ArrayEngine.max(array)
min
Accepts an array. Returns the smallest item (when compared as a number).
const array = ['A', 'B', 'C', 'D', 'E']
const value = ArrayEngine.min(array)
const array = [1, 2, 3, 4, 5]
const value = ArrayEngine.min(array)
NumberEngine
Features are available as static methods on the class:
const { NumberEngine } = require('./lib')
And as functions exported from the module:
const {
default: NumberEngine,
fibonacci,
haversine,
fromOctToDec,
fromHexToDec,
Weight,
Temperature,
Distance
} = require('./lib/number-engine')
Examples use the static form.
fibonacci
Accepts an index. Returns the number in a Fibonacci sequence corresponding to the index.
const fibonacci = NumberEngine.fibonacci(0)
const fibonacci = NumberEngine.fibonacci(1)
const fibonacci = NumberEngine.fibonacci(2)
const fibonacci = NumberEngine.fibonacci(3)
haversine
Accepts an object with fields describing latitude and longitude points from
and to
.
Returns a value in kilometres or miles.
const points = { from: { lat: 51.4934, lng: 0.0098 }, to: { lat: 40.7128, lng: 74.0060 } }
const km = NumberEngine.haversine(points).km()
const mi = NumberEngine.haversine(points).mi()
fromOctToDec
Accepts a string representing an octal. Returns a decimal.
const dec = NumberEngine.fromOctToDec('20')
fromHexToDec
Accepts a string representing a hexadecimal. Returns a decimal.
const dec = NumberEngine.fromHexToDec('10')
Weight
Convert a number from one unit to another.
As static methods on the class:
const { NumberEngine: { Weight } } = require('./lib')
And as functions exported from the module:
const {
default: Weight,
fromKgToGr,
fromKgToMg,
fromKgToOz,
fromKgToLb,
fromKgToSt,
fromGrToKg,
fromGrToMg,
fromGrToOz,
fromGrToLb,
fromGrToSt,
fromMgToKg,
fromMgToGr,
fromMgToOz,
fromMgToLb,
fromMgToSt,
fromOzToKg,
fromOzToGr,
fromOzToMg,
fromOzToLb,
fromOzToSt,
fromLbToKg,
fromLbToGr,
fromLbToMg,
fromLbToOz,
fromLbToSt,
fromStToKg,
fromStToGr,
fromStToMg,
fromStToLb,
fromStToOz,
convert
} = require('./lib/number-engine/weight')
Examples use the static form.
Kilogram
const gr = Weight.fromKgToGr(10)
const mg = Weight.fromKgToMg(10)
const oz = Weight.fromKgToOz(10)
const lb = Weight.fromKgToLb(10)
const st = Weight.fromKgToSt(10)
Or, using convert
.
const gr = Weight.convert(10).fromKg.toGr()
const mg = Weight.convert(10).fromKg.toMg()
const oz = Weight.convert(10).fromKg.toOz()
const lb = Weight.convert(10).fromKg.toLb()
const st = Weight.convert(10).fromKg.toSt()
Gram
const kg = Weight.fromGrToKg(10)
const mg = Weight.fromGrToMg(10)
const oz = Weight.fromGrToOz(10)
const lb = Weight.fromGrToLb(10)
const st = Weight.fromGrToSt(10)
Or, using convert
.
const kg = Weight.convert(10).fromGr.toKg()
const mg = Weight.convert(10).fromGr.toMg()
const oz = Weight.convert(10).fromGr.toOz()
const lb = Weight.convert(10).fromGr.toLb()
const st = Weight.convert(10).fromGr.toSt()
Miligram
const kg = Weight.fromMgToKg(10)
const gr = Weight.fromMgToGr(10)
const oz = Weight.fromMgToOz(10)
const lb = Weight.fromMgToLb(10)
const st = Weight.fromMgToSt(10)
Or, using convert
.
const kg = Weight.convert(10).fromMg.toKg()
const gr = Weight.convert(10).fromMg.toGr()
const oz = Weight.convert(10).fromMg.toOz()
const lb = Weight.convert(10).fromMg.toLb()
const st = Weight.convert(10).fromMg.toSt()
Ounce
const kg = Weight.fromOzToKg(10)
const gr = Weight.fromOzToGr(10)
const mg = Weight.fromOzToMg(10)
const lb = Weight.fromOzToLb(10)
const st = Weight.fromOzToSt(10)
Or, using convert
.
const kg = Weight.convert(10).fromOz.toKg()
const gr = Weight.convert(10).fromOz.toGr()
const mg = Weight.convert(10).fromOz.toMg()
const lb = Weight.convert(10).fromOz.toLb()
const st = Weight.convert(10).fromOz.toSt()
Pound
const kg = Weight.fromLbToKg(10)
const gr = Weight.fromLbToGr(10)
const mg = Weight.fromLbToMg(10)
const oz = Weight.fromLbToOz(10)
const st = Weight.fromLbToSt(10)
Or, using convert
.
const kg = Weight.convert(10).fromLb.toKg()
const gr = Weight.convert(10).fromLb.toGr()
const mg = Weight.convert(10).fromLb.toMg()
const oz = Weight.convert(10).fromLb.toOz()
const st = Weight.convert(10).fromLb.toSt()
Stone
const kg = Weight.fromStToKg(10)
const gr = Weight.fromStToGr(10)
const mg = Weight.fromStToMg(10)
const lb = Weight.fromStToLb(10)
const oz = Weight.fromStToOz(10)
Or, using convert
.
const kg = Weight.convert(10).fromSt.toKg()
const gr = Weight.convert(10).fromSt.toGr()
const mg = Weight.convert(10).fromSt.toMg()
const lb = Weight.convert(10).fromSt.toLb()
const oz = Weight.convert(10).fromSt.toOz()
Temperature
Convert a number from one unit to another.
As static methods on the class:
const { NumberEngine: { Temperature } } = require('./lib')
And as functions exported from the module:
const {
default: Temperature,
fromCToF,
fromFToC
} = require('./lib/number-engine/temperature')
Examples use the static form.
const value = Temperature.fromCToF(10)
const value = Temperature.fromFToC(10)
Distance
Convert a number from one unit to another.
As static methods on the class:
const { NumberEngine: { Distance } } = require('./lib')
And as functions exported from the module:
const {
default: Distance,
fromKmToMt,
fromKmToCm,
fromKmToMm,
fromKmToIn,
fromKmToFt,
fromKmToYd,
fromKmToMi,
fromMtToKm,
fromMtToCm,
fromMtToMm,
fromMtToIn,
fromMtToFt,
fromMtToYd,
fromMtToMi,
fromCmToKm,
fromCmToMt,
fromCmToMm,
fromCmToIn,
fromCmToFt,
fromCmToYd,
fromCmToMi,
fromMmToKm,
fromMmToMt,
fromMmToCm,
fromMmToIn,
fromMmToFt,
fromMmToYd,
fromMmToMi,
fromInToKm,
fromInToMt,
fromInToCm,
fromInToMm,
fromInToFt,
fromInToYd,
fromInToMi,
fromFtToKm,
fromFtToMt,
fromFtToCm,
fromFtToMm,
fromFtToIn,
fromFtToYd,
fromFtToMi,
fromYdToKm,
fromYdToMt,
fromYdToCm,
fromYdToMm,
fromYdToIn,
fromYdToFt,
fromYdToMi,
fromMiToKm,
fromMiToMt,
fromMiToCm,
fromMiToMm,
fromMiToIn,
fromMiToFt,
fromMiToYd,
convert
} = require('./lib/number-engine/distance')
Examples use the static form.
Kilometre
const mt = Distance.fromKmToMt(10)
const cm = Distance.fromKmToCm(10)
const mm = Distance.fromKmToMm(10)
const in = Distance.fromKmToIn(10)
const ft = Distance.fromKmToFt(10)
const yd = Distance.fromKmToYd(10)
const mi = Distance.fromKmToMi(10)
Or, using convert
.
const mt = Distance.convert(10).fromKm.toMt()
const cm = Distance.convert(10).fromKm.toCm()
const mm = Distance.convert(10).fromKm.toMm()
const in = Distance.convert(10).fromKm.toIn()
const ft = Distance.convert(10).fromKm.toFt()
const yd = Distance.convert(10).fromKm.toYd()
const mi = Distance.convert(10).fromKm.toMi()
Metre
const km = Distance.fromMtToKm(10)
const cm = Distance.fromMtToCm(10)
const mm = Distance.fromMtToMm(10)
const in = Distance.fromMtToIn(10)
const ft = Distance.fromMtToFt(10)
const yd = Distance.fromMtToYd(10)
const mi = Distance.fromMtToMi(10)
Or, using convert
.
const km = Distance.convert(10).fromMt.toKm()
const cm = Distance.convert(10).fromMt.toCm()
const mm = Distance.convert(10).fromMt.toMm()
const in = Distance.convert(10).fromMt.toIn()
const ft = Distance.convert(10).fromMt.toFt()
const yd = Distance.convert(10).fromMt.toYd()
const mi = Distance.convert(10).fromMt.toMi()
Centimetre
const km = Distance.fromCmToKm(10)
const mt = Distance.fromCmToMt(10)
const mm = Distance.fromCmToMm(10)
const in = Distance.fromCmToIn(10)
const ft = Distance.fromCmToFt(10)
const yd = Distance.fromCmToYd(10)
const mi = Distance.fromCmToMi(10)
Or, using convert
.
const km = Distance.convert(10).fromCm.toKm()
const mt = Distance.convert(10).fromCm.toMt()
const mm = Distance.convert(10).fromCm.toMm()
const in = Distance.convert(10).fromCm.toIn()
const ft = Distance.convert(10).fromCm.toFt()
const yd = Distance.convert(10).fromCm.toYd()
const mi = Distance.convert(10).fromCm.toMi()
Milimetre
const km = Distance.fromMmToKm(10)
const mt = Distance.fromMmToMt(10)
const cm = Distance.fromMmToCm(10)
const in = Distance.fromMmToIn(10)
const ft = Distance.fromMmToFt(10)
const yd = Distance.fromMmToYd(10)
const mi = Distance.fromMmToMi(10)
Or, using convert
.
const km = Distance.convert(10).fromMm.toKm()
const mt = Distance.convert(10).fromMm.toMt()
const cm = Distance.convert(10).fromMm.toCm()
const in = Distance.convert(10).fromMm.toIn()
const ft = Distance.convert(10).fromMm.toFt()
const yd = Distance.convert(10).fromMm.toYd()
const mi = Distance.convert(10).fromMm.toMi()
Inch
const km = Distance.fromInToKm(10)
const mt = Distance.fromInToMt(10)
const cm = Distance.fromInToCm(10)
const mm = Distance.fromInToMm(10)
const ft = Distance.fromInToFt(10)
const yd = Distance.fromInToYd(10)
const mi = Distance.fromInToMi(10)
Or, using convert
.
const km = Distance.convert(10).fromIn.toKm()
const mt = Distance.convert(10).fromIn.toMt()
const cm = Distance.convert(10).fromIn.toCm()
const mm = Distance.convert(10).fromIn.toMm()
const ft = Distance.convert(10).fromIn.toFt()
const yd = Distance.convert(10).fromIn.toYd()
const mi = Distance.convert(10).fromIn.toMi()
Feet
const km = Distance.fromFtToKm(10)
const mt = Distance.fromFtToMt(10)
const cm = Distance.fromFtToCm(10)
const mm = Distance.fromFtToMm(10)
const in = Distance.fromFtToIn(10)
const yd = Distance.fromFtToYd(10)
const mi = Distance.fromFtToMi(10)
Or, using convert
.
const km = Distance.convert(10).fromFt.toKm()
const mt = Distance.convert(10).fromFt.toMt()
const cm = Distance.convert(10).fromFt.toCm()
const mm = Distance.convert(10).fromFt.toMm()
const in = Distance.convert(10).fromFt.toIn()
const yd = Distance.convert(10).fromFt.toYd()
const mi = Distance.convert(10).fromFt.toMi()
Yard
const km = Distance.fromYdToKm(10)
const mt = Distance.fromYdToMt(10)
const cm = Distance.fromYdToCm(10)
const mm = Distance.fromYdToMm(10)
const in = Distance.fromYdToIn(10)
const ft = Distance.fromYdToFt(10)
const mi = Distance.fromYdToMi(10)
Or, using convert
.
const km = Distance.convert(10).fromYd.toKm()
const mt = Distance.convert(10).fromYd.toMt()
const cm = Distance.convert(10).fromYd.toCm()
const mm = Distance.convert(10).fromYd.toMm()
const in = Distance.convert(10).fromYd.toIn()
const ft = Distance.convert(10).fromYd.toFt()
const mi = Distance.convert(10).fromYd.toMi()
Mile
const km = Distance.fromMiToKm(10)
const mt = Distance.fromMiToMt(10)
const cm = Distance.fromMiToCm(10)
const mm = Distance.fromMiToMm(10)
const in = Distance.fromMiToIn(10)
const ft = Distance.fromMiToFt(10)
const yd = Distance.fromMiToYd(10)
Or, using convert
.
const km = Distance.convert(10).fromMi.toKm()
const mt = Distance.convert(10).fromMi.toMt()
const cm = Distance.convert(10).fromMi.toCm()
const mm = Distance.convert(10).fromMi.toMm()
const in = Distance.convert(10).fromMi.toIn()
const ft = Distance.convert(10).fromMi.toFt()
const yd = Distance.convert(10).fromMi.toYd()
StringEngine
const { StringEngine } = require('./lib')
charAt
Accepts a string and a number which is an index. Returns the character for the entity at the index.
const string = StringEngine.charAt('Hello, World', 5)
const string = StringEngine.charAt('Hello, World', 5)
charCodeAt
Accepts a string and a number which is an index. Returns the character code for the entity at the index.
const number = StringEngine.charCodeAt('Hello, World', 5)
const number = StringEngine.charCodeAt('Hello, World', 5)
charOf
Accepts a string which is an HTML entity. Returns the character for the entity.
const string = StringEngine.charOf(',')
const string = StringEngine.charOf(',')
charCodeOf
Accepts a string which is an HTML entity. Returns the character code for the entity.
const number = StringEngine.charCodeOf(',')
const number = StringEngine.charCodeOf(',')
entityAt
Accepts a string and a number which is an index.
Where the character at the index is an entity, it returns the entity.
const string = StringEngine.entityAt('Hello, World', 5)
const string = StringEngine.entityAt('Hello, World', 6)
Where the character at the index has an entity name, it returns the entity name.
const string = StringEngine.entityAt('Hello, World', 5)
Where the character at the index has an entity code, it returns the entity code.
const string = StringEngine.entityAt('Hello, World', 6)
entityCodeAt
Accepts a string and a number which is an index.
Where the character at the index has an entity code, it returns the entity code.
const string = StringEngine.entityCodeAt('Hello, World', 5)
Where the character at the index is an entity code, it returns the entity code.
const string = StringEngine.entityCodeAt('Hello, World', 5)
Where the character at the index is an entity name, it returns the entity code for the char at the index.
const string = StringEngine.entityCodeAt('Hello, World', 5)
entityNameAt
Accepts a string and a number which is an index.
Where the character at the index has an entity name, it returns the entity name.
const string = StringEngine.entityNameAt('Hello, World', 5)
Where the character at the index is an entity name, it returns the entity name.
const string = StringEngine.entityNameAt('Hello, World', 5)
Where the character at the index is an entity code, it returns the entity name for the char at the index.
const string = StringEngine.entityNameAt('Hello, World', 5)
entityOf
Accepts a string and a number which is an index.
Where the character at the index is an entity, it returns the entity.
const string = StringEngine.entityOf(',')
const string = StringEngine.entityOf(' ')
fromCharCode
Accepts a number which is a character code. Returns the character for that code.
const string = StringEngine.charFromEntityCode('+')
charFromEntityCode
Accepts a string which is an HTML entity code. Returns the character for that entity.
const string = StringEngine.charFromEntityCode('+')
charFromEntityName
Accepts a string which is an HTML entity name. Returns the character for that entity.
const string = StringEngine.charFromEntityCode('+')
toEntityCode
Accepts a string. Returns a string replaced with the HTML entity code for each character.
const string = StringEngine.toEntityCode('Hello, World')
toEntityName
Accepts a string. Returns a string replaced with the HTML entity name for each character where it is known, otherwise the character is not replaced.
const string = StringEngine.toEntityName('Hello, World')
entityCodeFromChar
Accepts a single-character string. Returns a string representing the HTML entity code for that character.
const entityCode = StringEngine.entityCodeFromChar('+')
The entity is computed from the character code point.
entityNameFromChar
Accepts a single-character string. Returns a string representing the HTML entity code for that character.
const entityName = StringEngine.entityNameFromChar('+')
Where the character has no entity or the entity is not known it returns the character.
entityCodeOf
See entityCodeFromChar
.
entityNameOf
See entityNameFromChar
.
fromDecToOct
Accepts number. Returns a string representing an octal.
const oct = StringEngine.fromDecToOct(16)
fromDecToHex
Accepts number. Returns a string representing a hexadecimal.
const hex = StringEngine.fromDecToHex(16)
reverse
Accepts a string. Returns the string with characters in reverse order.
const string = StringEngine.reverse('ABCDE')