-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from formbird/special-dates
Handle dates specially
- Loading branch information
Showing
12 changed files
with
174 additions
and
212 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters