Skip to content

Commit cc304ee

Browse files
committed
Reformat code in quill-delta
1 parent ea2478e commit cc304ee

File tree

6 files changed

+1109
-1030
lines changed

6 files changed

+1109
-1030
lines changed

package-lock.json

Lines changed: 8 additions & 0 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
@@ -62,6 +62,7 @@
6262
"@testing-library/react": "14.3.0",
6363
"@testing-library/react-native": "12.4.3",
6464
"@testing-library/user-event": "14.4.3",
65+
"@types/diff": "7.0.2",
6566
"@types/eslint": "8.56.9",
6667
"@types/estree": "1.0.5",
6768
"@types/istanbul-lib-report": "3.0.0",

packages/sync/src/quill-delta/AttributeMap.ts

Lines changed: 105 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,122 @@
22
// - lodash.clonedeep is replaced with JSON parse / stringify
33
// - lodash.isequal is replaced with fast-deep-equal.
44

5+
/**
6+
* External dependencies
7+
*/
58
import { default as isEqual } from 'fast-deep-equal/es6';
69

7-
function cloneDeep<T>(value: T): T {
8-
return JSON.parse(JSON.stringify(value)) as T;
10+
function cloneDeep< T >( value: T ): T {
11+
return JSON.parse( JSON.stringify( value ) ) as T;
912
}
1013

1114
interface AttributeMap {
12-
[key: string]: unknown;
15+
[ key: string ]: unknown;
1316
}
1417

1518
namespace AttributeMap {
16-
export function compose(
17-
a: AttributeMap = {},
18-
b: AttributeMap = {},
19-
keepNull = false,
20-
): AttributeMap | undefined {
21-
if (typeof a !== 'object') {
22-
a = {};
23-
}
24-
if (typeof b !== 'object') {
25-
b = {};
26-
}
27-
let attributes = cloneDeep(b);
28-
if (!keepNull) {
29-
attributes = Object.keys(attributes).reduce<AttributeMap>((copy, key) => {
30-
if (attributes[key] != null) {
31-
copy[key] = attributes[key];
32-
}
33-
return copy;
34-
}, {});
35-
}
36-
for (const key in a) {
37-
if (a[key] !== undefined && b[key] === undefined) {
38-
attributes[key] = a[key];
39-
}
40-
}
41-
return Object.keys(attributes).length > 0 ? attributes : undefined;
42-
}
19+
export function compose(
20+
a: AttributeMap = {},
21+
b: AttributeMap = {},
22+
keepNull = false
23+
): AttributeMap | undefined {
24+
if ( typeof a !== 'object' ) {
25+
a = {};
26+
}
27+
if ( typeof b !== 'object' ) {
28+
b = {};
29+
}
30+
let attributes = cloneDeep( b );
31+
if ( ! keepNull ) {
32+
attributes = Object.keys( attributes ).reduce< AttributeMap >(
33+
( copy, key ) => {
34+
if ( attributes[ key ] !== null ) {
35+
copy[ key ] = attributes[ key ];
36+
}
37+
return copy;
38+
},
39+
{}
40+
);
41+
}
42+
for ( const key in a ) {
43+
if ( a[ key ] !== undefined && b[ key ] === undefined ) {
44+
attributes[ key ] = a[ key ];
45+
}
46+
}
47+
return Object.keys( attributes ).length > 0 ? attributes : undefined;
48+
}
4349

44-
export function diff(
45-
a: AttributeMap = {},
46-
b: AttributeMap = {},
47-
): AttributeMap | undefined {
48-
if (typeof a !== 'object') {
49-
a = {};
50-
}
51-
if (typeof b !== 'object') {
52-
b = {};
53-
}
54-
const attributes = Object.keys(a)
55-
.concat(Object.keys(b))
56-
.reduce<AttributeMap>((attrs, key) => {
57-
if (!isEqual(a[key], b[key])) {
58-
attrs[key] = b[key] === undefined ? null : b[key];
59-
}
60-
return attrs;
61-
}, {});
62-
return Object.keys(attributes).length > 0 ? attributes : undefined;
63-
}
50+
export function diff(
51+
a: AttributeMap = {},
52+
b: AttributeMap = {}
53+
): AttributeMap | undefined {
54+
if ( typeof a !== 'object' ) {
55+
a = {};
56+
}
57+
if ( typeof b !== 'object' ) {
58+
b = {};
59+
}
60+
const attributes = Object.keys( a )
61+
.concat( Object.keys( b ) )
62+
.reduce< AttributeMap >( ( attrs, key ) => {
63+
if ( ! isEqual( a[ key ], b[ key ] ) ) {
64+
attrs[ key ] = b[ key ] === undefined ? null : b[ key ];
65+
}
66+
return attrs;
67+
}, {} );
68+
return Object.keys( attributes ).length > 0 ? attributes : undefined;
69+
}
6470

65-
export function invert(
66-
attr: AttributeMap = {},
67-
base: AttributeMap = {},
68-
): AttributeMap {
69-
attr = attr || {};
70-
const baseInverted = Object.keys(base).reduce<AttributeMap>((memo, key) => {
71-
if (base[key] !== attr[key] && attr[key] !== undefined) {
72-
memo[key] = base[key];
73-
}
74-
return memo;
75-
}, {});
76-
return Object.keys(attr).reduce<AttributeMap>((memo, key) => {
77-
if (attr[key] !== base[key] && base[key] === undefined) {
78-
memo[key] = null;
79-
}
80-
return memo;
81-
}, baseInverted);
82-
}
71+
export function invert(
72+
attr: AttributeMap = {},
73+
base: AttributeMap = {}
74+
): AttributeMap {
75+
attr = attr || {};
76+
const baseInverted = Object.keys( base ).reduce< AttributeMap >(
77+
( memo, key ) => {
78+
if (
79+
base[ key ] !== attr[ key ] &&
80+
attr[ key ] !== undefined
81+
) {
82+
memo[ key ] = base[ key ];
83+
}
84+
return memo;
85+
},
86+
{}
87+
);
88+
return Object.keys( attr ).reduce< AttributeMap >( ( memo, key ) => {
89+
if ( attr[ key ] !== base[ key ] && base[ key ] === undefined ) {
90+
memo[ key ] = null;
91+
}
92+
return memo;
93+
}, baseInverted );
94+
}
8395

84-
export function transform(
85-
a: AttributeMap | undefined,
86-
b: AttributeMap | undefined,
87-
priority = false,
88-
): AttributeMap | undefined {
89-
if (typeof a !== 'object') {
90-
return b;
91-
}
92-
if (typeof b !== 'object') {
93-
return undefined;
94-
}
95-
if (!priority) {
96-
return b; // b simply overwrites us without priority
97-
}
98-
const attributes = Object.keys(b).reduce<AttributeMap>((attrs, key) => {
99-
if (a[key] === undefined) {
100-
attrs[key] = b[key]; // null is a valid value
101-
}
102-
return attrs;
103-
}, {});
104-
return Object.keys(attributes).length > 0 ? attributes : undefined;
105-
}
96+
export function transform(
97+
a: AttributeMap | undefined,
98+
b: AttributeMap | undefined,
99+
priority = false
100+
): AttributeMap | undefined {
101+
if ( typeof a !== 'object' ) {
102+
return b;
103+
}
104+
if ( typeof b !== 'object' ) {
105+
return undefined;
106+
}
107+
if ( ! priority ) {
108+
return b; // b simply overwrites us without priority
109+
}
110+
const attributes = Object.keys( b ).reduce< AttributeMap >(
111+
( attrs, key ) => {
112+
if ( a[ key ] === undefined ) {
113+
attrs[ key ] = b[ key ]; // null is a valid value
114+
}
115+
return attrs;
116+
},
117+
{}
118+
);
119+
return Object.keys( attributes ).length > 0 ? attributes : undefined;
120+
}
106121
}
107122

108123
export default AttributeMap;

0 commit comments

Comments
 (0)