5.116 find series of function with specific number of terms

Doing series in Maple with specific order value, the number of terms generated ofcourse depends on the function. I had need to have the series generated always with same number of terms. I could not find an option in Maple to do that. This function does this. It keeps finding the series for the function with increasing order until the number terms that comes out is what requested. There is an upper limit that can be changed if needed to protect against pathological cases.

restart; 
 
get_series_by_terms:=proc(expr,x::symbol,at::numeric,number_terms_needed::posint) 
local keep_running::boolean:=true; 
local current_order::integer:=0; 
local MAX_ORDER_TO_TRY::posint:=100; #change as needed 
local result; 
 
    do 
        current_order := current_order+1; 
        result        := convert(series(expr,x=at,current_order),polynom); 
        if nops(result) >=  number_terms_needed or current_order>MAX_ORDER_TO_TRY then 
           keep_running:=false; 
        fi; 
    until keep_running=false; 
 
    return result; 
end proc:
 

And now

get_series_by_terms(sin(x),x,0,10)
 

returns

\begin{equation} \begin {aligned} & x -\frac {1}{6} x^{3}+\frac {1}{120} x^{5}-\frac {1}{5040} x^{7}+ \frac {1}{362880} x^{9} -\\ & \frac {1}{39916800} x^{11}+\frac {1}{6227020800} x^{13}-\frac {1}{1307674368000} x^{15}+\\ & \frac {1}{355687428096000} x^{17}-\frac {1}{121645100408832000} x^{19} \end {aligned} \end{equation}