Skip to content

Commit

Permalink
isConceptLoaded, unifyConcepts, unifyConceptNames, and selectUnifiedS…
Browse files Browse the repository at this point in the history
…tate helper functions
  • Loading branch information
REllEK-IO committed Oct 23, 2023
1 parent 0ac0782 commit fc91f31
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
97 changes: 62 additions & 35 deletions src/model/concept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,unknown> = {};
if (baseConcept.meta) {
baseConceptMeta = baseConcept.meta;
}
let targetConceptMeta: Record<string,unknown> = {};
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,
Expand Down Expand Up @@ -136,3 +154,12 @@ export const defaultMethodCreator: MethodCreator = () : [Method, Subject<Action>
);
return [defaultMethod, defaultSubject];
};

export const isConceptLoaded = (concepts: Concept[], conceptName: string): boolean => {
for (const concept of concepts) {
if (concept.name === conceptName) {
return true;
}
}
return false;
};
11 changes: 11 additions & 0 deletions src/model/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ export function selectConcept(concepts: Concept[], name: string): Concept {
}
return concept as Concept;
}

export function selectUnifiedState<T>(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;
}
18 changes: 18 additions & 0 deletions src/test/concepts.test.ts
Original file line number Diff line number Diff line change
@@ -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();
}
},
]);
});

0 comments on commit fc91f31

Please sign in to comment.