Skip to content

Commit 781cbfc

Browse files
authored
JSONDiff updates flow types for change utils (#63)
* Uses es6 modules syntax in jsondiff and dmp * Adds flowtipe definitions to change utils and jsondiff - jsondiff is only imported by change utils - jsondiff exports a single configured instance - jsondiff flowtype definitions are added in .flow.js file since jsondiff itself is a bit of a mess * Updates to package configurations
1 parent e3a4386 commit 781cbfc

File tree

12 files changed

+324
-98
lines changed

12 files changed

+324
-98
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ src/simperium/jsondiff
33
lib/
44
sample/
55
examples/
6+
flow-typed/

flow-typed/npm/uuid_v3.x.x.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// flow-typed signature: 615e568e95029d58f116dd157e320137
2+
// flow-typed version: 2b95c0dfc1/uuid_v3.x.x/flow_>=v0.32.x
3+
4+
declare module "uuid" {
5+
declare class uuid {
6+
static (
7+
options?: {|
8+
random?: number[],
9+
rng?: () => number[] | Buffer
10+
|},
11+
buffer?: number[] | Buffer,
12+
offset?: number
13+
): string,
14+
15+
static v1(
16+
options?: {|
17+
node?: number[],
18+
clockseq?: number,
19+
msecs?: number | Date,
20+
nsecs?: number
21+
|},
22+
buffer?: number[] | Buffer,
23+
offset?: number
24+
): string,
25+
26+
static v4(
27+
options?: {|
28+
random?: number[],
29+
rng?: () => number[] | Buffer
30+
|},
31+
buffer?: number[] | Buffer,
32+
offset?: number
33+
): string
34+
}
35+
declare module.exports: Class<uuid>;
36+
}
37+
38+
declare module "uuid/v1" {
39+
declare class v1 {
40+
static (
41+
options?: {|
42+
node?: number[],
43+
clockseq?: number,
44+
msecs?: number | Date,
45+
nsecs?: number
46+
|},
47+
buffer?: number[] | Buffer,
48+
offset?: number
49+
): string
50+
}
51+
52+
declare module.exports: Class<v1>;
53+
}
54+
55+
declare module "uuid/v4" {
56+
declare class v4 {
57+
static (
58+
options?: {|
59+
random?: number[],
60+
rng?: () => number[] | Buffer
61+
|},
62+
buffer?: number[] | Buffer,
63+
offset?: number
64+
): string
65+
}
66+
67+
declare module.exports: Class<v4>;
68+
}
69+
70+
declare module "uuid/v5" {
71+
declare class v5 {
72+
static (
73+
name?: string | number[],
74+
namespace?: string | number[],
75+
buffer?: number[] | Buffer,
76+
offset?: number
77+
): string
78+
}
79+
80+
declare module.exports: Class<v5>;
81+
}

package-lock.json

Lines changed: 69 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"babel-preset-env": "^1.6.1",
3333
"babel-preset-flow": "^6.23.0",
3434
"eslint": "^4.17.0",
35+
"flow-bin": "^0.66.0",
3536
"mocha": "^5.0.0",
3637
"nyc": "^11.4.1"
3738
}

src/simperium/channel.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
import { format, inherits } from 'util'
33
import { EventEmitter } from 'events'
44
import { parseMessage, parseVersionMessage, change as change_util } from './util'
5-
import JSONDiff from './jsondiff'
65
import { v4 as uuid } from 'uuid'
76

8-
const jsondiff = new JSONDiff( {list_diff: false} );
9-
107
const UNKNOWN_CV = '?';
118
const CODE_INVALID_VERSION = 405;
129
const CODE_EMPTY_RESPONSE = 412;
@@ -128,7 +125,7 @@ internal.updateObjectVersion = function( id, version, data, original, patch, ack
128125
// apply the transformed patch and emit the update
129126
if ( transformed ) {
130127
patch = transformed;
131-
update = jsondiff.apply_object_diff( data, transformed );
128+
update = change_util.apply( transformed, data );
132129
// queue up the new change
133130
change = change_util.modify( id, version, patch );
134131
this.localQueue.queue( change );
@@ -218,7 +215,7 @@ internal.applyChange = function( change, ghost ) {
218215

219216
original = ghost.data;
220217
patch = change.v;
221-
modified = jsondiff.apply_object_diff( original, patch );
218+
modified = change_util.apply( patch, original );
222219
return internal.updateObjectVersion.call( this, change.id, change.ev, modified, original, patch, acknowledged )
223220
.then( updateChangeVersion );
224221
} else if ( change.o === operation.REMOVE ) {
@@ -842,7 +839,7 @@ LocalQueue.prototype.compressAndSend = function( id, ghost ) {
842839
break;
843840
}
844841

845-
target = jsondiff.apply_object_diff( target, c.v );
842+
target = change_util.apply( c.v, target );
846843
}
847844

848845
type = target === null ? change_util.type.REMOVE : change_util.type.MODIFY;

src/simperium/jsondiff/diff_match_patch.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
module.exports = diff_match_patch;
21

32
/**
43
* Diff Match and Patch
@@ -29,7 +28,7 @@ module.exports = diff_match_patch;
2928
* Class containing the diff, match and patch methods.
3029
* @constructor
3130
*/
32-
function diff_match_patch() {
31+
export default function diff_match_patch() {
3332

3433
// Defaults.
3534
// Redefine these in your program to override the defaults.
@@ -65,9 +64,9 @@ function diff_match_patch() {
6564
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
6665
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
6766
*/
68-
var DIFF_DELETE = -1;
69-
var DIFF_INSERT = 1;
70-
var DIFF_EQUAL = 0;
67+
export const DIFF_DELETE = -1;
68+
export const DIFF_INSERT = 1;
69+
export const DIFF_EQUAL = 0;
7170

7271
/** @typedef {{0: number, 1: string}} */
7372
diff_match_patch.Diff;
@@ -2183,13 +2182,3 @@ diff_match_patch.patch_obj.prototype.toString = function() {
21832182
}
21842183
return text.join('').replace(/%20/g, ' ');
21852184
};
2186-
2187-
2188-
// Export these global variables so that they survive Google's JS compiler.
2189-
// In a browser, 'this' will be 'window'.
2190-
// Users of node.js should 'require' the uncompressed version since Google's
2191-
// JS compiler may break the following exports for non-browser environments.
2192-
module.exports['diff_match_patch'] = diff_match_patch;
2193-
module.exports['DIFF_DELETE'] = DIFF_DELETE;
2194-
module.exports['DIFF_INSERT'] = DIFF_INSERT;
2195-
module.exports['DIFF_EQUAL'] = DIFF_EQUAL;

src/simperium/jsondiff/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import JSONDiff from './jsondiff'
2-
import diff_match_patch from './diff_match_patch'
1+
// @flow
2+
import { JSONDiff } from './jsondiff'
3+
import type { ObjectOperationSet } from './jsondiff';
34

4-
export { JSONDiff as jsondiff, diff_match_patch }
5+
export type { ObjectOperationSet }
56

6-
export default function init( options ) {
7-
return new JSONDiff( options );
8-
}
7+
const jsondiff = new JSONDiff( { list_diff: false } );
8+
9+
export { jsondiff as default };

src/simperium/jsondiff/jsondiff.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
var diff_match_patch = require('./diff_match_patch');
1+
import diff_match_patch, { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from './diff_match_patch';
22

3-
// stolen from https://raw.github.com/Simperium/jsondiff/master/src/jsondiff.js
4-
(function() {
5-
var jsondiff,
6-
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
7-
__hasProp = Object.prototype.hasOwnProperty;
3+
export { jsondiff as JSONDiff };
84

9-
jsondiff = (function() {
5+
// stolen from https://raw.github.com/Simperium/jsondiff/master/src/jsondiff.js
6+
const __hasProp = Object.prototype.hasOwnProperty;
7+
const __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
108

11-
function jsondiff(options) {
9+
function jsondiff(options: ?{ list_diff: boolean } ) {
1210
this.options = options || {list_diff: true};
1311
this.patch_apply_with_offsets = __bind(this.patch_apply_with_offsets, this);
1412
this.transform_object_diff = __bind(this.transform_object_diff, this);
@@ -611,11 +609,3 @@ var diff_match_patch = require('./diff_match_patch');
611609
text = text.substring(nullPadding.length, text.length - nullPadding.length);
612610
return text;
613611
};
614-
615-
return jsondiff;
616-
617-
})();
618-
619-
module.exports = jsondiff;
620-
621-
}).call();

0 commit comments

Comments
 (0)