fix: fix regular of variable (#3024)

* fix: fix regular of variable

* refactor: the better way
This commit is contained in:
被雨水过滤的空气-Rain 2023-11-11 11:23:34 +08:00 committed by GitHub
parent b92e9d1b69
commit 454db91827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 7 deletions

View File

@ -20,8 +20,8 @@ import { CollectionProvider, useCollection, useCollectionManager } from '../coll
import { FilterBlockRecord } from '../filter-provider/FilterProvider'; import { FilterBlockRecord } from '../filter-provider/FilterProvider';
import { useRecordIndex } from '../record-provider'; import { useRecordIndex } from '../record-provider';
import { SharedFilterProvider } from './SharedFilterProvider'; import { SharedFilterProvider } from './SharedFilterProvider';
import { useAssociationNames } from './hooks';
import { useTemplateBlockContext } from './TemplateBlockProvider'; import { useTemplateBlockContext } from './TemplateBlockProvider';
import { useAssociationNames } from './hooks';
export const BlockResourceContext = createContext(null); export const BlockResourceContext = createContext(null);
export const BlockAssociationContext = createContext(null); export const BlockAssociationContext = createContext(null);
@ -125,7 +125,6 @@ const useResourceAction = (props, opts = {}) => {
params['appends'] = appends; params['appends'] = appends;
} }
} }
console.log(templateFinshed);
const result = useRequest( const result = useRequest(
snapshot snapshot
? async () => ({ ? async () => ({

View File

@ -11,7 +11,7 @@ import { filterEmptyValues } from './utils/filterEmptyValues';
import { getAction } from './utils/getAction'; import { getAction } from './utils/getAction';
import { getPath } from './utils/getPath'; import { getPath } from './utils/getPath';
import { clearRequested, getRequested, hasRequested, stashRequested } from './utils/hasRequested'; import { clearRequested, getRequested, hasRequested, stashRequested } from './utils/hasRequested';
import { REGEX_OF_VARIABLE, isVariable } from './utils/isVariable'; import { isVariable } from './utils/isVariable';
import { uniq } from './utils/uniq'; import { uniq } from './utils/uniq';
export const VariablesContext = createContext<VariablesContextType>(null); export const VariablesContext = createContext<VariablesContextType>(null);
@ -234,9 +234,7 @@ const VariablesProvider = ({ children }) => {
let result = null; let result = null;
await onLocalVariablesReady(localVariables, async () => { await onLocalVariablesReady(localVariables, async () => {
const matches = variableString.match(REGEX_OF_VARIABLE); const path = getPath(variableString);
const path = matches[0].replace(REGEX_OF_VARIABLE, '$1');
result = getCollectionJoinField(getFieldPath(path)); result = getCollectionJoinField(getFieldPath(path));
// 当仅有一个例如 `$user` 这样的字符串时,需要拼一个假的 `collectionField` 返回 // 当仅有一个例如 `$user` 这样的字符串时,需要拼一个假的 `collectionField` 返回

View File

@ -0,0 +1,5 @@
import { getPath } from '../../utils/getPath';
test('{{ $user.name }} => $user.name', () => {
expect(getPath('{{ $user.name }}')).toBe('$user.name');
});

View File

@ -0,0 +1,5 @@
import { getVariableName } from '../../utils/getVariableName';
test('{{ $user.name }} => $user', () => {
expect(getVariableName('{{ $user.name }}')).toBe('$user');
});

View File

@ -0,0 +1,29 @@
import { isVariable } from '../../utils/isVariable';
it('should return false for a string with only one opening brace', () => {
expect(isVariable('{variable}}')).toBe(false);
});
it('should return false for a string with only one closing brace', () => {
expect(isVariable('{{variable}')).toBe(false);
});
it('should return false for a string with no braces', () => {
expect(isVariable('variable')).toBe(false);
});
it('should return true for a string with a valid variable', () => {
expect(isVariable('{{variable}}')).toBe(true);
});
it('should return true for a string with a valid variable "{{ $nRecord.cc-cc }}"', () => {
expect(isVariable('{{ $nRecord.cc-cc }}')).toBe(true);
});
it('should return true for a string with a valid variable "{{ $nRecord._name }}"', () => {
expect(isVariable('{{ $nRecord._name }}')).toBe(true);
});
it('should return true for a string with a valid variable " {{ $nRecord.name }} "', () => {
expect(isVariable(' {{ $nRecord.name }} ')).toBe(true);
});

View File

@ -1,4 +1,4 @@
export const REGEX_OF_VARIABLE = /\{\{\s*([a-zA-Z0-9_$.]+?)\s*\}\}/g; export const REGEX_OF_VARIABLE = /\{\{\s*([a-zA-Z0-9_$-.]+?)\s*\}\}/g;
export const isVariable = (str: unknown) => { export const isVariable = (str: unknown) => {
if (typeof str !== 'string') { if (typeof str !== 'string') {