Skip to content

Commit

Permalink
fix(types): added support for arrays as paths in operations' "from" (…
Browse files Browse the repository at this point in the history
…copy/move)
  • Loading branch information
grantila committed Apr 25, 2022
1 parent 69d7c6e commit f0c5f1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -83,6 +83,12 @@ The options are:
- `whitespace` ('auto' | 'tabs' | number): Specifies whitespace strategy. Defaults to 'auto'. Force tabs using 'tabs' or spaces using number (e.g. 2 or 4).
- `ordered` (boolean): Try to insert new properties in order.

### RFC6902

By the spec RFC6902, the `path` property of an operation (and the `from` in `move` and `copy` operations) must be a well-formed JSON Pointer, with encoded path segments. [`jsonpos`](https://github.com/grantila/jsonpos/#json-pointer-paths) exposes helpers for this.

To be more practical for programmatic usage, this package allows not only JSON Pointer strings as paths, but also arrays of (raw unencoded) strings. So you can use e.g. `"/foo/bar"` or `["foo", "bar"]`, whichever you prefer. For path segments containing e.g. a slash, it would be `"/f~1o~1o/bar"` or `["f/o/o", "bar"]`.


[npm-image]: https://img.shields.io/npm/v/edit-json.svg
[npm-url]: https://npmjs.org/package/edit-json
Expand Down
16 changes: 13 additions & 3 deletions lib/types-rfc6902.ts
@@ -1,10 +1,12 @@
type ExtendedPath = string | string[ ];

interface OperationBase
{
/**
* An extended RFC6901 path which is either a JSON Pointer, or an array of
* unencoded path segments.
*/
path: string | string[ ];
path: ExtendedPath;
}

export interface AddOperation extends OperationBase
Expand All @@ -27,13 +29,21 @@ export interface ReplaceOperation extends OperationBase
export interface MoveOperation extends OperationBase
{
op: 'move';
from: string;
/**
* An extended RFC6901 path which is either a JSON Pointer, or an array of
* unencoded path segments.
*/
from: ExtendedPath;
}

export interface CopyOperation extends OperationBase
{
op: 'copy';
from: string;
/**
* An extended RFC6901 path which is either a JSON Pointer, or an array of
* unencoded path segments.
*/
from: ExtendedPath;
}

export interface TestOperation extends OperationBase
Expand Down

0 comments on commit f0c5f1c

Please sign in to comment.