From fc91f31302fb7f6ffaa112b62851f0fd8d037799 Mon Sep 17 00:00:00 2001 From: REllEK-IO Date: Mon, 23 Oct 2023 08:10:34 -0700 Subject: [PATCH] isConceptLoaded, unifyConcepts, unifyConceptNames, and selectUnifiedState helper functions --- package.json | 2 +- src/index.ts | 3 +- src/model/concept.ts | 97 +++++++++++++++++++++++++-------------- src/model/selector.ts | 11 +++++ src/test/concepts.test.ts | 18 ++++++++ 5 files changed, 94 insertions(+), 37 deletions(-) create mode 100644 src/test/concepts.test.ts diff --git a/package.json b/package.json index f6f55c3..d758238 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@phuire/strx", - "version": "0.0.44", + "version": "0.0.45", "description": "Unified Turing Machine", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/src/index.ts b/src/index.ts index 6116296..2976bde 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,7 +52,8 @@ export type { Method, Principle, Mode, - MethodCreator + MethodCreator, + isConceptLoaded } from './model/concept'; export type { KeyedSelector } from './model/selector'; export { selectState, selectConcept, selectSlice, selectPayload } from './model/selector'; diff --git a/src/model/concept.ts b/src/model/concept.ts index 1e641e1..58909e3 100644 --- a/src/model/concept.ts +++ b/src/model/concept.ts @@ -61,42 +61,60 @@ export function createConcept( }; } -// Worry about this Functionality Later -// export function unifyConcepts( -// baseConcept: Concept, -// targetConcept: Concept, -// key?: string, -// ): Concept { -// const baseConceptState = baseConcept.state as object; -// const targetConceptState = targetConcept.state as object; -// let baseConceptPrinciples: PrincipleFunction[] = []; -// if (baseConcept.principles) { -// baseConceptPrinciples = baseConcept.principles; -// } -// let targetConceptPrinciples: PrincipleFunction[] = []; -// if (targetConcept.principles) { -// targetConceptPrinciples = targetConcept.principles; -// } -// let baseConceptMode: Mode[] = []; -// if (baseConcept.mode) { -// baseConceptMode = baseConcept.mode; -// } -// let targetConceptMode: Mode[] = []; -// if (targetConcept.mode) { -// targetConceptMode = targetConcept.mode; -// } +// Will document usage later +export function unifyConcepts( + baseConcept: Concept, + targetConcept: Concept, + unifiedName: string +): Concept { + const baseConceptState = baseConcept.state as object; + const targetConceptState = targetConcept.state as object; + let baseConceptPrinciples: PrincipleFunction[] = []; + if (baseConcept.principles) { + baseConceptPrinciples = baseConcept.principles; + } + let targetConceptPrinciples: PrincipleFunction[] = []; + if (targetConcept.principles) { + targetConceptPrinciples = targetConcept.principles; + } + let baseConceptMode: Mode[] = []; + if (baseConcept.mode) { + baseConceptMode = baseConcept.mode; + } + let targetConceptMode: Mode[] = []; + if (targetConcept.mode) { + targetConceptMode = targetConcept.mode; + } + let baseConceptMeta: Record = {}; + if (baseConcept.meta) { + baseConceptMeta = baseConcept.meta; + } + let targetConceptMeta: Record = {}; + if (targetConcept.meta) { + targetConceptMeta = targetConcept.meta; + } + + return { + name: unifiedName, + qualities: [...baseConcept.qualities, ...targetConcept.qualities], + state: { + ...baseConceptState, + ...targetConceptState, + }, + principles: [...baseConceptPrinciples, ...targetConceptPrinciples], + mode: [...baseConceptMode, ...targetConceptMode], + meta: {...baseConceptMeta, ...targetConceptMeta} + }; +} -// return { -// key: key ? key : targetConcept.key, -// qualities: [...baseConcept.qualities, ...targetConcept.qualities], -// state: { -// ...baseConceptState, -// ...targetConceptState, -// }, -// principles: [...baseConceptPrinciples, ...targetConceptPrinciples], -// mode: [...baseConceptMode, ...targetConceptMode], -// }; -// } +/** + * Naming pattern for unified concepts within STRX + * unifiedName: base-target + * @IMPORTANT concept folder must carry the same naming convention + */ +export function unifyConceptNames(base: string, target: string) { + return base + '-' + target; +} export function createQuality( actionType: ActionType, @@ -136,3 +154,12 @@ export const defaultMethodCreator: MethodCreator = () : [Method, Subject ); return [defaultMethod, defaultSubject]; }; + +export const isConceptLoaded = (concepts: Concept[], conceptName: string): boolean => { + for (const concept of concepts) { + if (concept.name === conceptName) { + return true; + } + } + return false; +}; diff --git a/src/model/selector.ts b/src/model/selector.ts index 8ffc3c1..2f7af70 100644 --- a/src/model/selector.ts +++ b/src/model/selector.ts @@ -86,3 +86,14 @@ export function selectConcept(concepts: Concept[], name: string): Concept { } return concept as Concept; } + +export function selectUnifiedState(concepts: Concept[], name: string): T { + let concept; + for (let i = 0; i < concepts.length; i++) { + if (concepts[i].name.split(name).length > 1) { + concept = concepts[i]; + break; + } + } + return concept?.state as T; +} diff --git a/src/test/concepts.test.ts b/src/test/concepts.test.ts new file mode 100644 index 0000000..24c9827 --- /dev/null +++ b/src/test/concepts.test.ts @@ -0,0 +1,18 @@ +import { counterName, createCounterConcept } from '../concepts/counter/counter.concept'; +import { createAxium } from '../model/axium'; +import { isConceptLoaded } from '../model/concept'; + +test('Concepts exists', (done) => { + const axium = createAxium('Mock Axium', [createCounterConcept()]); + axium.stage('Check Concepts', [ + (concepts, __) => { + if (isConceptLoaded(concepts, counterName)) { + expect(true).toBe(true); + done(); + } else { + expect(false).toBe(true); + done(); + } + }, + ]); +});