Browse Source

Added interpolator.

pull/337/head
Sebastian Stehle 8 years ago
parent
commit
4f662ec05a
  1. 1
      src/Squidex/app/framework/internal.ts
  2. 79
      src/Squidex/app/framework/utils/interpolator.spec.ts
  3. 43
      src/Squidex/app/framework/utils/interpolator.ts
  4. 2
      src/Squidex/app/shared/state/contents.forms.spec.ts

1
src/Squidex/app/framework/internal.ts

@ -23,6 +23,7 @@ export * from './utils/date-helper';
export * from './utils/date-time';
export * from './utils/duration';
export * from './utils/error';
export * from './utils/interpolator';
export * from './utils/immutable-array';
export * from './utils/lazy';
export * from './utils/math-helper';

79
src/Squidex/app/framework/utils/interpolator.spec.ts

@ -0,0 +1,79 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { DateTime } from './date-time';
import { interpolate } from './interpolator';
describe('interpolate', () => {
it('should keep string untouched if does not contain interpolation', () => {
const result = interpolate('hello world');
expect(result).toEqual('hello world');
});
it('should interpolate with object value', () => {
const result = interpolate('hello ${string}', { string: 'world' });
expect(result).toEqual('hello world');
});
it('should interpolate with array value', () => {
const result = interpolate('hello ${1}', ['my', 'world']);
expect(result).toEqual('hello world');
});
it('should interpolate with complex path', () => {
const result = interpolate('hello ${array.1}', { array: ['my', 'world'] });
expect(result).toEqual('hello world');
});
it('should return undefined if not found in object', () => {
const result = interpolate('hello ${value}', { string: 'world' });
expect(result).toEqual('hello undefined');
});
it('should return undefined if not found in array', () => {
const result = interpolate('hello ${4}', ['my', 'world']);
expect(result).toEqual('hello undefined');
});
it('should return undefined if not a valid index', () => {
const result = interpolate('hello ${index}', ['my', 'world']);
expect(result).toEqual('hello undefined');
});
it('should return undefined if it resolved to object', () => {
const result = interpolate('hello ${data}', { data: { } });
expect(result).toEqual('hello undefined');
});
it('should return undefined if it resolved to array', () => {
const result = interpolate('hello ${data}', { data: [] });
expect(result).toEqual('hello undefined');
});
it('should allow object shortcuts', () => {
const result = interpolate('hello ${data}', { data: { iv: 'world' } }, 'iv');
expect(result).toEqual('hello world');
});
it('should resolve dateTime', () => {
const now = DateTime.now();
const result = interpolate('hello ${time}', { time: now });
expect(result).toEqual(`hello ${now.toISOString()}`);
});
});

43
src/Squidex/app/framework/utils/interpolator.ts

@ -0,0 +1,43 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { DateTime } from './date-time';
import { Types } from './types';
const regex = /\${[^}]+}/;
export function interpolate(pattern: string, value?: any, shortcut?: string, fallback = 'undefined'): string {
const result = pattern.replace(regex, (match: string) => {
let replaced = value;
const path = match.substr(2, match.length - 3).split('.');
for (let segment of path) {
if (Types.isObject(replaced)) {
replaced = replaced[segment];
} else if (Types.isArray(replaced)) {
replaced = replaced[Number.parseInt(segment, 10)];
} else {
break;
}
}
if (Types.isString(replaced)) {
return replaced;
} else if (Types.isNumber(replaced)) {
return replaced.toString();
} else if (Types.is(replaced, DateTime)) {
return replaced.toISOString();
} else if (Types.isObject(replaced) && shortcut) {
return replaced[shortcut] || fallback;
}
return fallback;
});
return result;
}

2
src/Squidex/app/shared/state/contents.forms.spec.ts

@ -371,7 +371,7 @@ describe('StringField', () => {
});
function createSchema(properties: SchemaPropertiesDto, index = 1, fields: RootFieldDto[]) {
return new SchemaDetailsDto('id' + index, 'schema' + index, '', properties, false, true, null!, null!, null!, null!, null!, fields);
return new SchemaDetailsDto('id' + index, 'schema' + index, '', properties, false, true, null!, null!, null!, null!, null!, fields, {});
}
function createField(properties: FieldPropertiesDto, index = 1, partitioning = 'languages') {

Loading…
Cancel
Save