@flowerforce/flower-core 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import { CoreUtilitiesFunctions } from './interfaces/CoreInterface';
2
+ export declare const flattenRules: (ob: Record<string, any>) => Record<string, any>;
3
+ export declare const CoreUtils: CoreUtilitiesFunctions;
@@ -0,0 +1,2 @@
1
+ import { TinyEmitter } from 'tiny-emitter';
2
+ export declare const Emitter: TinyEmitter;
@@ -0,0 +1,5 @@
1
+ import { Flatten, Unflatten } from './interfaces/FlatInterface';
2
+ export declare const flat: {
3
+ flatten: Flatten;
4
+ unflatten: Unflatten;
5
+ };
@@ -0,0 +1,2 @@
1
+ import { ReducersFunctions } from './interfaces/ReducerInterface';
2
+ export declare const FlowerCoreReducers: ReducersFunctions;
@@ -0,0 +1,2 @@
1
+ import { ISelectors } from './interfaces/SelectorsInterface';
2
+ export declare const FlowerCoreStateSelectors: ISelectors;
@@ -0,0 +1,2 @@
1
+ import { CoreStateUtils } from './interfaces/UtilsInterface';
2
+ export declare const FlowerStateUtils: CoreStateUtils;
@@ -0,0 +1,5 @@
1
+ export declare const MatchRules: {
2
+ rulesMatcher: (rules?: Record<string, any> | Record<string, any>[], formValue?: Record<string, any>, apply?: boolean, options?: Record<string, any>) => boolean[];
3
+ operators: import("./rules-matcher/interface").Operators;
4
+ utils: import("./rules-matcher/interface").RulesMatcherUtils;
5
+ };
@@ -0,0 +1,8 @@
1
+ export { flat as Flat } from './Flat';
2
+ export { Emitter } from './Emitter';
3
+ export { FlowerCoreReducers } from './FlowerCoreStateFunctions';
4
+ export { FlowerStateUtils } from './FlowerCoreStateUtils';
5
+ export { FlowerCoreStateSelectors as Selectors } from './FlowerCoreStateSelectors';
6
+ export { CoreUtils } from './CoreUtils';
7
+ export { MatchRules } from './RulesMatcher';
8
+ export * from './interfaces';
@@ -0,0 +1,129 @@
1
+ export type Rules<T> = {
2
+ rules: Rules<T> | string | null | undefined | T;
3
+ };
4
+ export type Edge<T = object> = {
5
+ source: string;
6
+ target: string;
7
+ id?: string;
8
+ data?: {
9
+ rules: RulesObject<T>;
10
+ };
11
+ };
12
+ export type Node = {
13
+ nodeId: string | undefined;
14
+ nodeType: string;
15
+ nodeTitle: string;
16
+ children: Record<string, any> | Record<string, any>[] | undefined;
17
+ nextRules: {
18
+ [x: string]: {
19
+ rules: RulesObject<any>;
20
+ } | RulesObject<any>;
21
+ } | undefined;
22
+ retain: boolean;
23
+ disabled: boolean;
24
+ id: string;
25
+ props: Record<string, any>;
26
+ type: Record<string, any>;
27
+ };
28
+ export declare enum RulesModes {
29
+ $and = "$and",
30
+ $or = "$or"
31
+ }
32
+ declare enum RulesOperators {
33
+ $exist = "$exist",
34
+ $eq = "$eq",
35
+ $ne = "$ne",
36
+ $gt = "$gt",
37
+ $gte = "$gte",
38
+ $lt = "$lt",
39
+ $lte = "$lte",
40
+ $strGt = "$strGt",
41
+ $strGte = "$strGte",
42
+ $strLt = "$strLt",
43
+ $strLte = "$strLte",
44
+ $in = "$in",
45
+ $nin = "$nin",
46
+ $all = "$all",
47
+ $regex = "$regex"
48
+ }
49
+ type RulesValuesType<T> = {
50
+ '$form.isValid': boolean;
51
+ } & T;
52
+ type RulesOperatorsInArray<T> = Partial<{
53
+ [KEY in keyof T]: Partial<{
54
+ [K in keyof typeof RulesOperators]: T[KEY];
55
+ }>;
56
+ }>;
57
+ export type RulesByNodeId<T extends Record<string, any>> = {
58
+ nodeId: string;
59
+ rules: {
60
+ rules: RulesObject<T>;
61
+ } | RulesObject<T> | null;
62
+ };
63
+ type RulesWithName = {
64
+ nodeId: string;
65
+ rules: string | {
66
+ name?: string;
67
+ rules: RulesObject<any>;
68
+ };
69
+ };
70
+ export type RulesObject<T> = RulesValuesType<T> | {
71
+ [K in keyof typeof RulesModes]: Array<RulesOperatorsInArray<RulesValuesType<T>>> | Array<RulesObject<RulesValuesType<T>>>;
72
+ } | Array<Exclude<RulesOperatorsInArray<RulesValuesType<T>>, undefined>>;
73
+ export type CleanPath = (name: string, char?: string) => string;
74
+ export type GetPath = (idValue?: string) => {
75
+ path: string | string[];
76
+ flowNameFromPath?: string;
77
+ };
78
+ export type AllEqual = (...args: Array<number | string | boolean>[]) => boolean;
79
+ export type FindValidRule<T = Rules<RulesObject<any>>> = (nextRules: {
80
+ rules: {
81
+ rules: T;
82
+ };
83
+ }, value: Record<string, any>, prefix?: {
84
+ prefix: string;
85
+ } | string) => T[keyof T] | undefined;
86
+ export type IsEmptyRules<T = {
87
+ rules: RulesObject<any>;
88
+ } | RulesObject<any>> = (rules: T) => boolean;
89
+ export type MapEdge<K = RulesByNodeId<any>, T = K[]> = (nextNode: T) => Array<K>;
90
+ export type MakeRules<T extends Record<string, any> = {
91
+ rules: RulesObject<any> | object;
92
+ }> = (rules: T) => Array<RulesByNodeId<T>>;
93
+ export type GetRulesExists = (rules: RulesByNodeId<any>[]) => ReturnType<MapEdge> | undefined;
94
+ export type GenerateNodesForFlowerJson = (nodes: Node[], edges?: Edge[]) => {
95
+ nodeId: string | undefined;
96
+ nodeType: string;
97
+ nodeTitle: string;
98
+ children: Node['children'];
99
+ nextRules: ReturnType<GetRulesExists>;
100
+ retain: boolean;
101
+ disabled: boolean;
102
+ }[];
103
+ export type HasNode = (state: Record<string, any>, name: string, node: string) => boolean;
104
+ export type MakeObjectRules = (nodes: Node[]) => {
105
+ [x: string]: Node['nextRules'];
106
+ };
107
+ export type GenerateNodes = (nodes: Node[]) => {
108
+ [x: string]: Omit<Node, 'nextRules'>;
109
+ };
110
+ export type MapKeysDeepLodash = (obj: Record<string, any>, cb: (...args: any) => any, isRecursive?: boolean) => Record<string, any>;
111
+ export type GenerateRulesName = (nextRules: RulesWithName[]) => {
112
+ [X: string]: string;
113
+ };
114
+ export interface CoreUtilitiesFunctions {
115
+ generateRulesName: GenerateRulesName;
116
+ mapKeysDeepLodash: MapKeysDeepLodash;
117
+ generateNodes: GenerateNodes;
118
+ makeObjectRules: MakeObjectRules;
119
+ hasNode: HasNode;
120
+ isEmptyRules: IsEmptyRules;
121
+ mapEdge: MapEdge;
122
+ makeRules: MakeRules;
123
+ generateNodesForFlowerJson: GenerateNodesForFlowerJson;
124
+ cleanPath: CleanPath;
125
+ getPath: GetPath;
126
+ allEqual: AllEqual;
127
+ findValidRule: FindValidRule;
128
+ }
129
+ export {};
@@ -0,0 +1,6 @@
1
+ export interface IEmitter {
2
+ on<F extends (...args: unknown[]) => unknown, C>(name: string, cb: F, ctx?: C): this;
3
+ once<F extends (...args: unknown[]) => unknown, C>(name: string, cb: F, ctx?: C): this;
4
+ emit(name: string, opt?: Record<string, any>): this;
5
+ off<F extends (...args: unknown[]) => unknown>(name: string, cb?: F): this;
6
+ }
@@ -0,0 +1,20 @@
1
+ export type KeyIdentity = (key: string) => string;
2
+ export type GetKey = (key?: string) => string | number | undefined;
3
+ interface FlattenOptions {
4
+ maxDepth: number;
5
+ safe?: boolean;
6
+ delimiter?: string;
7
+ transformKey?: (key: string) => string;
8
+ }
9
+ interface UnflattenOptions extends FlattenOptions {
10
+ overwrite?: boolean;
11
+ object?: boolean;
12
+ }
13
+ export type Flatten<T = object> = (target: T, opts?: FlattenOptions) => T;
14
+ export type FlattenStep = (object: {
15
+ [x: string]: any;
16
+ }, prev?: string, currentDepth?: number) => void;
17
+ export type Unflatten<T = {
18
+ [x: string]: any;
19
+ }> = (target: T, opts?: UnflattenOptions) => T;
20
+ export {};
@@ -0,0 +1,123 @@
1
+ import { Node } from './CoreInterface';
2
+ import { Flower } from './Store';
3
+ export type ActionWithPayload<T> = {
4
+ type: string;
5
+ payload: T;
6
+ };
7
+ type ReducerFunctionSign<T extends object, R> = (state: Record<string, Flower<T>>, action: ActionWithPayload<R>) => Record<string, Flower<T>> | void;
8
+ export type ActionsTypes = 'historyAdd' | 'historyPrevToNode' | 'setFormTouched' | 'forceAddHistory' | 'historyPop' | 'restoreHistory' | 'replaceNode' | 'initializeFromNode' | 'forceResetHistory' | 'destroy' | 'initNodes' | 'setCurrentNode' | 'formAddErrors' | 'formRemoveErrors' | 'addData' | 'addDataByPath' | 'replaceData' | 'unsetData' | 'setFormIsValidating' | 'node' | 'prevToNode' | 'next' | 'prev' | 'reset';
9
+ export type ReducersFunctions<T extends Record<string, any> = Record<string, Flower<Record<string, any>>>> = {
10
+ historyAdd: ReducerFunctionSign<T, {
11
+ name: string;
12
+ node: string;
13
+ }>;
14
+ historyPrevToNode: ReducerFunctionSign<T, {
15
+ name: string;
16
+ node: string;
17
+ } | string>;
18
+ setFormTouched: ReducerFunctionSign<T, {
19
+ flowName: string;
20
+ currentNode: string;
21
+ } | string>;
22
+ forceAddHistory: ReducerFunctionSign<T, {
23
+ history: string[];
24
+ name?: string;
25
+ flowName?: string;
26
+ }>;
27
+ historyPop: ReducerFunctionSign<T, {
28
+ name: string;
29
+ }>;
30
+ restoreHistory: ReducerFunctionSign<T, {
31
+ name: string;
32
+ }>;
33
+ replaceNode: ReducerFunctionSign<T, {
34
+ node: string;
35
+ name?: string;
36
+ flowName?: string;
37
+ }>;
38
+ initializeFromNode: ReducerFunctionSign<T, {
39
+ node: string;
40
+ name?: string;
41
+ flowName?: string;
42
+ }>;
43
+ forceResetHistory: ReducerFunctionSign<T, {
44
+ name?: string;
45
+ flowName?: string;
46
+ }>;
47
+ destroy: ReducerFunctionSign<T, {
48
+ name: string;
49
+ }>;
50
+ initNodes: ReducerFunctionSign<T, {
51
+ name: string;
52
+ startId: string;
53
+ persist: boolean;
54
+ nodes: Node[];
55
+ initialData: any;
56
+ }>;
57
+ setCurrentNode: ReducerFunctionSign<T, {
58
+ name: string;
59
+ node: string;
60
+ }>;
61
+ formAddErrors: ReducerFunctionSign<T, {
62
+ name: string;
63
+ currentNode: string;
64
+ id: string;
65
+ errors: {
66
+ [x: string]: string[];
67
+ } | string[];
68
+ }>;
69
+ formRemoveErrors: ReducerFunctionSign<T, {
70
+ name: string;
71
+ currentNode: string;
72
+ id: string;
73
+ }>;
74
+ addData: ReducerFunctionSign<T, {
75
+ flowName: string;
76
+ value: T;
77
+ }>;
78
+ addDataByPath: ReducerFunctionSign<T, {
79
+ id: string[];
80
+ flowName: string;
81
+ value: T | string;
82
+ }>;
83
+ replaceData: ReducerFunctionSign<T, {
84
+ flowName: string;
85
+ value: T;
86
+ }>;
87
+ unsetData: ReducerFunctionSign<T, {
88
+ id: string[] | string;
89
+ flowName: string;
90
+ }>;
91
+ setFormIsValidating: ReducerFunctionSign<T, {
92
+ name: string;
93
+ currentNode: string;
94
+ isValidating?: boolean;
95
+ }>;
96
+ node: ReducerFunctionSign<T, {
97
+ name: string;
98
+ flowName?: string;
99
+ nodeId?: string;
100
+ node?: string;
101
+ history: string[];
102
+ }>;
103
+ prevToNode: ReducerFunctionSign<T, {
104
+ name?: string;
105
+ flowName?: string;
106
+ node: string;
107
+ }>;
108
+ next: ReducerFunctionSign<T, {
109
+ name?: string;
110
+ flowName?: string;
111
+ data: T;
112
+ route?: string;
113
+ }>;
114
+ prev: ReducerFunctionSign<T, {
115
+ name?: string;
116
+ flowName?: string;
117
+ }>;
118
+ reset: ReducerFunctionSign<T, {
119
+ name?: string;
120
+ flowName?: string;
121
+ }>;
122
+ };
123
+ export {};
@@ -0,0 +1,42 @@
1
+ import { RulesObject } from './CoreInterface';
2
+ import { Flower, Form, Node } from './Store';
3
+ export interface ISelectors {
4
+ selectGlobal<T extends Record<string, any>>(state: {
5
+ flower: {
6
+ [x: string]: Flower<T>;
7
+ };
8
+ }): {
9
+ [x: string]: Flower<T>;
10
+ };
11
+ selectFlower<T extends Record<string, any>>(name: string): (state: {
12
+ [x: string]: Flower<T>;
13
+ }) => Flower<T>;
14
+ selectFlowerFormNode<T extends Record<string, any>>(id: string): (state: Flower<T>) => Form<T>;
15
+ selectFlowerHistory<T extends Record<string, any>>(flower: Flower<T>): Array<string>;
16
+ makeSelectNodesIds<T extends Record<string, any>>(flower: Flower<T>): Flower<T>['nodes'];
17
+ makeSelectStartNodeId<T extends Record<string, any>>(flower: Flower<T>): string;
18
+ makeSelectCurrentNodeId<T extends Record<string, any>>(flower: Flower<T>, startNodeId: Flower<T>['startId']): string;
19
+ makeSelectPrevNodeRetain<T extends Record<string, any>>(nodes: Flower<T>['nodes'], history: Flower<T>['history'], current: Flower<T>['current']): boolean | string | undefined;
20
+ makeSelectCurrentNodeDisabled<T extends Record<string, any>>(nodes: {
21
+ [x: string]: Partial<Node>;
22
+ }, current: Flower<T>['current']): boolean;
23
+ makeSelectNodeErrors<T extends Record<string, any>>(form: Form<T> | undefined): {
24
+ touched: boolean;
25
+ errors: any;
26
+ isValid: boolean;
27
+ isValidating?: boolean;
28
+ };
29
+ getDataByFlow<T extends Record<string, any>>(flower: Flower<T>): T;
30
+ getDataFromState<T extends Record<string, any>>(id: string | string[]): (data: T) => Partial<T>;
31
+ makeSelectNodeFormTouched<T extends Record<string, any>>(form: Form<T>): boolean | undefined;
32
+ makeSelectFieldError<T extends Record<string, any>>(name: string, id: string, validate: {
33
+ rules?: RulesObject<any>;
34
+ message?: string;
35
+ }[] | null): (data?: T) => Array<string>;
36
+ selectorRulesDisabled<T extends Record<string, any>>(id: string, rules: any, keys: string[] | null, flowName: string, value: any): (data: T | undefined, form: {
37
+ touched: boolean;
38
+ errors: any;
39
+ isValid: boolean;
40
+ isValidating?: boolean;
41
+ }) => boolean;
42
+ }
@@ -0,0 +1,64 @@
1
+ import { RulesByNodeId, RulesModes } from './CoreInterface';
2
+ export declare enum RulesOperators {
3
+ $exist = "$exist",
4
+ $eq = "$eq",
5
+ $ne = "$ne",
6
+ $gt = "$gt",
7
+ $gte = "$gte",
8
+ $lt = "$lt",
9
+ $lte = "$lte",
10
+ $strGt = "$strGt",
11
+ $strGte = "$strGte",
12
+ $strLt = "$strLt",
13
+ $strLte = "$strLte",
14
+ $in = "$in",
15
+ $nin = "$nin",
16
+ $all = "$all",
17
+ $regex = "$regex"
18
+ }
19
+ type RulesValuesType<T> = {
20
+ '$form.isValid': boolean;
21
+ } & T;
22
+ type RulesOperatorsInArray<T> = Partial<{
23
+ [KEY in keyof T]: Partial<{
24
+ [K in keyof typeof RulesOperators]: T[KEY];
25
+ }>;
26
+ }>;
27
+ export type RulesObject<T> = {
28
+ [K in keyof typeof RulesModes]: Array<RulesOperatorsInArray<RulesValuesType<T>>> | Array<RulesObject<RulesValuesType<T>>>;
29
+ } | Array<RulesOperatorsInArray<RulesValuesType<T>>>;
30
+ export interface StoreRoot<T extends Record<string, any>> {
31
+ flower: {
32
+ [x: string]: Flower<T>;
33
+ };
34
+ }
35
+ export interface Flower<T extends Record<string, any>> {
36
+ persist: boolean;
37
+ startId: string;
38
+ current: string;
39
+ history: string[];
40
+ nodes: {
41
+ [x: string]: Node;
42
+ };
43
+ nextRules: {
44
+ [x: string]: RulesByNodeId<T>[];
45
+ };
46
+ data: T;
47
+ form: {
48
+ [x: string]: Form<T>;
49
+ };
50
+ }
51
+ export interface Node {
52
+ nodeId: string;
53
+ nodeType: string;
54
+ retain?: boolean;
55
+ disabled?: boolean;
56
+ }
57
+ export type Form<T> = {
58
+ touched?: boolean;
59
+ isValidating?: boolean;
60
+ errors?: {
61
+ [K in keyof T]: Array<string>;
62
+ };
63
+ };
64
+ export {};
@@ -0,0 +1,12 @@
1
+ export interface CoreStateUtils {
2
+ getAllData<T extends object>(state: T | undefined): Record<string, any> | undefined;
3
+ selectFlowerFormNode<T extends object>(name: string, id: string): (state: T) => Record<string, any>;
4
+ makeSelectCurrentNodeId<T extends object>(name: string): (state: T) => string;
5
+ makeSelectCurrentNextRules<T extends object>(name: string): (state: T) => any;
6
+ makeSelectNodeErrors<T extends object>(name: string, currentNodeId: string): (state: T) => {
7
+ touched: boolean;
8
+ errors: any;
9
+ isValid: boolean;
10
+ isValidating?: boolean;
11
+ };
12
+ }
@@ -0,0 +1,5 @@
1
+ export * from './CoreInterface';
2
+ export * from './ReducerInterface';
3
+ export * from './SelectorsInterface';
4
+ export * from './Store';
5
+ export * from './UtilsInterface';
@@ -0,0 +1,64 @@
1
+ import { RulesObject } from '../interfaces/CoreInterface';
2
+ export type CheckTypeOf = (v: any) => boolean;
3
+ export type CheckOperationObj = {
4
+ op: keyof Operators;
5
+ value: any;
6
+ opt?: any;
7
+ };
8
+ export interface RulesMatcherUtils {
9
+ isNumber: <T>(el: T) => boolean;
10
+ rule: (block: Record<string, any>, keys: Record<string, any>, options: Record<string, any>) => {
11
+ valid: boolean;
12
+ name: string;
13
+ };
14
+ getKey: (block: RulesObject<any>, keys: Record<string, any>, options: Record<string, any>) => Record<string, any>;
15
+ isDate: CheckTypeOf;
16
+ isDefined: CheckTypeOf;
17
+ isObject: CheckTypeOf;
18
+ isFunction: CheckTypeOf;
19
+ isString: CheckTypeOf;
20
+ getDefaultStringValue: (value: '$required' | '$exists' | string) => CheckOperationObj;
21
+ getTypeOf: (value: any) => string | null;
22
+ getDefaultRule: (value: any) => CheckOperationObj;
23
+ isEmpty: (value: any) => boolean;
24
+ forceArray: <T>(value: T) => Array<T>;
25
+ getPath: (path: string, prefix?: string) => string;
26
+ forceNumber: <T>(value: T) => number;
27
+ checkRule: <T extends Record<string, any>>(block: RulesObject<T>, data: T, options: Record<string, any>) => boolean;
28
+ getKeys: <T extends Record<string, any>>(rules?: T, options?: Record<string, any>) => string[] | null;
29
+ }
30
+ export declare enum OperatorsKeys {
31
+ $exists = "$exists",
32
+ $eq = "$eq",
33
+ $ne = "$ne",
34
+ $gt = "$gt",
35
+ $gte = "$gte",
36
+ $lt = "$lt",
37
+ $lte = "$lte",
38
+ $strGt = "$strGt",
39
+ $strGte = "$strGte",
40
+ $strLt = "$strLt",
41
+ $strLte = "$strLte",
42
+ $in = "$in",
43
+ $nin = "$nin",
44
+ $all = "$all",
45
+ $regex = "$regex"
46
+ }
47
+ export type OperatorsFunction = (a: any, b: any, opt?: any, data?: any) => boolean;
48
+ export type Operators = {
49
+ $exists: OperatorsFunction;
50
+ $eq: OperatorsFunction;
51
+ $ne: OperatorsFunction;
52
+ $gt: OperatorsFunction;
53
+ $gte: OperatorsFunction;
54
+ $lt: OperatorsFunction;
55
+ $lte: OperatorsFunction;
56
+ $strGt: OperatorsFunction;
57
+ $strGte: OperatorsFunction;
58
+ $strLt: OperatorsFunction;
59
+ $strLte: OperatorsFunction;
60
+ $in: OperatorsFunction;
61
+ $nin: OperatorsFunction;
62
+ $all: OperatorsFunction;
63
+ $regex: OperatorsFunction;
64
+ };
@@ -0,0 +1,3 @@
1
+ import { Operators } from './interface';
2
+ declare const operators: Operators;
3
+ export default operators;
@@ -0,0 +1,3 @@
1
+ import { RulesMatcherUtils } from './interface';
2
+ declare const rulesMatcherUtils: RulesMatcherUtils;
3
+ export default rulesMatcherUtils;
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@flowerforce/flower-core",
3
+ "version": "3.0.0",
4
+ "description": "Core functions for flowerJS",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "scripts": {
9
+ "tsc:noemit": "tsc -p tsconfig.lib.json --noEmit"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "!dist/package.json",
14
+ "LICENSE",
15
+ "README.md",
16
+ "CHANGELOG.md"
17
+ ],
18
+ "license": "ISC",
19
+ "peerDependencies": {
20
+ "lodash": ">=4"
21
+ },
22
+ "devDependencies": {
23
+ "@types/lodash": "^4.17.1",
24
+ "jest": "^29.7.0",
25
+ "ts-jest": "^29.1.2",
26
+ "typescript": "^5.4.5"
27
+ },
28
+ "dependencies": {
29
+ "tiny-emitter": "^2.1.0"
30
+ },
31
+ "exports": {
32
+ "./package.json": "./package.json",
33
+ ".": {
34
+ "module": "./dist/index.esm.js",
35
+ "import": "./dist/index.cjs.mjs",
36
+ "default": "./dist/index.cjs.js"
37
+ }
38
+ },
39
+ "module": "./dist/index.esm.js",
40
+ "main": "./dist/index.cjs.js"
41
+ }