mirror of https://github.com/Squidex/squidex.git
4 changed files with 124 additions and 1 deletions
@ -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()}`); |
|||
}); |
|||
}); |
|||
@ -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; |
|||
} |
|||
Loading…
Reference in new issue