hyperformula
Version:
HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas
43 lines • 1.17 kB
JavaScript
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import { CellError } from "../../Cell.mjs";
import { FunctionArgumentType, FunctionPlugin } from "./FunctionPlugin.mjs";
/**
* Interpreter plugin containing COUNTUNIQUE function
*/
export class CountUniquePlugin extends FunctionPlugin {
/**
* Corresponds to COUNTUNIQUE(Number1, Number2, ...).
*
* Returns number of unique numbers from arguments
*
* @param ast
* @param state
*/
countunique(ast, state) {
return this.runFunction(ast.args, state, this.metadata('COUNTUNIQUE'), (...args) => {
const valuesSet = new Set();
const errorsSet = new Set();
for (const scalarValue of args) {
if (scalarValue instanceof CellError) {
errorsSet.add(scalarValue.type);
} else if (scalarValue !== '') {
valuesSet.add(scalarValue);
}
}
return valuesSet.size + errorsSet.size;
});
}
}
CountUniquePlugin.implementedFunctions = {
'COUNTUNIQUE': {
method: 'countunique',
parameters: [{
argumentType: FunctionArgumentType.SCALAR
}],
repeatLastArgs: 1,
expandRanges: true
}
};