Skip to content

Commit

Permalink
fixed readme
Browse files Browse the repository at this point in the history
  • Loading branch information
phhu committed Apr 25, 2022
1 parent 788471f commit 89cad6f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ reviveFunctionsInObjectCurried(options)(jsonLikeObject)(data)
```

* `options` is an object with three keys:
* `functions` is an object containing key value pairs of function names and function bodies. Defaults to `{}`
* `functions` is an object containing key value pairs of function names and function bodies. Defaults to `{}`. It can be useful to pass selected functions from libraries such as Ramda or lodash/fp.
* `addFunctionTag` is a function which, given a function name (e.g. `get`), returns a function tag (e.g. `$get`). Defaults to `name=>"$"+name`, i.e. using "$" as a function tag.
* `stringifyFirst`: for `reviveFunctionsInObject` and `reviveFunctionsInObjectCurried`, allows explicit determination of whether to run `JSON.stringify` on the input value, before passing to `JSON.parse`. Defaults to `undefined`, meaning this is done on objects only.
* `data` an optional Javascript value to be passed as a curried last argument to `functions`
* `jsonLikeObject` is a JSON-like Javascript structure, or a string containing valid JSON, in objects containing function specifications like `{$functionname: [param1, param2, ...]}` will be substituted with the result of calling the function with the specified parameters (and, optionally, the data object, applied as a last, curried argument). If only one parameter is needed, the array can be omitted (e.g. `{$functionname: param1}`). Parameters can be generated recursively, from nested function calls (see examples below).
* `jsonString` is a string as would normally be passed to JSON.parse
* `jsonLikeObject` is a JSON-like Javascript structure, or a string containing valid JSON, in which objects containing function specifications like `{$functionname: [param1, param2, ...]}` will be substituted with the result of calling the function with the specified parameters (and, optionally, the data object, applied as a last, curried argument). If only one parameter is needed, the array can be omitted (e.g. `{$functionname: param1}`). Parameters can be generated recursively, from nested function calls (see examples below). The whole JSON-like tree is parsed. Items which are not recognised as function specifications are passed through unchanged, as per normal `JSON.parse` behaviour.
* `jsonString` is a string as would normally be passed to `JSON.parse`

## Examples

Expand Down Expand Up @@ -59,7 +59,7 @@ const res2 = reviveFunctionsInObject({
addFunctionTag: f => 'fn::' + f,
},
{
sum: {"fn::add": [2,{"fn::get": "test"}] },
sum: {"fn::add": [2, {"fn::get": "test"}] },
unchanged: "other values get passed through"
},
{test: 42}
Expand Down Expand Up @@ -91,4 +91,6 @@ const res3 = data.map(reviver);

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse for details of `JSON.parse` and its reviver function parameter.

See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html for an example of this kind of substitution being used in Amazon Web Services (AWS) Cloudformation.
See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html for an example of this kind of substitution being used in Amazon Web Services (AWS) Cloudformation.

NPM: https://www.npmjs.com/package/revive-functions

0 comments on commit 89cad6f

Please sign in to comment.