2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 09:24:04 +00:00
books/schemas/tests/helpers.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-03-28 10:01:29 +00:00
import { cloneDeep } from 'lodash';
2022-03-25 12:32:37 +00:00
import Account from '../app/Account.json';
import JournalEntry from '../app/JournalEntry.json';
import JournalEntryAccount from '../app/JournalEntryAccount.json';
import PartyRegional from '../regional/in/Party.json';
import { Schema, SchemaStub, SchemaStubMap } from '../types';
import Customer from './Customer.json';
import Party from './Party.json';
2022-03-25 12:32:37 +00:00
interface AppSchemaMap extends SchemaStubMap {
Account: SchemaStub;
JournalEntry: SchemaStub;
JournalEntryAccount: SchemaStub;
Party: SchemaStub;
Customer: SchemaStub;
}
interface RegionalSchemaMap extends SchemaStubMap {
Party: SchemaStub;
}
export function getTestSchemaMap(): {
appSchemaMap: AppSchemaMap;
regionalSchemaMap: RegionalSchemaMap;
} {
const appSchemaMap = {
Account,
JournalEntry,
JournalEntryAccount,
Party,
Customer,
} as AppSchemaMap;
const regionalSchemaMap = { Party: PartyRegional } as RegionalSchemaMap;
2022-03-28 10:01:29 +00:00
return cloneDeep({
2022-03-25 12:32:37 +00:00
appSchemaMap,
regionalSchemaMap,
2022-03-28 10:01:29 +00:00
});
2022-03-25 12:32:37 +00:00
}
export function everyFieldExists(fieldList: string[], schema: Schema): boolean {
return fieldsExist(fieldList, schema, 'every');
}
export function someFieldExists(fieldList: string[], schema: Schema): boolean {
return fieldsExist(fieldList, schema, 'some');
}
function fieldsExist(
fieldList: string[],
schema: Schema,
type: 'every' | 'some'
): boolean {
const schemaFieldNames = schema.fields.map((f) => f.fieldname);
return fieldList.map((f) => schemaFieldNames.includes(f))[type](Boolean);
}
export function subtract(
targetList: string[],
...removalLists: string[][]
): string[] {
const removalList = removalLists.flat();
return targetList.filter((f) => !removalList.includes(f));
}