Skip to content

Commit

Permalink
Removed primitive data types from JsonDocumentLike, other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcouch-sil committed Apr 15, 2024
1 parent 2c5a7ab commit d30ec04
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/platform-bible-utils/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/platform-bible-utils/dist/index.cjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/platform-bible-utils/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type JsonObjectLike = {
[key: string]: unknown;
};
export type JsonArrayLike = unknown[];
export type JsonDocumentLike = JsonObjectLike | JsonArrayLike | number | string | null | undefined;
export type JsonDocumentLike = JsonObjectLike | JsonArrayLike;
/**
* Options for DocumentCombiner objects
*
Expand Down Expand Up @@ -1186,7 +1186,7 @@ export interface StateBase {
*/
export interface ModifierExtensionControlled {
[k: string]: unknown;
$ref?: undefined;
platformType?: undefined;
type?: undefined;
}
/** Group of related settings definitions */
Expand Down
32 changes: 16 additions & 16 deletions lib/platform-bible-utils/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/platform-bible-utils/dist/index.js.map

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions lib/platform-bible-utils/src/document-combiner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,20 @@ describe('Simple array combining', () => {
});
});

test('transformValidatedBaseDocument and transformValidatedContribution allow array or non-array docs to merge into an array together', () => {
const arrayBase = [1, 2, 3];
const numBase = 1;
const numContribution = 4;
test('transformBaseDocumentAfterValidation and transformContributionAfterValidation allow array or non-array docs to merge into an array together', () => {
const arrayBase = [{ thing: 0 }, [], [4]];
const objectBase = { thing: 1 };
const objectContribution = { stuff: 3 };
const arrayContribution = [5, 6];

const combiner = new ArrayDocumentCombiner(arrayBase);
expect(JSON.stringify(combiner.output)).toBe('[1,2,3]');
combiner.addOrUpdateContribution('numContribution', numContribution);
expect(JSON.stringify(combiner.output)).toBe('[1,2,3,4]');
expect(JSON.stringify(combiner.output)).toBe('[{"thing":0},[],[4]]');
combiner.addOrUpdateContribution('objectContribution', objectContribution);
expect(JSON.stringify(combiner.output)).toBe('[{"thing":0},[],[4],{"stuff":3}]');
combiner.addOrUpdateContribution('arrayContribution', arrayContribution);
expect(JSON.stringify(combiner.output)).toBe('[1,2,3,4,5,6]');
combiner.updateBaseDocument(numBase);
expect(JSON.stringify(combiner.output)).toBe('[1,4,5,6]');
expect(JSON.stringify(combiner.output)).toBe('[{"thing":0},[],[4],{"stuff":3},5,6]');
combiner.updateBaseDocument(objectBase);
expect(JSON.stringify(combiner.output)).toBe('[{"thing":1},{"stuff":3},5,6]');
});

test('Collisions are not allowed', () => {
Expand All @@ -247,8 +247,9 @@ test('Collisions are not allowed', () => {
test('Can handle various empty contributions', () => {
const combiner = new DocumentCombinerWithoutValidation({});
expect(() => combiner.addOrUpdateContribution('A', {})).not.toThrow();
// @ts-expect-error: Purposefully passing garbage
expect(() => combiner.addOrUpdateContribution('A', undefined)).not.toThrow();
// Purposefully passing garbage
// @ts-expect-error: Purposefully passing garbage
// eslint-disable-next-line no-null/no-null
expect(() => combiner.addOrUpdateContribution('A', null)).not.toThrow();
});
Expand Down
9 changes: 1 addition & 8 deletions lib/platform-bible-utils/src/document-combiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import { deepClone } from './util';
type JsonObjectLike = { [key: string]: unknown };
type JsonArrayLike = unknown[];

export type JsonDocumentLike =
| JsonObjectLike
| JsonArrayLike
| number
| string
| null
// Undefined is not a valid JSON type. However, our deserialize can output this, so might as well support it
| undefined;
export type JsonDocumentLike = JsonObjectLike | JsonArrayLike;

/**
* Options for DocumentCombiner objects
Expand Down
4 changes: 2 additions & 2 deletions lib/platform-bible-utils/src/settings.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface StateBase {
*/
export interface ModifierExtensionControlled {
[k: string]: unknown;
$ref?: undefined;
platformType?: undefined;
type?: undefined;
}
/** Group of related settings definitions */
Expand Down Expand Up @@ -354,7 +354,7 @@ const settingsDefs = {
anyOf: [
{
type: 'object',
required: ['$ref'],
required: ['platformType'],
},
{
type: 'object',
Expand Down
11 changes: 0 additions & 11 deletions src/shared/utils/settings-document-combiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ export default class SettingsDocumentCombiner extends DocumentCombiner {
// Return promise if cached
if (this.localizedOutputPromise) return this.localizedOutputPromise;

if (!this.latestOutput) return undefined;

// Not awaiting this promise, but we're catching inside the function and logging
this.localizedOutputPromise = localizeSettingsContributionInfo(
// We know the latest output is always SettingsContributionInfo because of our work in this combiner
Expand Down Expand Up @@ -280,13 +278,4 @@ export default class SettingsDocumentCombiner extends DocumentCombiner {
// validating to do. Unless someday we want to double check we have a properly formatted
// `SettingsContributionInfo`
}

// We don't need `this` on this override method
// eslint-disable-next-line class-methods-use-this
protected override transformFinalOutputBeforeValidation(
finalOutput: JsonDocumentLike,
): JsonDocumentLike {
// We already transformed the input documents, so we don't have any transforming to do
return finalOutput;
}
}

0 comments on commit d30ec04

Please sign in to comment.