Skip to content

feat: enhanced custom function registration#60

Merged
cawalch merged 1 commit intomainfrom
feat-enhanced-function-registration
Jul 13, 2025
Merged

feat: enhanced custom function registration#60
cawalch merged 1 commit intomainfrom
feat-enhanced-function-registration

Conversation

@cawalch
Copy link
Collaborator

@cawalch cawalch commented Jul 13, 2025

Adds enhanced function registration API with type safety and error handling.

Maintain the existing registerFunction API for backward compatibility.

import { register, search, TYPE_NUMBER } from "@jmespath-community/jmespath";

// TypeScript prevents registering built-in functions at compile time
// register('sum', myFunc, signature); // TypeScript error!

// Enhanced registration with better error handling
const result = register('multiply', ([a, b]) => a * b, [
  { types: [TYPE_NUMBER] },
  { types: [TYPE_NUMBER] }
]);

if (result.success) {
  console.log(result.message); // "Function multiply() registered successfully"
} else {
  console.error(result.message); // Detailed error information
}
import { registerFunction, register } from "@jmespath-community/jmespath";

// Option 1: Using registerFunction with options
registerFunction('myFunc', () => 'first', []);
registerFunction('myFunc', () => 'second', [], { override: true, warn: true });
// Console: "Warning: Overriding existing function: myFunc()"

// Option 2: Using enhanced register API
const result = register('myFunc', () => 'third', [], { override: true });
console.log(result.message); // "Function myFunc() overridden successfully"
import {
  isRegistered,
  getRegisteredFunctions,
  getCustomFunctions,
  unregisterFunction,
  clearCustomFunctions
} from "@jmespath-community/jmespath";

// Check if function exists
console.log(isRegistered('sum')); // true (built-in)
console.log(isRegistered('myFunc')); // true (if registered)

// Get all registered functions
const allFunctions = getRegisteredFunctions();
console.log(allFunctions); // ['abs', 'avg', 'ceil', ..., 'myFunc']

// Get only custom functions
const customFunctions = getCustomFunctions();
console.log(customFunctions); // ['myFunc', 'divide', ...]

// Unregister custom function (built-ins cannot be unregistered)
const removed = unregisterFunction('myFunc');
console.log(removed); // true if successful

// Clear all custom functions
clearCustomFunctions();
console.log(getCustomFunctions()); // []

@cawalch cawalch self-assigned this Jul 13, 2025
@cawalch cawalch requested a review from springcomp July 13, 2025 15:51
@cawalch cawalch added the enhancement New feature or request label Jul 13, 2025
Adds enhanced function registration API with type safety and error handling.

Maintain the existing `registerFunction` API for backward compatibility.

```typescript
import { register, search, TYPE_NUMBER } from "@jmespath-community/jmespath";

// TypeScript prevents registering built-in functions at compile time
// register('sum', myFunc, signature); // TypeScript error!

// Enhanced registration with better error handling
const result = register('multiply', ([a, b]) => a * b, [
  { types: [TYPE_NUMBER] },
  { types: [TYPE_NUMBER] }
]);

if (result.success) {
  console.log(result.message); // "Function multiply() registered successfully"
} else {
  console.error(result.message); // Detailed error information
}
```

```javascript
import { registerFunction, register } from "@jmespath-community/jmespath";

// Option 1: Using registerFunction with options
registerFunction('myFunc', () => 'first', []);
registerFunction('myFunc', () => 'second', [], { override: true, warn: true });
// Console: "Warning: Overriding existing function: myFunc()"

// Option 2: Using enhanced register API
const result = register('myFunc', () => 'third', [], { override: true });
console.log(result.message); // "Function myFunc() overridden successfully"
```

```javascript
import {
  isRegistered,
  getRegisteredFunctions,
  getCustomFunctions,
  unregisterFunction,
  clearCustomFunctions
} from "@jmespath-community/jmespath";

// Check if function exists
console.log(isRegistered('sum')); // true (built-in)
console.log(isRegistered('myFunc')); // true (if registered)

// Get all registered functions
const allFunctions = getRegisteredFunctions();
console.log(allFunctions); // ['abs', 'avg', 'ceil', ..., 'myFunc']

// Get only custom functions
const customFunctions = getCustomFunctions();
console.log(customFunctions); // ['myFunc', 'divide', ...]

// Unregister custom function (built-ins cannot be unregistered)
const removed = unregisterFunction('myFunc');
console.log(removed); // true if successful

// Clear all custom functions
clearCustomFunctions();
console.log(getCustomFunctions()); // []
```

<!-- ps-id: 6c0db344-9d2c-4393-8899-8d6e92a911fc -->
@cawalch cawalch force-pushed the feat-enhanced-function-registration branch from fb43cd0 to c7af8f3 Compare July 13, 2025 19:10
@springcomp
Copy link
Contributor

Thanks, that is an awesome improvement.

@cawalch cawalch merged commit 36aae73 into main Jul 13, 2025
7 checks passed
@cawalch cawalch deleted the feat-enhanced-function-registration branch July 16, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants