2.7.6 • Published 1 month ago

@mic-rexjs/usecases v2.7.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Description

UseCases of Clean Architecture.

Install

$ npm install --save @mic-rexjs/usecases
# -
$ yarn add --dev @mic-rexjs/usecases

Links

Usage with Non-Entity Mode

// a.ts
import { Reducers } from '@mic-rexjs/usecases';

type MathReducers = Reducers<{
  add(value1: number, value2: number): number;

  subtraction(value1: number, value2: number): number;
}>;

const mathUseCase = (): MathReducers => {
  const add = (value1: number, value2: number): number => {
    return value1 + value2;
  };

  const subtraction = (value1: number, value2: number): number => {
    return value1 - value2;
  };

  return { add, subtraction };
};

// b.ts
const { add, subtraction } = mathUseCase();

add(1, 2); // 3
subtraction(5, 3); // 2

Usage with Entity Mode

// a.ts
import {
	objectUseCase,
	ObjectReducers,
	EntityGenerator,
	EntityReducers
} from '@mic-rexjs/usecases';

interface File {
  path: string;
  content: string;
}

interface FileUseCaseOptions {
  maxContentLength?: number;
}

// All reducers should provide the first argument with an entity type T, such as `file: T`.
type FileReducers<T extends File> = EntityReducers<
  T,
  {
    writeFile(entity: T, content: string): EntityGenerator<T, string>;
    isTxt(entity: T): boolean;
  },
  // optional to extends an existed reducers
  ObjectReducers<T>
>;

const fileUseCase = <T extends File>({ maxContentLength = 2000 }: FileUseCaseOptions = {}): FileReducers<T> => {
  /**
   * if you have not extends an existed reducers,
   * you should call `entityUseCase` at here,
   * such as `const entityReducers = entityUseCase<T>()`.
   */
  const objectReducers = objectUseCase<T>();

  const writeFile = function* (entity: T, content: string): EntityGenerator<T, string> {
    const { content: oldContent } = entity;
    const newContent = oldContent + content;

    if (newContent.length > maxContentLength) {
      throw 'max length error';
    }

    // set new entity by yield expression
    yield {
      ...entity,
      content: newContent,
    };

    // return the new content
    return newContent;
  };

  const isTxt = (entity: T): boolean => {
    const { path } = entity;

    return path.endsWith('.txt');
  };

  return { ...objectReducers, writeFile, isTxt };
};

// b.ts
const defaultFile: File = { path: '', content: '' };
const { createEntityReducers } = entityReducerUseCase();
const { writeFile, isTxt, setEntity } = createEntityReducers(defaultFile, fileUseCase, { maxContentLength: 50 });

// no need to provide an entity parameter when you call these reducers!
const [entity1, content1] = writeFile('hello world');
isTxt(); // false

console.log(entity1); // { path: '', content: 'hello world' }
console.log(content1); // 'hello world'

const [entity2] = setEntity({ path: 'my.txt' });
isTxt(); // true

console.log(entity2); // { path: 'my.txt', content: 'hello world' }

Usage with React

See more about @mic-rexjs/usecases-react

Test Demos

2.7.6

1 month ago

2.7.4

2 months ago

2.7.5

2 months ago

2.7.3

2 months ago

2.7.2

2 months ago

2.7.1

2 months ago

2.7.0

2 months ago

2.6.1

2 months ago

2.6.0

2 months ago

2.5.2

2 months ago

2.5.0

2 months ago

2.5.1

2 months ago

2.4.0

2 months ago

2.3.2

3 months ago

2.3.3

3 months ago

2.3.1

3 months ago

2.3.0

4 months ago

2.2.6

4 months ago

2.2.3

4 months ago

2.2.2

4 months ago

2.2.5

4 months ago

2.2.4

4 months ago

2.2.1

4 months ago

2.2.0

4 months ago

2.1.2

5 months ago

2.1.4

5 months ago

2.1.3

5 months ago

2.1.5

5 months ago

2.1.1

5 months ago

2.1.0

5 months ago

2.0.4

5 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

5 months ago

2.0.0

5 months ago

1.4.28

6 months ago

1.4.27

6 months ago

1.4.29

6 months ago

1.4.25

6 months ago

1.4.24

6 months ago

1.4.23

6 months ago

1.4.22

6 months ago

1.4.21

6 months ago

1.4.20

6 months ago

1.4.19

6 months ago

1.4.18

6 months ago

1.4.17

6 months ago

1.4.16

6 months ago

1.4.15

6 months ago

1.4.14

6 months ago

1.4.13

6 months ago

1.4.12

6 months ago

1.4.11

7 months ago

1.4.10

7 months ago

1.4.9

7 months ago

1.4.8

8 months ago

1.4.7

8 months ago

1.4.6

8 months ago

1.4.5

8 months ago

1.4.4

8 months ago

1.4.3

8 months ago

1.4.2

8 months ago