@borgar/fx
Version:
Utilities for working with Excel formulas
30 lines (28 loc) • 787 B
text/typescript
import type { Token, TokenEnhanced } from './types.ts';
export function cloneToken<T extends Token | TokenEnhanced> (token: T): T {
// Token
const newToken: Partial<TokenEnhanced> = {
type: token.type,
value: token.value
};
if (token.loc) {
newToken.loc = [ token.loc[0], token.loc[1] ];
}
if (token.unterminated != null) {
newToken.unterminated = token.unterminated;
}
// TokenEnhanced
if (typeof token.index === 'number') {
newToken.index = token.index;
if (typeof token.groupId === 'string') {
newToken.groupId = token.groupId;
}
if (typeof token.depth === 'number') {
newToken.depth = token.depth;
}
if (typeof token.error === 'boolean') {
newToken.error = token.error;
}
}
return newToken as T;
}