@@ -129,7 +129,6 @@ export function expandFunction(
129129 //
130130 if ( h === 'Divide' ) {
131131 const num = expand ( ops [ 0 ] ) ;
132- if ( num === null ) return null ;
133132 if ( isFunction ( num , 'Add' ) )
134133 return add ( ...num . ops . map ( ( x ) => x . div ( ops [ 1 ] ) ) ) ;
135134 return ce . _fn ( 'Divide' , [ num , ops [ 1 ] ] ) ;
@@ -160,14 +159,14 @@ export function expandFunction(
160159 //
161160 // Negate
162161 //
163- if ( h === 'Negate' ) return expand ( ops [ 0 ] ) ? .neg ( ) ?? null ;
162+ if ( h === 'Negate' ) return expand ( ops [ 0 ] ) . neg ( ) ;
164163
165164 //
166165 //
167166 // Add
168167 //
169168
170- if ( h === 'Add' ) return add ( ...ops . map ( ( x ) => expand ( x ) ?? x ) ) ;
169+ if ( h === 'Add' ) return add ( ...ops . map ( ( x ) => expand ( x ) ) ) ;
171170
172171 //
173172 // Power
@@ -188,11 +187,11 @@ export function expandFunction(
188187 * If the exression is a relational operator, expand the operands.
189188 * Return null if the expression cannot be expanded.
190189 */
191- export function expand ( expr : Expression | undefined ) : Expression | null {
190+ export function expand ( expr : Expression ) : Expression {
192191 // To expand an expression, we need to use its canonical form
193- expr = expr ? .canonical ;
192+ expr = expr . canonical ;
194193
195- if ( ! expr || typeof expr . operator !== 'string' ) return null ;
194+ if ( typeof expr . operator !== 'string' ) return expr ;
196195
197196 //
198197 // Expand relational operators
@@ -201,14 +200,16 @@ export function expand(expr: Expression | undefined): Expression | null {
201200 const ops = isFunction ( expr ) ? expr . ops : [ ] ;
202201 return expr . engine . _fn (
203202 expr . operator ,
204- ops . map ( ( x ) => expand ( x ) ?? x )
203+ ops . map ( ( x ) => expand ( x ) )
205204 ) ;
206205 }
207206
208- return expandFunction (
209- expr . engine ,
210- expr . operator ,
211- isFunction ( expr ) ? expr . ops : [ ]
207+ return (
208+ expandFunction (
209+ expr . engine ,
210+ expr . operator ,
211+ isFunction ( expr ) ? expr . ops : [ ]
212+ ) ?? expr
212213 ) ;
213214}
214215
@@ -217,11 +218,11 @@ export function expand(expr: Expression | undefined): Expression | null {
217218 *
218219 * `expand()` only expands the top level of the expression.
219220 */
220- export function expandAll ( expr : Expression ) : Expression | null {
221- if ( ! expr . operator || ! isFunction ( expr ) ) return null ;
221+ export function expandAll ( expr : Expression ) : Expression {
222+ if ( ! expr . operator || ! isFunction ( expr ) ) return expr ;
222223
223- const ops = expr . ops . map ( ( x ) => expandAll ( x ) ?? x ) ;
224+ const ops = expr . ops . map ( ( x ) => expandAll ( x ) ) ;
224225
225226 const result = expr . engine . function ( expr . operator , ops ) ;
226- return expand ( result ) ?? result ;
227+ return expand ( result ) ;
227228}
0 commit comments