Skip to content

Commit e5af171

Browse files
authored
refactor: reduce duplication
Signed-off-by: Athan <kgryte@gmail.com>
1 parent 63b911a commit e5af171

1 file changed

Lines changed: 20 additions & 23 deletions

File tree

  • lib/node_modules/@stdlib/blas/ext/join-between/lib

lib/node_modules/@stdlib/blas/ext/join-between/lib/assign.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ function assign( x, separators, out, options ) {
8989
var opts;
9090
var sep;
9191
var shx;
92-
var sh;
92+
var shc;
93+
var flg;
9394
var M;
9495
var p;
9596
var s;
@@ -124,50 +125,46 @@ function assign( x, separators, out, options ) {
124125
}
125126
shx = getShape( x );
126127

128+
// Resolve the complement shape...
129+
if ( hasOwnProp( opts, 'dims' ) ) {
130+
shc = nonCoreShape( shx, opts.dims );
131+
flg = true;
132+
} else {
133+
shc = [];
134+
flg = false;
135+
}
127136
// Broadcast prefix to the complement shape...
128137
if ( isndarrayLike( p ) ) {
129-
if ( hasOwnProp( opts, 'dims' ) ) {
130-
prefix = maybeBroadcastArray( p, nonCoreShape( shx, opts.dims ) );
138+
if ( flg ) {
139+
prefix = maybeBroadcastArray( p, shc );
131140
} else if ( ndims( p ) === 0 ) {
132141
prefix = p;
133142
} else {
134143
throw new TypeError( format( 'invalid option. `%s` option must be a zero-dimensional ndarray when not specifying dimensions. Value: `%s`.', 'prefix', p ) );
135144
}
136145
} else {
137-
if ( hasOwnProp( opts, 'dims' ) ) {
138-
sh = nonCoreShape( shx, opts.dims );
139-
} else {
140-
sh = [];
141-
}
142-
prefix = broadcastScalar( p, 'generic', sh, getOrder( x ) );
146+
prefix = broadcastScalar( p, 'generic', shc, getOrder( x ) );
143147
}
144148
// Broadcast suffix to the complement shape...
145149
if ( isndarrayLike( s ) ) {
146-
if ( hasOwnProp( opts, 'dims' ) ) {
147-
suffix = maybeBroadcastArray( s, nonCoreShape( shx, opts.dims ) );
150+
if ( flg ) {
151+
suffix = maybeBroadcastArray( s, shc );
148152
} else if ( ndims( s ) === 0 ) {
149153
suffix = s;
150154
} else {
151155
throw new TypeError( format( 'invalid option. `%s` option must be a zero-dimensional ndarray when not specifying dimensions. Value: `%s`.', 'suffix', s ) );
152156
}
153157
} else {
154-
if ( hasOwnProp( opts, 'dims' ) ) {
155-
sh = nonCoreShape( shx, opts.dims );
156-
} else {
157-
sh = [];
158-
}
159-
suffix = broadcastScalar( s, 'generic', sh, getOrder( x ) );
158+
suffix = broadcastScalar( s, 'generic', shc, getOrder( x ) );
160159
}
161160
// Broadcast separators to [ ...complementShape, M-1 ] where M is the number of elements along the reduced dimensions...
162-
if ( hasOwnProp( opts, 'dims' ) ) {
163-
sh = nonCoreShape( shx, opts.dims );
164-
M = numel( shx ) / numel( sh );
161+
if ( flg ) {
162+
M = numel( shx ) / numel( shc );
165163
} else {
166-
sh = [];
167164
M = numel( shx );
168165
}
169-
sh.push( M - 1 );
170-
sep = maybeBroadcastArray( separators, sh );
166+
shc.push( M - 1 );
167+
sep = maybeBroadcastArray( separators, shc );
171168

172169
return base( x, prefix, suffix, sep, out, opts );
173170
}

0 commit comments

Comments
 (0)