Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- Added a configuration option `context` that can be used to pass data to custom functions.

## [2.6.2] - 2024-02-15

### Changed
Expand Down
13 changes: 13 additions & 0 deletions src/Config.ts
Comment thread
michaelbridge marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export interface ConfigParams {
* @category Engine
*/
chooseAddressMappingPolicy: ChooseAddressMapping,
/**
* A generic parameter that can be used to pass data to custom functions.
*
* @default undefined
*
* @category Engine
*/
context: unknown,
/**
* Sets symbols that denote currency numbers.
*
Expand Down Expand Up @@ -494,6 +502,7 @@ export class Config implements ConfigParams, ParserConfig {
currencySymbol: ['$'],
caseSensitive: false,
caseFirst: 'lower',
context: undefined,
chooseAddressMappingPolicy: new AlwaysDense(),
dateFormats: ['DD/MM/YYYY', 'DD/MM/YY'],
decimalSeparator: '.',
Expand Down Expand Up @@ -596,6 +605,8 @@ export class Config implements ConfigParams, ParserConfig {
public readonly currencySymbol: string[]
/** @inheritDoc */
public readonly undoLimit: number
/** @inheritDoc */
public readonly context: unknown
/**
* Built automatically based on translation package.
*
Expand Down Expand Up @@ -623,6 +634,7 @@ export class Config implements ConfigParams, ParserConfig {
caseSensitive,
caseFirst,
chooseAddressMappingPolicy,
context,
currencySymbol,
dateFormats,
decimalSeparator,
Expand Down Expand Up @@ -709,6 +721,7 @@ export class Config implements ConfigParams, ParserConfig {
this.maxColumns = configValueFromParam(maxColumns, 'number', 'maxColumns')
this.currencySymbol = this.setupCurrencySymbol(currencySymbol)
validateNumberToBeAtLeast(this.maxColumns, 'maxColumns', 1)
this.context = context

privatePool.set(this, {
licenseKeyValidityState: checkLicenseKeyValidity(this.licenseKey)
Expand Down
6 changes: 6 additions & 0 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ describe('Config', () => {
expect(() => new Config({caseFirst: 'abcd'})).toThrowError('Expected one of \'upper\' \'lower\' \'false\' for config parameter: caseFirst')
})

it('should return the specified context', () => {
const context ={a: 1, b: 2}
const config = new Config({context})
expect(config.context).toBe(context)
})

it('should throw error when Function Translation cannot be found', () => {
const config = new Config()
const functionName = '0123456ABCDEFGH'
Comment thread
michaelbridge marked this conversation as resolved.
Resolved
Expand Down
28 changes: 27 additions & 1 deletion test/custom-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class GreetingsPlugin extends FunctionPlugin {
EXAMPLE_ARRAY_FUNCTION: 'EXAMPLE_ARRAY_FUNCTION',
}
}

greet(ast: ProcedureAst, state: InterpreterState) {
return this.runFunction(
ast.args,
Expand Down Expand Up @@ -219,6 +219,18 @@ class GreetingsPlugin extends FunctionPlugin {
}
}

class ContextPlugin extends FunctionPlugin {
public static implementedFunctions = {
'GETCONTEXT': {
method: 'getContext',
}
}

public getContext(ast: ProcedureAst, state: InterpreterState): unknown {
return this.config.context
}
}

describe('Register static custom plugin', () => {
it('should register plugin with translations', () => {
HyperFormula.registerLanguage('plPL', plPL)
Expand Down Expand Up @@ -552,3 +564,17 @@ describe('Custom function implemented with runFunction)', () => {
}).toThrow(new Error('Function returning array cannot be vectorized.'))
})
})

describe('Context accessible within custom function', () => {
it('works', () => {
HyperFormula.registerFunctionPlugin(ContextPlugin)
HyperFormula.getLanguage('enGB').extendFunctions({GETCONTEXT: 'GETCONTEXT'})

const context = 'abc'
const engine = HyperFormula.buildFromArray([
['=GETCONTEXT()'],
], {context})

expect(engine.getCellValue(adr('A1'))).toEqual(context)
})
})