Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 Fix complex deep compose with keepNull=false #9

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reedsy/quill-delta",
"version": "5.1.0-reedsy.2.1.1",
"version": "5.1.0-reedsy.3.0.0",
"description": "Format for representing rich text documents and changes.",
"author": "Jason Chen <[email protected]>",
"homepage": "https://github.com/quilljs/delta",
Expand Down
29 changes: 8 additions & 21 deletions src/AttributeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@ function isObject(value: unknown): value is AttributeMap {
return value === Object(value) && !Array.isArray(value);
}

function isDeepNull(value: unknown): boolean {
if (value == null) return true;
if (!isObject(value)) return false;
for (const key in value) {
if (!isDeepNull(value[key])) return false;
}
return true;
function isEmptyObject(value: unknown): value is AttributeMap {
return isObject(value) && !Object.keys(value).length;
}

function removeNull<T>(value: T): T {
if (!value || !isObject(value)) return value;
function removeNullOrEmptyObjects<T>(value: T): void {
if (!value || !isObject(value)) return;
for (const key in value) {
const k = key as keyof typeof value;
if (value[k] == null) delete value[k];
else removeNull(value[k]);
removeNullOrEmptyObjects(value[k]);
if (value[k] == null || isEmptyObject(value[k])) delete value[k];
}
return value;
}

namespace AttributeMap {
Expand All @@ -41,22 +35,15 @@ namespace AttributeMap {
if (typeof b !== 'object') {
b = {};
}
let attributes = cloneDeep(b);
const attributes = cloneDeep(b);
for (const key in a) {
const aValue = a[key];
const attrValue = attributes[key];
if (isObject(aValue) && isObject(attrValue)) {
attributes[key] = compose(aValue, attrValue, keepNull);
}
}
if (!keepNull) {
attributes = Object.keys(attributes).reduce<AttributeMap>((copy, key) => {
if (!isDeepNull(attributes[key])) {
copy[key] = removeNull(attributes[key]);
}
return copy;
}, {});
}
if (!keepNull) removeNullOrEmptyObjects(attributes);
for (const key in a) {
if (a[key] !== undefined && b[key] === undefined) {
attributes[key] = a[key];
Expand Down
205 changes: 176 additions & 29 deletions test/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,47 +145,194 @@ describe('AttributeMap', () => {
});
});

it('composing objects with deep null and keepNull=false', function () {
expect(
AttributeMap.compose(
{
complex: {
foo: {
bar: null,
describe('keepNull=false', function () {
it('composing objects with deep null', function () {
expect(
AttributeMap.compose(
{
complex: {
foo: {
bar: null,
},
},
},
{
complex: {
foo: {
baz: 123,
},
},
},
false,
),
).toEqual({
complex: {
foo: {
baz: 123,
},
},
{
complex: {
foo: {
baz: 123,
});
});

it('composing arrays with deep null', function () {
expect(
AttributeMap.compose(
{
complex: [1, 2, 3],
},
{
complex: [null],
},
false,
),
).toEqual({
complex: [null],
});
});

it('composing object with a deep empty object', function () {
expect(
AttributeMap.compose(
{},
{
complex: {
foo: {},
bar: 123,
},
},
false,
),
).toEqual({
complex: {
bar: 123,
},
false,
),
).toEqual({
complex: {
foo: {
baz: 123,
});
});

it('composing object whose only deep value is null', function () {
expect(
AttributeMap.compose(
{
complex: {
foo: {
bar: null,
},
},
},
{
complex: {
baz: {
qux: 123,
},
},
},
false,
),
).toEqual({
complex: {
baz: {
qux: 123,
},
},
},
});
});
});

it('composing arrays with deep null and keepNull=false', function () {
expect(
AttributeMap.compose(
{
complex: [1, 2, 3],
describe('keepNull=true', function () {
it('composing objects with deep null', function () {
expect(
AttributeMap.compose(
{
complex: {
foo: {
bar: null,
},
},
},
{
complex: {
foo: {
baz: 123,
},
},
},
true,
),
).toEqual({
complex: {
foo: {
bar: null,
baz: 123,
},
},
{
complex: [null],
});
});

it('composing arrays with deep null', function () {
expect(
AttributeMap.compose(
{
complex: [1, 2, 3],
},
{
complex: [null],
},
true,
),
).toEqual({
complex: [null],
});
});

it('composing object with a deep empty object', function () {
expect(
AttributeMap.compose(
{},
{
complex: {
foo: {},
bar: 123,
},
},
true,
),
).toEqual({
complex: {
foo: {},
bar: 123,
},
false,
),
).toEqual({
complex: [null],
});
});

it('composing object whose only deep value is null', function () {
expect(
AttributeMap.compose(
{
complex: {
foo: {
bar: null,
},
},
},
{
complex: {
baz: {
qux: 123,
},
},
},
true,
),
).toEqual({
complex: {
foo: {
bar: null,
},
baz: {
qux: 123,
},
},
});
});
});
});
Expand Down