@khanacademy/perseus-core 29.0.0 → 30.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -300,7 +300,7 @@ export type PerseusImageDetail = {
300
300
  export declare const ItemExtras: readonly ["calculator", "financialCalculatorMonthlyPayment", "financialCalculatorTotalAmount", "financialCalculatorTimeToPayOff", "periodicTable", "periodicTableWithKey"];
301
301
  export type CalculatorVariant = "scientific" | "graphing" | "four_function";
302
302
  export type PerseusAnswerArea = Record<(typeof ItemExtras)[number], boolean> & {
303
- calculatorVariant: CalculatorVariant | null;
303
+ calculatorVariant?: CalculatorVariant;
304
304
  };
305
305
  /**
306
306
  * The type representing the common structure of all widget's options. The
@@ -1385,10 +1385,10 @@ export type PerseusNumericInputWidgetOptions = {
1385
1385
  export type PerseusNumericInputAnswer = {
1386
1386
  /**
1387
1387
  * Translatable Display; A description for why this answer is correct,
1388
- * wrong, or ungraded
1388
+ * wrong, or ungraded. Always the empty string in answerless data.
1389
1389
  */
1390
1390
  message: string;
1391
- /** The expected answer */
1391
+ /** The expected answer. Null in answerless data. */
1392
1392
  value?: number | null;
1393
1393
  /** Whether this answer is "correct", "wrong", or "ungraded" */
1394
1394
  status: string;
@@ -1399,12 +1399,15 @@ export type PerseusNumericInputAnswer = {
1399
1399
  /**
1400
1400
  * Whether we should check the answer strictly against the configured
1401
1401
  * answerForms (strict = true) or include the set of default answerForms
1402
- * (strict = false).
1402
+ * (strict = false). Always false in answerless data.
1403
1403
  */
1404
1404
  strict: boolean;
1405
- /** A range of error +/- the value */
1405
+ /**
1406
+ * The maximum difference between the answer key and a correct user
1407
+ * input. Omitted in answerless data.
1408
+ */
1406
1409
  maxError?: number | null;
1407
- /** Unsimplified answers are Ungraded, Accepted, or Wrong. */
1410
+ /** How unsimplified responses should be handled. */
1408
1411
  simplify: PerseusNumericInputSimplify;
1409
1412
  };
1410
1413
  /** Options for the number-line widget. A draggable point on a number line. */
@@ -1921,17 +1924,8 @@ export type PerseusVideoWidgetOptions = {
1921
1924
  /** `static` is not used for the video widget. */
1922
1925
  static?: boolean;
1923
1926
  };
1924
- export type PerseusInputNumberAnswerType = "number" | "decimal" | "integer" | "rational" | "improper" | "mixed" | "percent" | "pi";
1925
- /** Options for the input-number widget (deprecated; prefer numeric-input). */
1926
- export type PerseusInputNumberWidgetOptions = {
1927
- answerType?: PerseusInputNumberAnswerType;
1928
- inexact?: boolean;
1929
- maxError?: number | string;
1930
- rightAlign?: boolean;
1931
- simplify: "required" | "optional" | "enforced";
1932
- size: "normal" | "small";
1933
- value: string | number;
1934
- };
1927
+ export type PerseusInputNumberAnswer = PerseusNumericInputAnswer;
1928
+ export type PerseusInputNumberWidgetOptions = PerseusNumericInputWidgetOptions;
1935
1929
  /** Options for the molecule-renderer widget. Renders a molecule via SMILES. */
1936
1930
  export type PerseusMoleculeRendererWidgetOptions = {
1937
1931
  widgetId: string;
@@ -0,0 +1 @@
1
+ export {};
@@ -74,7 +74,7 @@ const parseLegacyButtonSet=enumeration("basic","basic+div","trig","prealgebra","
74
74
 
75
75
  function versionedWidgetOptions(latestMajorVersion,parseLatest){return new VersionedWidgetOptionsParserBuilder(latestMajorVersion,parseLatest,latest=>latest,(raw,ctx)=>ctx.failure("widget options with a known version number",raw))}class VersionedWidgetOptionsParserBuilder{withMigrationFrom(majorVersion,parseOldVersion,migrateToNextVersion){const parseOtherVersions=this.parser;const migrateToLatest=old=>this.migrateToLatest(migrateToNextVersion(old));return new VersionedWidgetOptionsParserBuilder(majorVersion,parseOldVersion,migrateToLatest,parseOtherVersions)}constructor(majorVersion,parseThisVersion,migrateToLatest,parseOtherVersions){this.migrateToLatest=migrateToLatest;this.parseOtherVersions=parseOtherVersions;const parseThisVersionAndMigrateToLatest=pipeParsers(parseThisVersion).then(convert(this.migrateToLatest)).parser;this.parser=(raw,ctx)=>{if(!isPlainObject(raw)){return ctx.failure("object",raw)}const versionParseResult=parseVersionedObject(raw,ctx);if(isFailure(versionParseResult)){return versionParseResult}if(versionParseResult.value.version.major!==majorVersion){return this.parseOtherVersions(raw,ctx)}return parseThisVersionAndMigrateToLatest(raw,ctx)};}}const parseVersionedObject=object({version:defaulted(object({major:number,minor:number}),()=>({major:0,minor:0}))});
76
76
 
77
- const stringOrNumberOrNullOrUndefined=union(string).or(number).or(constant(null)).or(constant(undefined)).parser;function numberOrNullToString(v){return typeof v==="number"||v===null?String(v):v}const parsePossiblyInvalidAnswerForm=object({value:optional(string),form:defaulted(boolean,()=>false),simplify:defaulted(boolean,()=>false),considered:enumeration("correct","wrong","ungraded"),key:pipeParsers(stringOrNumberOrNullOrUndefined).then(convert(numberOrNullToString)).parser});function removeInvalidAnswerForms(possiblyInvalid){return possiblyInvalid.flatMap(answerForm=>{const{value}=answerForm;if(value!=null){return [{...answerForm,value}]}return []})}const parseAnswerForms=pipeParsers(defaulted(array(parsePossiblyInvalidAnswerForm),()=>[])).then(convert(removeInvalidAnswerForms)).parser;const version2$1=object({major:constant(2),minor:number});const parseExpressionWidgetV2=parseWidgetWithVersion(version2$1,constant("expression"),object({answerForms:parseAnswerForms,functions:array(string),times:boolean,visibleLabel:optional(string),ariaLabel:optional(string),buttonSets:parseLegacyButtonSets,buttonsVisible:optional(enumeration("always","never","focused")),extraKeys:optional(array(keypadKeys))}));const version1$2=object({major:constant(1),minor:number});const parseExpressionWidgetV1=parseWidgetWithVersion(version1$2,constant("expression"),object({answerForms:parseAnswerForms,functions:array(string),times:boolean,visibleLabel:optional(string),ariaLabel:optional(string),buttonSets:parseLegacyButtonSets,buttonsVisible:optional(enumeration("always","never","focused"))}));function migrateV1ToV2$1(widget){const{options}=widget;return {...widget,version:{major:2,minor:0},options:{times:options.times,buttonSets:options.buttonSets,functions:options.functions,buttonsVisible:options.buttonsVisible,visibleLabel:options.visibleLabel,ariaLabel:options.ariaLabel,answerForms:options.answerForms,extraKeys:deriveExtraKeys(options)??[]}}}const version0$2=optional(object({major:constant(0),minor:number}));const parseExpressionWidgetV0=parseWidgetWithVersion(version0$2,constant("expression"),object({functions:array(string),times:boolean,visibleLabel:optional(string),ariaLabel:optional(string),form:boolean,simplify:boolean,value:string,buttonSets:parseLegacyButtonSets,buttonsVisible:optional(enumeration("always","never","focused"))}));function migrateV0ToV1$3(widget){const{options}=widget;return {...widget,version:{major:1,minor:0},options:{times:options.times,buttonSets:options.buttonSets,functions:options.functions,buttonsVisible:options.buttonsVisible,visibleLabel:options.visibleLabel,ariaLabel:options.ariaLabel,answerForms:[{considered:"correct",form:options.form,simplify:options.simplify,value:options.value}]}}}const parseExpressionWidget=versionedWidgetOptions(2,parseExpressionWidgetV2).withMigrationFrom(1,parseExpressionWidgetV1,migrateV1ToV2$1).withMigrationFrom(0,parseExpressionWidgetV0,migrateV0ToV1$3).parser;
77
+ const stringOrNumberOrNullOrUndefined=union(string).or(number).or(constant(null)).or(constant(undefined)).parser;function numberOrNullToString(v){return typeof v==="number"||v===null?String(v):v}const parsePossiblyInvalidAnswerForm=object({value:optional(string),form:defaulted(boolean,()=>false),simplify:defaulted(boolean,()=>false),considered:enumeration("correct","wrong","ungraded"),key:pipeParsers(stringOrNumberOrNullOrUndefined).then(convert(numberOrNullToString)).parser});function removeInvalidAnswerForms(possiblyInvalid){return possiblyInvalid.flatMap(answerForm=>{const{value}=answerForm;if(value!=null){return [{...answerForm,value}]}return []})}const parseAnswerForms=pipeParsers(defaulted(array(parsePossiblyInvalidAnswerForm),()=>[])).then(convert(removeInvalidAnswerForms)).parser;const version2$1=object({major:constant(2),minor:number});const parseExpressionWidgetV2=parseWidgetWithVersion(version2$1,constant("expression"),object({answerForms:parseAnswerForms,functions:array(string),times:boolean,visibleLabel:optional(string),ariaLabel:optional(string),buttonSets:parseLegacyButtonSets,buttonsVisible:optional(enumeration("always","never","focused")),extraKeys:optional(array(keypadKeys))}));const version1$2=object({major:constant(1),minor:number});const parseExpressionWidgetV1=parseWidgetWithVersion(version1$2,constant("expression"),object({answerForms:parseAnswerForms,functions:array(string),times:boolean,visibleLabel:optional(string),ariaLabel:optional(string),buttonSets:parseLegacyButtonSets,buttonsVisible:optional(enumeration("always","never","focused"))}));function migrateV1ToV2$1(widget){const{options}=widget;return {...widget,version:{major:2,minor:0},options:{times:options.times,buttonSets:options.buttonSets,functions:options.functions,buttonsVisible:options.buttonsVisible,visibleLabel:options.visibleLabel,ariaLabel:options.ariaLabel,answerForms:options.answerForms,extraKeys:deriveExtraKeys(options)??[]}}}const version0$2=optional(object({major:constant(0),minor:number}));const parseExpressionWidgetV0=parseWidgetWithVersion(version0$2,constant("expression"),object({functions:array(string),times:boolean,visibleLabel:optional(string),ariaLabel:optional(string),form:boolean,simplify:boolean,value:string,buttonSets:parseLegacyButtonSets,buttonsVisible:optional(enumeration("always","never","focused"))}));function migrateV0ToV1$4(widget){const{options}=widget;return {...widget,version:{major:1,minor:0},options:{times:options.times,buttonSets:options.buttonSets,functions:options.functions,buttonsVisible:options.buttonsVisible,visibleLabel:options.visibleLabel,ariaLabel:options.ariaLabel,answerForms:[{considered:"correct",form:options.form,simplify:options.simplify,value:options.value}]}}}const parseExpressionWidget=versionedWidgetOptions(2,parseExpressionWidgetV2).withMigrationFrom(1,parseExpressionWidgetV1,migrateV1ToV2$1).withMigrationFrom(0,parseExpressionWidgetV0,migrateV0ToV1$4).parser;
78
78
 
79
79
  const parseFreeResponseWidget=parseWidget(constant("free-response"),object({allowUnlimitedCharacters:boolean,characterLimit:number,placeholder:string,question:string,scoringCriteria:array(object({text:string}))}));
80
80
 
@@ -96,7 +96,7 @@ function emptyToZero(x){return x===""?0:x}const imageDimensionToNumber=pipeParse
96
96
 
97
97
  const pairOfNumbers$2=pair(number,number);const parseImageWidget=parseWidget(constant("image"),object({title:optional(string),caption:optional(string),alt:optional(string),longDescription:optional(string),decorative:optional(boolean),backgroundImage:parsePerseusImageBackground,scale:optional(number),static:optional(boolean),labels:optional(array(object({content:string,alignment:string,coordinates:array(number)}))),range:optional(pair(pairOfNumbers$2,pairOfNumbers$2)),box:optional(pairOfNumbers$2)}));
98
98
 
99
- const booleanToString=(rawValue,ctx)=>{if(typeof rawValue==="boolean"){return ctx.success(String(rawValue))}return ctx.failure("boolean",rawValue)};const parseInputNumberWidget=parseWidget(constant("input-number"),object({answerType:optional(enumeration("number","decimal","integer","rational","improper","mixed","percent","pi")),inexact:optional(boolean),maxError:optional(union(number).or(string).parser),rightAlign:optional(boolean),simplify:enumeration("required","optional","enforced"),size:enumeration("normal","small"),value:defaulted(union(number).or(string).or(booleanToString).parser,()=>0)}));
99
+ const booleanToZero=(rawValue,ctx)=>{if(typeof rawValue==="boolean"){return ctx.success(0)}return ctx.failure("boolean",rawValue)};const parseMathFormat$1=enumeration("integer","mixed","improper","proper","decimal","percent","pi");const parseInputNumberWidgetV1=parseWidgetWithVersion(object({major:constant(1),minor:number}),constant("input-number"),object({size:string,coefficient:boolean,labelText:optional(string),textAlign:enumeration("left","center","right"),answers:array(object({value:optional(nullable(number)),status:string,message:string,answerForms:optional(array(parseMathFormat$1)),strict:boolean,maxError:optional(nullable(number)),simplify:enumeration("required","enforced","optional")}))}));function migrateV0ToV1$3(v0){const v1Options=convertInputNumberOptionsToNumericInput(v0.options);return {...v0,version:{major:1,minor:0},options:v1Options}}const parseInputNumberWidgetV0=parseWidgetWithVersion(optional(object({major:constant(0),minor:number})),constant("input-number"),object({answerType:optional(enumeration("number","decimal","integer","rational","improper","mixed","percent","pi")),inexact:optional(boolean),maxError:optional(union(number).or(string).parser),rightAlign:optional(boolean),simplify:enumeration("required","optional","enforced"),size:enumeration("normal","small"),value:defaulted(union(number).or(string).or(booleanToZero).parser,()=>0)}));function convertInputNumberOptionsToNumericInput(inputNumberOptions){return {coefficient:false,textAlign:inputNumberOptions.rightAlign?"right":"left",size:inputNumberOptions.size,answers:[{status:"correct",value:Number(inputNumberOptions.value),simplify:inputNumberOptions.simplify,message:"",maxError:getMaxError(inputNumberOptions),strict:true,answerForms:getAnswerForms(inputNumberOptions)}]}}function getMaxError(inputNumberOptions){if(!inputNumberOptions.inexact){return 0}if(inputNumberOptions.maxError==null){return undefined}return Number(inputNumberOptions.maxError)}const mathFormatsForAnswerType={number:[],decimal:["decimal"],integer:["integer"],rational:["integer","proper","improper","mixed"],improper:["integer","proper","improper"],mixed:["integer","proper","mixed"],percent:["integer","decimal","proper","improper","mixed","percent"],pi:["pi"]};function getAnswerForms(options){const value=Number(options.value);const{inexact}=options;const precision=1e10;const rounded=Math.round(value*precision)/precision;const answerType=options.answerType??"number";if(answerType==="number"&&!inexact&&!equalFloats(rounded,value)){return ["proper","improper","mixed"]}return mathFormatsForAnswerType[answerType]}function equalFloats(a,b){return Math.abs(a-b)<Math.pow(2,-42)}const parseInputNumberWidget=versionedWidgetOptions(1,parseInputNumberWidgetV1).withMigrationFrom(0,parseInputNumberWidgetV0,migrateV0ToV1$3).parser;
100
100
 
101
101
  const pairOfNumbers$1=pair(number,number);const stringOrEmpty=defaulted(string,()=>"");const parseKey=pipeParsers(optional(string)).then(convert(String)).parser;const parseFunctionElement=object({type:constant("function"),key:parseKey,options:object({value:string,funcName:string,rangeMin:string,rangeMax:string,color:string,strokeDasharray:string,strokeWidth:number})});const parseLabelElement=object({type:constant("label"),key:parseKey,options:object({label:string,color:string,coordX:string,coordY:string})});const parseLineElement=object({type:constant("line"),key:parseKey,options:object({color:string,startX:string,startY:string,endX:string,endY:string,strokeDasharray:string,strokeWidth:number,arrows:string})});const parseMovableLineElement=object({type:constant("movable-line"),key:parseKey,options:object({startX:string,startY:string,startSubscript:number,endX:string,endY:string,endSubscript:number,constraint:string,snap:number,constraintFn:string,constraintXMin:string,constraintXMax:string,constraintYMin:string,constraintYMax:string})});const parseMovablePointElement=object({type:constant("movable-point"),key:parseKey,options:object({startX:string,startY:string,varSubscript:number,constraint:string,snap:number,constraintFn:string,constraintXMin:stringOrEmpty,constraintXMax:stringOrEmpty,constraintYMin:stringOrEmpty,constraintYMax:stringOrEmpty})});const parseParametricElement=object({type:constant("parametric"),key:parseKey,options:object({x:string,y:string,rangeMin:string,rangeMax:string,color:string,strokeDasharray:string,strokeWidth:number})});const parsePointElement=object({type:constant("point"),key:parseKey,options:object({color:string,coordX:string,coordY:string})});const parseRectangleElement=object({type:constant("rectangle"),key:parseKey,options:object({color:string,coordX:string,coordY:string,width:string,height:string})});const parseInteractionWidget=parseWidget(constant("interaction"),object({static:defaulted(boolean,()=>false),graph:object({editableSettings:optional(array(enumeration("canvas","graph"))),box:pairOfNumbers$1,labels:array(string),range:pair(pairOfNumbers$1,pairOfNumbers$1),gridStep:pairOfNumbers$1,markings:enumeration("graph","grid","none","axes"),snapStep:optional(pairOfNumbers$1),valid:optional(union(boolean).or(string).parser),backgroundImage:optional(parsePerseusImageBackground),showProtractor:optional(boolean),showRuler:optional(boolean),rulerLabel:optional(string),rulerTicks:optional(number),tickStep:pairOfNumbers$1}),elements:array(discriminatedUnionOn("type").withBranch("function",parseFunctionElement).withBranch("label",parseLabelElement).withBranch("line",parseLineElement).withBranch("movable-line",parseMovableLineElement).withBranch("movable-point",parseMovablePointElement).withBranch("parametric",parseParametricElement).withBranch("point",parsePointElement).withBranch("rectangle",parseRectangleElement).parser)}));
102
102
 
@@ -144,7 +144,7 @@ union(parsePerseusRenderer).or(array(parsePerseusRenderer)).parser;
144
144
 
145
145
  const parseHint=object({replace:defaulted(optional(boolean),()=>undefined),placeholder:defaulted(optional(boolean),()=>undefined),content:string,widgets:defaulted(parseWidgetsMap,()=>({})),images:parseImages,metadata:any});
146
146
 
147
- const booleanOrFalse=defaulted(boolean,()=>false);const nullableCalculatorVariant=defaulted(nullable(enumeration("scientific","graphing","four_function")),()=>null);const baseParser=defaulted(object({calculator:booleanOrFalse,calculatorVariant:nullableCalculatorVariant,financialCalculatorMonthlyPayment:booleanOrFalse,financialCalculatorTotalAmount:booleanOrFalse,financialCalculatorTimeToPayOff:booleanOrFalse,periodicTable:booleanOrFalse,periodicTableWithKey:booleanOrFalse}),()=>({calculator:false,calculatorVariant:null,financialCalculatorMonthlyPayment:false,financialCalculatorTotalAmount:false,financialCalculatorTimeToPayOff:false,periodicTable:false,periodicTableWithKey:false}));const parsePerseusAnswerArea=pipeParsers(baseParser).then(convert(parsed=>{const calculatorVariant=parsed.calculator&&parsed.calculatorVariant===null?"scientific":parsed.calculatorVariant;return {...parsed,calculatorVariant}})).parser;
147
+ const booleanOrFalse=defaulted(boolean,()=>false);const calculatorVariantOrUndefined=pipeParsers(defaulted(nullable(enumeration("scientific","graphing","four_function")),()=>null)).then(convert(v=>v??undefined)).parser;const baseParser=defaulted(object({calculator:booleanOrFalse,calculatorVariant:calculatorVariantOrUndefined,financialCalculatorMonthlyPayment:booleanOrFalse,financialCalculatorTotalAmount:booleanOrFalse,financialCalculatorTimeToPayOff:booleanOrFalse,periodicTable:booleanOrFalse,periodicTableWithKey:booleanOrFalse}),()=>({calculator:false,calculatorVariant:undefined,financialCalculatorMonthlyPayment:false,financialCalculatorTotalAmount:false,financialCalculatorTimeToPayOff:false,periodicTable:false,periodicTableWithKey:false}));const parsePerseusAnswerArea=pipeParsers(baseParser).then(convert(parsed=>{const{calculatorVariant:parsedCalcVariant,...rest}=parsed;const calculatorVariant=parsed.calculator&&parsedCalcVariant===undefined?"scientific":parsedCalcVariant;return calculatorVariant!==undefined?{...rest,calculatorVariant}:rest})).parser;
148
148
 
149
149
  const parsePerseusItem=object({question:parsePerseusRenderer,hints:defaulted(array(parseHint),()=>[]),answerArea:parsePerseusAnswerArea});
150
150