Skip to content
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
9 changes: 0 additions & 9 deletions spec/core/grid/GridJsonStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('GridJsonStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
Expand Down Expand Up @@ -100,11 +99,9 @@ describe('GridJsonStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -125,11 +122,9 @@ describe('GridJsonStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -151,11 +146,9 @@ describe('GridJsonStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -179,11 +172,9 @@ describe('GridJsonStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand Down
14 changes: 1 addition & 13 deletions spec/core/grid/GridJsonStringStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ describe('GridJsonStringStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand Down Expand Up @@ -102,11 +100,9 @@ describe('GridJsonStringStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -127,11 +123,9 @@ describe('GridJsonStringStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -153,11 +147,9 @@ describe('GridJsonStringStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -176,15 +168,13 @@ describe('GridJsonStringStore', () => {
expect(store.toJSONString()).toBe(
JSON.stringify({
_kind: Kind.Grid,
meta: { ver: '4.0', foo: 'bar' },
meta: { foo: 'bar', ver: '4.0' },
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -208,11 +198,9 @@ describe('GridJsonStringStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand Down
14 changes: 1 addition & 13 deletions spec/core/grid/GridJsonUint8ArrayStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ describe('GridJsonUint8ArrayStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand Down Expand Up @@ -104,11 +102,9 @@ describe('GridJsonUint8ArrayStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -129,11 +125,9 @@ describe('GridJsonUint8ArrayStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -155,11 +149,9 @@ describe('GridJsonUint8ArrayStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -178,15 +170,13 @@ describe('GridJsonUint8ArrayStore', () => {
expect(store.toJSONString()).toBe(
JSON.stringify({
_kind: Kind.Grid,
meta: { ver: '4.0', foo: 'bar' },
meta: { foo: 'bar', ver: '4.0' },
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -210,11 +200,9 @@ describe('GridJsonUint8ArrayStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand Down
6 changes: 0 additions & 6 deletions spec/core/grid/GridObjStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ describe('GridObjStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -114,11 +112,9 @@ describe('GridObjStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand All @@ -142,11 +138,9 @@ describe('GridObjStore', () => {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'boo',
meta: {},
},
],
rows: [
Expand Down
22 changes: 8 additions & 14 deletions spec/core/grid/HGrid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ describe('HGrid', function (): void {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'goo',
meta: {},
},
],
rows: [
Expand All @@ -348,11 +346,9 @@ describe('HGrid', function (): void {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'goo',
meta: {},
},
],
rows: [
Expand All @@ -379,11 +375,9 @@ describe('HGrid', function (): void {
cols: [
{
name: 'foo',
meta: {},
},
{
name: 'goo',
meta: {},
},
],
rows: [
Expand Down Expand Up @@ -1587,21 +1581,21 @@ type,val
grid.reorderColumns(['a', 'b', 'c', 'd'])

expect(grid.toJSON().cols).toEqual([
{ name: 'a', meta: {} },
{ name: 'b', meta: {} },
{ name: 'c', meta: {} },
{ name: 'd', meta: {} },
{ name: 'a' },
{ name: 'b' },
{ name: 'c' },
{ name: 'd' },
])
})

it('reorders the columns alphabetically in reverse', function (): void {
grid.reorderColumns(['d', 'c', 'b', 'a'])

expect(grid.toJSON().cols).toEqual([
{ name: 'd', meta: {} },
{ name: 'c', meta: {} },
{ name: 'b', meta: {} },
{ name: 'a', meta: {} },
{ name: 'd' },
{ name: 'c' },
{ name: 'b' },
{ name: 'a' },
])
})
}) // #reorderColumns()
Expand Down
41 changes: 33 additions & 8 deletions src/core/grid/GridJsonStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class GridJsonStore<DictVal extends HDict>
this.#metaJson = grid.meta
this.#columnsJson = grid.cols
this.#rowsJson = grid.rows

this.removeEmptyColumnMeta()
}

get version(): string {
Expand All @@ -65,6 +67,12 @@ export class GridJsonStore<DictVal extends HDict>
// adds the version number to the grid's meta data. We need to remove the version so
// comparisons (i.e. `equals`) still work as expected.
if (this.#meta.has(GRID_VERSION_NAME)) {
if (!this.#version) {
this.#version = this.#meta
.get(GRID_VERSION_NAME)
?.toString()
}

this.#meta.remove(GRID_VERSION_NAME)
}
} else {
Expand Down Expand Up @@ -123,19 +131,26 @@ export class GridJsonStore<DictVal extends HDict>

toJSON(): HaysonGrid {
// Return a new grid only with the parts of the grid that have changed.
const meta =
this.#meta || this.#version
? { ...this.meta.toJSON() }
: this.#metaJson ?? {}

// Ensure the grid meta is always present. It always needs to be present in
// the JSON encoding.
if (!meta[GRID_VERSION_NAME]) {
meta[GRID_VERSION_NAME] = this.version
}

return {
_kind: Kind.Grid,
meta:
this.#meta || this.#version
? {
[GRID_VERSION_NAME]: this.version,
...this.meta.toJSON(),
}
: this.#metaJson ?? {},
meta,
cols: this.#columns
? this.#columns.map((column: GridColumn) => ({
name: column.name,
meta: column.meta.toJSON(),
meta: column.meta.isEmpty()
? undefined
: column.meta.toJSON(),
}))
: this.#columnsJson ?? [],
rows: this.#rows
Expand All @@ -144,6 +159,16 @@ export class GridJsonStore<DictVal extends HDict>
}
}

private removeEmptyColumnMeta() {
if (this.#columnsJson) {
for (const column of this.#columnsJson) {
if (column.meta && !Object.keys(column.meta).length) {
column.meta = undefined
}
}
}
}

toJSONString(): string {
return JSON.stringify(this.toJSON())
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/grid/GridObjStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class GridObjStore<DictVal extends HDict> implements GridStore<DictVal> {
},
cols: this.columns.map((column: GridColumn) => ({
name: column.name,
meta: column.meta.toJSON(),
meta: column.meta.isEmpty() ? undefined : column.meta.toJSON(),
})),
rows: this.rows.map((row: DictVal): HaysonDict => row.toJSON()),
}
Expand Down