Skip to content

Commit

Permalink
Merge pull request #13 from formbird/special-dates
Browse files Browse the repository at this point in the history
Handle dates specially
  • Loading branch information
ranile authored Sep 4, 2023
2 parents b54fc64 + 3a2dd43 commit 1367762
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 212 deletions.
277 changes: 94 additions & 183 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ neon-serde = { git = "https://github.com/formbird/neon-serde", package = "neon-s

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.86"
serde-wasm-bindgen = "0.5.0"
serde-wasm-bindgen = { git = "https://github.com/hamza1311/serde-wasm-bindgen", branch = "special-dates", features = ["special-dates"] }

[profile.release]
# less code to include into binary
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@formbird/json-patcher",
"version": "0.5.0",
"version": "0.6.0-pre.0",
"description": "Diff Updater",
"main": "node/index.js",
"browser": "browser/index.mjs",
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ mod node;
#[cfg(not(target_arch = "wasm32"))]
pub use node::*;

pub use json_patch::{AddOperation, RemoveOperation, ReplaceOperation, MoveOperation, CopyOperation, TestOperation};
pub use json_patch::{
AddOperation, CopyOperation, MoveOperation, RemoveOperation, ReplaceOperation, TestOperation,
};

#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "wasm32")]
pub use wasm::*;
pub use wasm::*;
14 changes: 7 additions & 7 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ fn to_json_value<'a, D: DeserializeOwned>(
) -> Result<D, Throw> {
match neon_serde::from_value(cx, js_value) {
Ok(v) => Ok(v),
Err(neon_serde::errors::Error::Js(err)) => return Err(err),
Err(e) => return cx.throw_error(e.to_string()),
Err(neon_serde::errors::Error::Js(err)) => Err(err),
Err(e) => cx.throw_error(e.to_string()),
}
}

pub fn create_patch(mut cx: FunctionContext) -> JsResult<JsString> {
pub fn create_patch(mut cx: FunctionContext) -> JsResult<JsValue> {
let left = cx.argument::<JsValue>(0)?;
let left = to_json_value(&mut cx, left)?;

let right = cx.argument::<JsValue>(1)?;
let right = to_json_value(&mut cx, right)?;

let patch = json_patch::diff(&left, &right);
let s = map_to_neon!(cx, serde_json::to_string(&patch))?;
Ok(cx.string(s))
let s = map_to_neon!(cx, neon_serde::to_value(&mut cx, &patch))?;
Ok(s)
}

pub fn apply_patch(mut cx: FunctionContext) -> JsResult<JsValue> {
Expand All @@ -47,8 +47,8 @@ pub fn apply_patch(mut cx: FunctionContext) -> JsResult<JsValue> {

match neon_serde::to_value(&mut cx, &doc) {
Ok(v) => Ok(v),
Err(neon_serde::errors::Error::Js(err)) => return Err(err),
Err(e) => return cx.throw_error(e.to_string()),
Err(neon_serde::errors::Error::Js(err)) => Err(err),
Err(e) => cx.throw_error(e.to_string()),
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use wasm_bindgen::prelude::*;
use json_patch::PatchOperation;
use serde::ser::Serialize;
use wasm_bindgen::prelude::*;

fn to_value(value: impl Serialize) -> Result<JsValue, serde_wasm_bindgen::Error> {
let ser = serde_wasm_bindgen::Serializer::json_compatible();
value.serialize(&ser)
}

#[wasm_bindgen(js_name = createPatch)]
pub fn create_patch(left: JsValue, right: JsValue) -> Result<String, serde_wasm_bindgen::Error> {
pub fn create_patch(left: JsValue, right: JsValue) -> Result<JsValue, serde_wasm_bindgen::Error> {
let left = serde_wasm_bindgen::from_value(left)?;
let right = serde_wasm_bindgen::from_value(right)?;
let diff = json_patch::diff(&left, &right);
Ok(serde_json::to_string(&diff).expect("diff is valid json"))
let output = to_value(&diff)?;
Ok(output)
}

#[wasm_bindgen(js_name = applyPatch)]
pub fn apply_patch(doc: JsValue, patches: JsValue) -> Result<JsValue, serde_wasm_bindgen::Error> {
let mut doc = serde_wasm_bindgen::from_value(doc)?;
let patches: Vec<PatchOperation> = serde_wasm_bindgen::from_value(patches)?;
json_patch::patch(&mut doc, &patches).expect("todo");
let ser = serde_wasm_bindgen::Serializer::json_compatible();
doc.serialize(&ser).map_err(|e| serde_wasm_bindgen::Error::new(&e.to_string()))
to_value(doc).map_err(|e| serde_wasm_bindgen::Error::new(&e.to_string()))
}
6 changes: 0 additions & 6 deletions test/_index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions test/arrays.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'chai'
import {applyPatch} from '..'
import {createParsedPatch as createPatch} from "./_index";
import {applyPatch, createPatch} from '..'


const pairs = [
Expand Down
52 changes: 52 additions & 0 deletions test/dates.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {applyPatch, createPatch} from "..";
import {expect} from "chai";

it(`dates round trip`, () => {
const input = {
date: new Date(2023, 6, 9),
systemHeader: {
date: new Date(2023, 4, 20)
},
arr: [
new Date(2023, 4, 20),
{
date: new Date(2023, 4, 20)
},
]
} as const
const output = {
date: new Date(2023, 4, 20),
systemHeader: {
date: new Date(2023, 6, 9)
},
arr: [
new Date(2023, 6, 9),
{
date: new Date(2023, 6, 9)
}
]
} as const
const patch = createPatch(input, output)
const actualOutput = applyPatch(input, patch)

expect(actualOutput).to.deep.equal(output);
// this is a way to check that the `actualOutput.date` is actually a date.
// for some reason, after going through neon, instanceof Date check fails _here_?
// this may have something to do with jest, as in neon_serde tests with mocha, the assertion is successful
expect(actualOutput.date.toISOString()).to.equal(output.date.toISOString())
expect(actualOutput.systemHeader.date.toISOString()).to.equal(output.systemHeader.date.toISOString())
expect(actualOutput.arr[0].toISOString()).to.equal(output.arr[0].toISOString())
expect(actualOutput.arr[1].date.toISOString()).to.equal(output.arr[1].date.toISOString())

})

it(`patch includes dates`, () => {
const date = new Date()
const input = {}
const output = {
date
}
const patch = createPatch(input, output)
const op = patch[0]
expect(op.value.toISOString()).to.equal(date.toISOString())
})
3 changes: 1 addition & 2 deletions test/roundtrips.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {applyPatch, type PatchOperation} from "..";
import { createParsedPatch as createPatch } from './_index'
import {applyPatch, type PatchOperation, createPatch} from "..";
import { expect } from 'chai'

function checkRoundtrip(
Expand Down
2 changes: 1 addition & 1 deletion test/specification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ spec_data.filter(spec => spec.diffable).forEach(spec => {
runCatching(spec, () => {
const actual = createPatch(spec.input, spec.output)
const expected = spec.patch.filter(operation => operation.op !== 'test')
expect(JSON.parse(actual)).to.deep.equal(expected)
expect(actual).to.deep.equal(expected)
})
})
})

0 comments on commit 1367762

Please sign in to comment.