-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
193 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Variable, EquationConfig, Equation } from "@/types/calculators"; | ||
|
||
export const Variables: Record<string, Variable> = { | ||
distanceMly: { | ||
key: "distanceMly", | ||
precision: 1, | ||
sigFigs: 3, | ||
placeholder: "d", | ||
}, | ||
m15: { key: "m15", precision: 2, placeholder: "\\Delta m_{15}" }, | ||
peakApparentMagnitude: { | ||
key: "peakApparentMagnitude", | ||
precision: 1, | ||
placeholder: "m", | ||
}, | ||
peakAbsoluteMagnitude: { | ||
key: "peakAbsoluteMagnitude", | ||
precision: 1, | ||
placeholder: "M", | ||
}, | ||
}; | ||
|
||
const Config: Record<Equation, EquationConfig> = { | ||
peakAbsoluteMagnitude: { | ||
latex: ({ result, constants, variables }) => | ||
`${result} = ${constants.A} + ${constants.B}\\left ( ${variables.m15} \\right )`, | ||
constants: { | ||
A: { value: -23.598, precision: 2 }, | ||
B: { value: 6.457, precision: 2 }, | ||
}, | ||
inputs: [Variables.m15], | ||
result: Variables.peakAbsoluteMagnitude, | ||
}, | ||
distanceMly: { | ||
latex: ({ result, constants, variables }) => | ||
`${result} = \\left (${constants.A}\\right )${constants.B}^{\\frac{${variables.peakApparentMagnitude} - ${variables.peakAbsoluteMagnitude}}{${constants.C}}} + ${constants.D}`, | ||
constants: { | ||
A: { value: 3.26, precision: 2 }, | ||
B: { value: 10 }, | ||
C: { value: 5 }, | ||
D: { value: 1 }, | ||
}, | ||
inputs: [Variables.peakApparentMagnitude, Variables.peakAbsoluteMagnitude], | ||
result: { | ||
...Variables.distanceMly, | ||
addUnit: (value) => `${value}\\space\\text{Mly}`, | ||
}, | ||
}, | ||
}; | ||
|
||
export default Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,91 @@ | ||
import isNumber from "lodash/isNumber"; | ||
import { fallbackLng } from "@/lib/i18n/settings"; | ||
import { LaTeXComposer } from "@/types/calculators"; | ||
import placeholders from "./placeholders"; | ||
import Equations from "./equations"; | ||
import { | ||
Variable, | ||
Constant, | ||
Result, | ||
EquationConfig, | ||
} from "@/types/calculators"; | ||
import { Variables } from "./config"; | ||
|
||
const withClass = (value: string) => `\\class{calc-output}{${value}}`; | ||
|
||
const peakAbsoluteMagnitude: LaTeXComposer = ( | ||
{ result = placeholders.peakAbsoluteMagnitude, m15 = placeholders.m15 }, | ||
locale = fallbackLng | ||
) => { | ||
const { | ||
constants: { A, B }, | ||
} = Equations.peakAbsoluteMagnitude({}); | ||
const formatConstant = ({ precision, value }: Constant, locale = fallbackLng) => | ||
Intl.NumberFormat(locale, { | ||
minimumFractionDigits: precision, | ||
maximumFractionDigits: precision, | ||
}).format(value); | ||
|
||
const { format } = new Intl.NumberFormat(locale, { | ||
minimumFractionDigits: 2, | ||
maximumFractionDigits: 2, | ||
}); | ||
const { format: resultFormat } = new Intl.NumberFormat(locale, { | ||
minimumFractionDigits: 1, | ||
maximumFractionDigits: 1, | ||
}); | ||
const formatVariable = ({ | ||
variable, | ||
config: { precision, sigFigs, placeholder }, | ||
locale = fallbackLng, | ||
}: { | ||
variable?: number; | ||
config: Variable; | ||
locale?: string; | ||
}) => { | ||
if (isNumber(variable)) { | ||
return Intl.NumberFormat(locale, { | ||
minimumFractionDigits: precision, | ||
maximumFractionDigits: precision, | ||
maximumSignificantDigits: sigFigs, | ||
}).format(variable); | ||
} | ||
|
||
return placeholder; | ||
}; | ||
|
||
const displayResult = isNumber(result) ? resultFormat(result) : result; | ||
const displayM15 = isNumber(m15) ? format(m15) : m15; | ||
const formatResult = ({ | ||
result: variable, | ||
config: { addUnit, ...config }, | ||
locale = fallbackLng, | ||
}: { | ||
result?: number; | ||
config: Result; | ||
locale?: string; | ||
}) => { | ||
if (isNumber(variable) && addUnit) { | ||
return withClass(addUnit(formatVariable({ variable, config, locale }))); | ||
} | ||
|
||
return `${withClass(displayResult)} = ${format(A)} + ${format( | ||
B | ||
)}\\left ( ${displayM15} \\right )`; | ||
return withClass( | ||
formatVariable({ | ||
variable, | ||
config, | ||
locale, | ||
}) | ||
); | ||
}; | ||
|
||
const supernovaDistance: LaTeXComposer = ( | ||
{ | ||
result = placeholders.distance, | ||
peakAbsoluteMagnitude = placeholders.peakAbsoluteMagnitude, | ||
peakApparentMagnitude = placeholders.peakApparentMagnitude, | ||
}, | ||
const LaTeXComposer = ( | ||
equation: EquationConfig, | ||
values: { result?: number; variables: Record<string, number | undefined> }, | ||
locale = fallbackLng | ||
) => { | ||
const { | ||
constants: { A, B, C, D }, | ||
} = Equations.supernovaDistance({}); | ||
const { latex, constants, result: resultConfig } = equation; | ||
const { result, variables } = values; | ||
|
||
const { format } = new Intl.NumberFormat(locale, { | ||
minimumFractionDigits: 1, | ||
maximumFractionDigits: 1, | ||
}); | ||
const formattedConstants: Record<string, string> = {}; | ||
const formattedVariables: Record<string, string> = {}; | ||
|
||
const displayResult = isNumber(result) | ||
? `${Intl.NumberFormat(locale, { | ||
maximumSignificantDigits: 3, | ||
}).format(result)}\\space\\text{Mly}` | ||
: result; | ||
const displayPeakAbsolute = isNumber(peakAbsoluteMagnitude) | ||
? format(peakAbsoluteMagnitude) | ||
: peakAbsoluteMagnitude; | ||
const displayPeakApparent = isNumber(peakApparentMagnitude) | ||
? format(peakApparentMagnitude) | ||
: peakApparentMagnitude; | ||
Object.entries(constants).map(([key, constant]) => { | ||
formattedConstants[key] = formatConstant(constant, locale); | ||
}); | ||
|
||
return `${withClass(displayResult)} = \\left (${Intl.NumberFormat(locale, { | ||
minimumFractionDigits: 2, | ||
maximumFractionDigits: 2, | ||
}).format( | ||
A | ||
)}\\right )${B}^{\\frac{${displayPeakApparent} - ${displayPeakAbsolute}}{${C}} + ${D}}`; | ||
}; | ||
Object.entries(variables).map(([key, variable]) => { | ||
formattedVariables[key] = formatVariable({ | ||
variable, | ||
config: Variables[key], | ||
locale, | ||
}); | ||
}); | ||
|
||
const LaTeX = { | ||
peakAbsoluteMagnitude, | ||
supernovaDistance, | ||
return latex({ | ||
result: formatResult({ result, config: resultConfig, locale }), | ||
constants: formattedConstants, | ||
variables: formattedVariables, | ||
}); | ||
}; | ||
|
||
export default LaTeX; | ||
export default LaTeXComposer; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.