Skip to content

Commit

Permalink
Merge pull request #277 from webitel/feat/update-apply-transform-method
Browse files Browse the repository at this point in the history
feature: Update apply transform function to add withContext config
  • Loading branch information
dlohvinov authored Aug 15, 2024
2 parents d3ebbf8 + e0ef602 commit 0ca98f6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
26 changes: 26 additions & 0 deletions docs/pages/webitel-ui/api/transformers/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,29 @@ starToSearch('search')({ search: 'string' });
// => { search: 'string*' }
starToSearch('search')({ search: 'string*' });
```

## withContext

You can pass withContext option to pass context into transformer function as a second argument.

example:
```js
const context = {
value: '1'
// ...
}

convert = () => {
return applyTransform({}, [
transformSchema
], {
withContext: context,
})
}
```

How it passes context into transformer function:
```js
transformer(result, withContext)
```
That give you option to work with node context into transformer.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webitel/ui-sdk",
"version": "24.8.23",
"version": "24.8.24",
"private": false,
"scripts": {
"dev": "vite",
Expand Down
7 changes: 6 additions & 1 deletion src/api/transformers/applyTransform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const applyTransform = (target, transformers, { debug = false } = {}) => {
const applyTransform = (target, transformers, { debug = false, withContext = null } = {}) => {
return transformers.reduce((result, transformer, index) => {
if (debug) console.info(`applyTransform debug on step ${index}`, result);

if (withContext) {
return transformer(result, withContext);
}

return transformer(result);
}, target);
};
Expand Down

0 comments on commit 0ca98f6

Please sign in to comment.