Skip to content

Commit

Permalink
feat(core/common): add helper to build a string from a string identifier
Browse files Browse the repository at this point in the history
refactor: move context inteface to common package
  • Loading branch information
trik committed Dec 3, 2020
1 parent 6e3abab commit b2f03b1
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 7 deletions.
54 changes: 54 additions & 0 deletions src/core/common/build-string-identifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfContext} from './context';
import {AjfStringIdentifier} from './string-identifier';

export const buildStringIdentifier =
(stringIdentifier: AjfStringIdentifier[]|undefined, context: AjfContext, emptyString = '') => {
if (stringIdentifier == null) {
return emptyString;
}
const str: string[] = stringIdentifier.map(s => {
const values: string[] = [];
if (s.value != null && s.value.length > 0) {
s.value.forEach(curValue => {
let val: string|number|string[]|number[]|null = null;
const vp: string[] = curValue.split('.');
vp.forEach(k => {
if (context[k] !== undefined) {
val = context[k];
}
});
if (val != null && (val as unknown) instanceof Array &&
(val as (string | number)[]).length > 0) {
val = (val as (string | number)[]).join(', ');
}
if (val != null) {
values.push(`${val}`);
}
});
}
return `${s.label}: ${values.length > 0 ? values.join(', ') : emptyString}`;
});
return str.join(' - ');
};
File renamed without changes.
1 change: 1 addition & 0 deletions src/core/common/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
export * from './apply-styles-directive';
export * from './auto-focus.directive';
export * from './common-module';
export * from './context';
export * from './dnd-directive';
export * from './format-if-number';
export * from './string-identifier';
Expand Down
1 change: 1 addition & 0 deletions src/core/models/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ng_module(
assets = [],
module_name = "@ajf/core/models",
deps = [
"//src/core/common",
"//src/core/utils",
"@npm//@angular/core",
"@npm//@types/esprima",
Expand Down
23 changes: 23 additions & 0 deletions src/core/models/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

export type AjfContext = import('@ajf/core/common').AjfContext;
2 changes: 1 addition & 1 deletion src/core/models/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*
*/

export * from './context';
export * from './error';
export * from './interface/condition';
export * from './interface/context';
export * from './interface/formula';
export * from './interface/validation-function';
export * from './serializers/condition-serializer';
Expand Down
2 changes: 1 addition & 1 deletion src/core/models/utils/evaluate-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*
*/

import {AjfContext} from '@ajf/core/common';
import {tokenize} from 'esprima';

import {AjfContext} from '../interface/context';
import {AjfExpressionUtils} from './expression-utils';

let execContext: any = {};
Expand Down
3 changes: 2 additions & 1 deletion src/core/models/utils/get-context-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*
*/

import {AjfContext} from '../interface/context';
import {AjfContext} from '@ajf/core/common';

import {AjfExpressionUtils} from './expression-utils';

export function getContextString(context?: AjfContext): string {
Expand Down
3 changes: 2 additions & 1 deletion src/core/models/utils/validate-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*
*/

import {AjfContext} from '../interface/context';
import {AjfContext} from '@ajf/core/common';

import {getContextString} from './get-context-string';

let cachedContext: any = {};
Expand Down
4 changes: 4 additions & 0 deletions tools/public_api_guard/core/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export declare class AjfCommonModule {
static ɵmod: i0.ɵɵNgModuleDefWithMeta<AjfCommonModule, [typeof i1.AjfDndDirective, typeof i2.AjfVideoDirective, typeof i3.ApplyStylesDirective, typeof i4.AutofocusDirective, typeof i5.FormatIfNumber, typeof i6.TranslateIfString], never, [typeof i1.AjfDndDirective, typeof i2.AjfVideoDirective, typeof i3.ApplyStylesDirective, typeof i4.AutofocusDirective, typeof i5.FormatIfNumber, typeof i6.TranslateIfString]>;
}

export declare type AjfContext = {
[key: string]: any;
};

export declare class AjfDndDirective {
file: Observable<FileList>;
get over(): boolean;
Expand Down
4 changes: 1 addition & 3 deletions tools/public_api_guard/core/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export declare class AjfConditionSerializer {
static fromJson(json: Partial<AjfCondition>): AjfCondition;
}

export declare type AjfContext = {
[key: string]: any;
};
export declare type AjfContext = import('@ajf/core/common').AjfContext;

export declare class AjfError extends Error {
get message(): string;
Expand Down

0 comments on commit b2f03b1

Please sign in to comment.