@mic-rexjs/usecases
TypeScript icon, indicating that this package has built-in type declarations

2.7.10 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.7.100latest

Version History

VersionDownloads (Last 7 Days)Published
2.7.100
2.7.90
2.7.80
2.7.74
2.7.60
2.7.50
2.7.40
2.7.30
2.7.20
2.7.10
2.7.02
2.6.10
2.6.00
2.5.20
2.5.10
2.5.00
2.4.00
2.3.30
2.3.20
2.3.10
2.3.00
2.2.60
2.2.50
2.2.40
2.2.30
2.2.20
2.2.11
2.2.00
2.1.50
2.1.40
2.1.30
2.1.20
2.1.10
2.1.00
2.0.40
2.0.30
2.0.20
2.0.10
2.0.00
1.4.290
1.4.280
1.4.270
1.4.250
1.4.240
1.4.230
1.4.220
1.4.210
1.4.200
1.4.190
1.4.180
1.4.170
1.4.160
1.4.150
1.4.140
1.4.130
1.4.120
1.4.110
1.4.100
1.4.90
1.4.80
1.4.70
1.4.60
1.4.50
1.4.40
1.4.30
1.4.20

Package Sidebar

Install

npm i @mic-rexjs/usecases

Weekly Downloads

7

Version

2.7.10

License

MIT

Unpacked Size

75.4 kB

Total Files

67

Last publish

Collaborators

  • china-liji