mirror of https://github.com/Squidex/squidex.git
12 changed files with 242 additions and 146 deletions
@ -0,0 +1,44 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { AbstractControl, FormArray, FormGroup } from '@angular/forms'; |
|||
|
|||
import { Types } from '@app/framework/internal'; |
|||
|
|||
export const formControls = (form: AbstractControl): AbstractControl[] => { |
|||
if (Types.is(form, FormGroup)) { |
|||
return Object.values(form.controls); |
|||
} else if (Types.is(form, FormArray)) { |
|||
return form.controls; |
|||
} else { |
|||
return []; |
|||
} |
|||
}; |
|||
|
|||
export const fullValue = (form: AbstractControl): any => { |
|||
if (Types.is(form, FormGroup)) { |
|||
const groupValue = {}; |
|||
|
|||
for (let key in form.controls) { |
|||
if (form.controls.hasOwnProperty(key)) { |
|||
groupValue[key] = fullValue(form.controls[key]); |
|||
} |
|||
} |
|||
|
|||
return groupValue; |
|||
} else if (Types.is(form, FormArray)) { |
|||
const arrayValue = []; |
|||
|
|||
for (let child of form.controls) { |
|||
arrayValue.push(fullValue(child)); |
|||
} |
|||
|
|||
return arrayValue; |
|||
} else { |
|||
return form.value; |
|||
} |
|||
}; |
|||
@ -0,0 +1,42 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Lazy } from './lazy'; |
|||
|
|||
describe('Lazy', () => { |
|||
it('should provider value', () => { |
|||
const lazy = new Lazy(() => 1); |
|||
|
|||
expect(lazy.value).toBe(1); |
|||
}); |
|||
|
|||
it('should call delegate once', () => { |
|||
let called = 0; |
|||
|
|||
const lazy = new Lazy(() => { |
|||
called++; |
|||
return 13; |
|||
}); |
|||
|
|||
expect(lazy.value).toBe(13); |
|||
expect(lazy.value).toBe(13); |
|||
expect(called).toBe(1); |
|||
}); |
|||
|
|||
it('should call delegate once when returned undefined', () => { |
|||
let called = 0; |
|||
|
|||
const lazy = new Lazy(() => { |
|||
called++; |
|||
return undefined; |
|||
}); |
|||
|
|||
expect(lazy.value).toBeUndefined(); |
|||
expect(lazy.value).toBeUndefined(); |
|||
expect(called).toBe(1); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,24 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
export class Lazy<T> { |
|||
private valueSet = false; |
|||
private valueField: T; |
|||
|
|||
public get value(): T { |
|||
if (!this.valueSet) { |
|||
this.valueField = this.factory(); |
|||
this.valueSet = true; |
|||
} |
|||
|
|||
return this.valueField; |
|||
} |
|||
constructor( |
|||
private readonly factory: () => T |
|||
) { |
|||
} |
|||
} |
|||
Loading…
Reference in new issue