Replace raw templates deeply when applying data to options (#15946)

* Add JsonValue type

* Replace raw templates deeply when applying data to options

* Add tests for applyOptionsData

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
Nicola Krumschmidt
2022-10-18 23:03:06 +02:00
committed by GitHub
parent 0f6ed82953
commit 13f4ec027d
3 changed files with 128 additions and 29 deletions

View File

@@ -29,4 +29,6 @@ export type DeepPartial<T> = T extends Builtin
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;
export type JsonValue = null | string | number | boolean | JsonValue[] | { [key: string]: JsonValue };
export type Plural<T extends string> = `${T}s`;

View File

@@ -0,0 +1,99 @@
import { describe, expect, it } from 'vitest';
import { applyOptionsData, optionToObject, optionToString } from './apply-options-data';
describe('applyOptionsData', () => {
it('returns an empty object if the options are empty', () => {
expect(applyOptionsData({}, {})).toEqual({});
});
it('returns the unchanged options if there are no mustaches', () => {
expect(
applyOptionsData(
{ str: 'num', arr: ['arr', { null: null }], obj: { str: 'obj', num: 42 } },
{ num: 42, arr: ['foo', 'bar'], obj: { foo: 'bar' } }
)
).toEqual({ str: 'num', arr: ['arr', { null: null }], obj: { str: 'obj', num: 42 } });
});
it('returns the options with any raw template replaced by the value in scope', () => {
expect(
applyOptionsData(
{ str: '{{ num }}', arr: ['{{ arr }}', { null: null }], obj: { str: '{{ obj }}', num: 42 } },
{ num: 42, arr: ['foo', 'bar'], obj: { foo: 'bar' } }
)
).toEqual({ str: 42, arr: [['foo', 'bar'], { null: null }], obj: { str: { foo: 'bar' }, num: 42 } });
});
it('returns the options with any non-raw template rendered with the respective stringified values from the scope', () => {
expect(
applyOptionsData(
{ str: 'num: {{ num }}', arr: ['arr: {{ arr }}', { null: null }], obj: { str: 'obj: {{ obj }}', num: 42 } },
{ num: 42, arr: ['foo', 'bar'], obj: { foo: 'bar' } }
)
).toEqual({
str: 'num: 42',
arr: ['arr: ["foo","bar"]', { null: null }],
obj: { str: 'obj: {"foo":"bar"}', num: 42 },
});
});
it('returns the options with raw templates with null scope values as literal null and undefined scope values as string undefined', () => {
expect(applyOptionsData({ null: '{{ null }}', undefined: '{{ undefined }}' }, { null: null })).toEqual({
null: null,
undefined: 'undefined',
});
});
it('returns the options with non-raw templates which reference null or undefined scope values as literal null and undefined strings', () => {
expect(
applyOptionsData({ null: 'null: {{ null }}', undefined: 'undefined: {{ undefined }}' }, { null: null })
).toEqual({
null: 'null: null',
undefined: 'undefined: undefined',
});
});
it('does not skip values in a template if they are not undefined', () => {
expect(applyOptionsData({ skip: '{{ num }}', keep: '{{ num }}' }, { num: 42 }, ['skip'])).toEqual({
skip: 42,
keep: 42,
});
expect(applyOptionsData({ skip: 'num: {{ num }}', keep: 'num: {{ num }}' }, { num: 42 }, ['skip'])).toEqual({
skip: 'num: 42',
keep: 'num: 42',
});
});
it('skips over values in a template which are undefined', () => {
expect(applyOptionsData({ skip: '{{ num }}', keep: '{{ num }}' }, {}, ['skip'])).toEqual({
skip: '{{ num }}',
keep: 'undefined',
});
expect(applyOptionsData({ skip: 'num: {{ num }}', keep: 'num: {{ num }}' }, {}, ['skip'])).toEqual({
skip: 'num: {{ num }}',
keep: 'num: undefined',
});
});
});
describe('optionToObject', () => {
it('returns the option parsed from json if the option is a string', () => {
expect(optionToObject('{ "foo": 42 }')).toEqual({ foo: 42 });
});
it('returns the unchanged option if it is not a string', () => {
expect(optionToObject(['foo', 42])).toEqual(['foo', 42]);
});
});
describe('optionToObject', () => {
it('returns the option stringified to json if it is an object or array', () => {
expect(optionToString({ foo: 42 })).toBe('{"foo":42}');
});
it('returns the option converted to a string if it is not an object or array', () => {
expect(optionToString(42)).toBe('42');
});
});

View File

@@ -1,8 +1,14 @@
import { renderFn, get, Scope, ResolveFn } from 'micromustache';
import { JsonValue } from '../types';
import { parseJSON } from './parse-json';
type Mustacheable = string | number | boolean | null | Mustacheable[] | { [key: string]: Mustacheable };
type GenericString<T> = T extends string ? string : T;
type Mustache<T> = T extends string
? JsonValue
: T extends Array<infer U>
? Array<Mustache<U>>
: T extends Record<any, any>
? { [K in keyof T]: Mustache<T[K]> }
: T;
export function applyOptionsData(
options: Record<string, any>,
@@ -10,51 +16,43 @@ export function applyOptionsData(
skipUndefinedKeys: string[] = []
): Record<string, any> {
return Object.fromEntries(
Object.entries(options).map(([key, value]) => {
if (typeof value === 'string') {
const single = value.match(/^\{\{\s*([^}\s]+)\s*\}\}$/);
if (single !== null && single.length > 0) {
const foundValue = get(data, single[1]!);
if (foundValue !== undefined || !skipUndefinedKeys.includes(key)) {
return [key, foundValue];
} else {
return [key, value];
}
}
}
return [key, renderMustache(value, data, skipUndefinedKeys.includes(key))];
})
Object.entries(options).map(([key, value]) => [key, renderMustache(value, data, skipUndefinedKeys.includes(key))])
);
}
function resolveFn(skipUndefined: boolean): ResolveFn {
return (path: string, scope?: Scope) => {
if (!scope) return skipUndefined ? `{{${path}}}` : undefined;
function resolveFn(skipUndefined: boolean): (path: string, scope: Scope) => any {
return (path, scope) => {
const value = get(scope, path);
if (value !== undefined || !skipUndefined) {
return typeof value === 'object' ? JSON.stringify(value) : value;
} else {
return `{{${path}}}`;
return `{{ ${path} }}`;
}
};
}
function renderMustache<T extends Mustacheable>(item: T, scope: Scope, skipUndefined: boolean): GenericString<T> {
function renderMustache<T extends JsonValue>(item: T, scope: Scope, skipUndefined: boolean): Mustache<T> {
if (typeof item === 'string') {
return renderFn(item, resolveFn(skipUndefined), scope, { explicit: true }) as GenericString<T>;
const raw = item.match(/^\{\{\s*([^}\s]+)\s*\}\}$/);
if (raw !== null) {
const value = get(scope, raw[1]!);
if (value !== undefined) {
return value;
}
}
return renderFn(item, resolveFn(skipUndefined) as ResolveFn, scope, { explicit: true }) as Mustache<T>;
} else if (Array.isArray(item)) {
return item.map((element) => renderMustache(element, scope, skipUndefined)) as GenericString<T>;
return item.map((element) => renderMustache(element, scope, skipUndefined)) as Mustache<T>;
} else if (typeof item === 'object' && item !== null) {
return Object.fromEntries(
Object.entries(item).map(([key, value]) => [key, renderMustache(value, scope, skipUndefined)])
) as GenericString<T>;
) as Mustache<T>;
} else {
return item as GenericString<T>;
return item as Mustache<T>;
}
}