difference-engine v1.2.4
difference-engine
Difference Engine
npm i -P difference-engineFeatures are available as static methods on the class:
import DifferenceEngine from 'difference-engine'And as functions exported from the module:
import {
inclusive,
exclusive
} from '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) // returns ['C']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) // returns ['A', 'B']ArrayEngine
Features are available as static methods on the class:
import { ArrayEngine } from 'difference-engine'And as functions exported from the module:
import ArrayEngine, {
indexOf,
iterateForward,
iterateReverse,
iterateBetween,
max,
min,
bite
} from 'difference-engine/array-engine'Examples use the static form.
indexOf
const array = ['A', 'B', 'C', 'D', 'E']
const index = ArrayEngine.indexOf(array, 'E') // returns 4bite
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) // returns ['A']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 0, 4) // returns ['A', 'B', 'C', 'D', 'E']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 4, 0) // returns ['E', 'D', 'C', 'B', 'A']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 4, 4) // returns ['E']Negative
const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, -4, 0) // returns ['A']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, -4, 4) // returns ['A', 'B', 'C', 'D', 'E']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, -4, -4) // returns ['E', 'D', 'C', 'B', 'A']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 4, -4) // returns ['E', 'D', 'C', 'B', 'A']const alpha = ['A', 'B', 'C', 'D', 'E']
const omega = ArrayEngine.bite(alpha, 0, -4) // returns ['A']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) // returns 'E'const array = [1, 2, 3, 4, 5]
const value = ArrayEngine.max(array) // returns 5min
Accepts an array. Returns the smallest item (when compared as a number).
const array = ['A', 'B', 'C', 'D', 'E']
const value = ArrayEngine.min(array) // returns 'A'const array = [1, 2, 3, 4, 5]
const value = ArrayEngine.min(array) // returns 1NumberEngine
Features are available as static methods on the class:
import { NumberEngine } from 'difference-engine'And as functions exported from the module:
import NumberEngine, {
fibonacci,
haversine,
fromOctToDec,
fromHexToDec,
Weight,
Temperature,
Distance
} from 'difference-engine/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) // returns 0const fibonacci = NumberEngine.fibonacci(1) // returns 1const fibonacci = NumberEngine.fibonacci(2) // returns 1const fibonacci = NumberEngine.fibonacci(3) // returns 2haversine
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() // returns the distance in kilometres
const mi = NumberEngine.haversine(points).mi() // returns the distance in milesfromOctToDec
Accepts a string representing an octal. Returns a decimal.
const dec = NumberEngine.fromOctToDec('20') // returns 16fromHexToDec
Accepts a string representing a hexadecimal. Returns a decimal.
const dec = NumberEngine.fromHexToDec('10') // returns 16Weight
Convert a number from one unit to another.
As static methods on the class:
import { NumberEngine: { Weight } } from 'difference-engine'And as functions exported from the module:
import 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
} from 'difference-engine/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:
import { NumberEngine: { Temperature } } from 'difference-engine'And as functions exported from the module:
import Temperature, {
fromCToF,
fromFToC
} from 'difference-engine/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:
import { NumberEngine: { Distance } } from 'difference-engine'And as functions exported from the module:
import 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
} from 'difference-engine/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
import { StringEngine } from 'difference-engine'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) // returns ','const string = StringEngine.charAt('Hello, World', 5) // returns ','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) // returns 44const number = StringEngine.charCodeAt('Hello, World', 5) // returns 44charOf
Accepts a string which is an HTML entity. Returns the character for the entity.
const string = StringEngine.charOf(',') // returns ','const string = StringEngine.charOf(',') // returns ','charCodeOf
Accepts a string which is an HTML entity. Returns the character code for the entity.
const number = StringEngine.charCodeOf(',') // returns 44const number = StringEngine.charCodeOf(',') // returns 44entityAt
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) // returns ','const string = StringEngine.entityAt('Hello, World', 6) // returns ' 'Where the character at the index has an entity name, it returns the entity name.
const string = StringEngine.entityAt('Hello, World', 5) // returns ','Where the character at the index has an entity code, it returns the entity code.
const string = StringEngine.entityAt('Hello, World', 6) // returns ' '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) // returns ','Where the character at the index is an entity code, it returns the entity code.
const string = StringEngine.entityCodeAt('Hello, World', 5) // returns ','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) // returns '&'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) // returns ','Where the character at the index is an entity name, it returns the entity name.
const string = StringEngine.entityNameAt('Hello, World', 5) // returns ','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) // returns '&'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(',') // returns ','const string = StringEngine.entityOf(' ') // returns ' 'fromCodePoint
Accepts a number which is a character code point. Returns the character for that code point.
const string = StringEngine.fromCodePoint(43) // returns '+'charFromEntityCode
Accepts a string which is an HTML entity code. Returns the character for that entity.
const string = StringEngine.charFromEntityCode('+') // returns '+'charFromEntityName
Accepts a string which is an HTML entity name. Returns the character for that entity.
const string = StringEngine.charFromEntityCode('+') // returns '+'toEntityCode
Accepts a string. Returns a string replaced with the HTML entity code for each character.
const string = StringEngine.toEntityCode('Hello, World') // returns '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') // returns 'Hello, World'entityCodeFromChar
Accepts a single-character string. Returns a string representing the HTML entity code for that character.
const entityCode = StringEngine.entityCodeFromChar('+') // returns '+'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('+') // returns '+'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) // returns '20'fromDecToHex
Accepts number. Returns a string representing a hexadecimal.
const hex = StringEngine.fromDecToHex(16) // returns '10'reverse
Accepts a string. Returns the string with characters in reverse order.
const string = StringEngine.reverse('ABCDE') // returns 'EDCBA'10 months ago
11 months ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago