mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 10:37:01 +00:00
fix(utils): fix json-templates (#4525)
This commit is contained in:
parent
eea3262469
commit
073e2b0d5b
@ -8,7 +8,6 @@
|
||||
"@hapi/topo": "^6.0.0",
|
||||
"@rc-component/mini-decimal": "^1.1.0",
|
||||
"dayjs": "^1.11.9",
|
||||
"dedupe": "^3.0.2",
|
||||
"deepmerge": "^4.2.2",
|
||||
"flat-to-nested": "^1.1.1",
|
||||
"graphlib": "^2.1.8",
|
||||
|
27
packages/core/utils/src/__tests__/json-templates.test.ts
Normal file
27
packages/core/utils/src/__tests__/json-templates.test.ts
Normal file
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* This file is part of the NocoBase (R) project.
|
||||
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
||||
* Authors: NocoBase Team.
|
||||
*
|
||||
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { parse } from '../json-templates';
|
||||
|
||||
describe('json-templates', () => {
|
||||
it('parse json with string template', async () => {
|
||||
const template = {
|
||||
name: '{{id}}-{{name}}.',
|
||||
age: 18,
|
||||
};
|
||||
const result = parse(template)({
|
||||
name: 'test',
|
||||
id: 1,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
name: '1-test.',
|
||||
age: 18,
|
||||
});
|
||||
});
|
||||
});
|
@ -13,7 +13,6 @@
|
||||
// Created by Curran Kelleher and Chrostophe Serafin.
|
||||
// Contributions from Paul Brewer and Javier Blanco Martinez.
|
||||
import { get } from 'lodash';
|
||||
import dedupe from 'dedupe';
|
||||
|
||||
// An enhanced version of `typeof` that handles arrays and dates as well.
|
||||
function type(value) {
|
||||
@ -51,11 +50,7 @@ function Parameter(match) {
|
||||
|
||||
// Constructs a template function with deduped `parameters` property.
|
||||
function Template(fn, parameters) {
|
||||
// Paul Brewer Dec 2017 add deduplication call, use only key property to eliminate
|
||||
Object.assign(fn, {
|
||||
parameters: dedupe(parameters, (item) => item.key),
|
||||
});
|
||||
|
||||
fn.parameters = Array.from(new Map(parameters.map((parameter) => [parameter.key, parameter])).values());
|
||||
return fn;
|
||||
}
|
||||
|
||||
@ -87,7 +82,7 @@ export function parse(value) {
|
||||
const parseString = (() => {
|
||||
// This regular expression detects instances of the
|
||||
// template parameter syntax such as {{foo}} or {{foo:someDefault}}.
|
||||
const regex = /{{(\w|:|[\s-+.,@///()?=*_$])+}}/g;
|
||||
const regex = /{{(\w|:|[\s-+.,@/()?=*_$])+}}/g;
|
||||
|
||||
return (str) => {
|
||||
let parameters = [];
|
||||
@ -110,11 +105,12 @@ const parseString = (() => {
|
||||
value = value();
|
||||
}
|
||||
|
||||
// Accommodate numbers as values.
|
||||
if (str.startsWith('{{') && str.endsWith('}}')) {
|
||||
// Accommodate non-string as original values.
|
||||
if (matches.length === 1 && str.startsWith('{{') && str.endsWith('}}')) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Treat Date value inside string to ISO string.
|
||||
if (value instanceof Date) {
|
||||
value = value.toISOString();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user