Angular 在角坐标系中放置独立函数的位置

Angular 在角坐标系中放置独立函数的位置,angular,conventions,Angular,Conventions,我有这些功能,它们被3个组件使用 放在哪里合适 我在想RubyonRails中的类似功能。他们有lib,但我不确定lib文件夹中的这些方法是否正常 当前位于src/helpers/upload file helpers.ts export function fileSizeConverter(size: number, fromUnit: string, toUnit: string ): number | string { const units: string[] = ['B', 'KB

我有这些功能,它们被3个组件使用

放在哪里合适

我在想RubyonRails中的类似功能。他们有lib,但我不确定lib文件夹中的这些方法是否正常

当前位于
src/helpers/upload file helpers.ts

export function fileSizeConverter(size: number, fromUnit: string, toUnit: string ): number | string {
  const units: string[] = ['B', 'KB', 'MB', 'GB', 'TB'];
  const from = units.indexOf(fromUnit.toUpperCase());
  const to = units.indexOf(toUnit.toUpperCase());
  const BASE_SIZE = 1024;
  let result: number | string = 0;

  if (from < 0 || to < 0 ) { return result = 'Error: Incorrect units'; }

  result = from < to ? size / (BASE_SIZE ** to) : size * (BASE_SIZE ** from);

  return result.toFixed(2);
}

另外,我故意不想把这些放在一起上课。这只是单独调用。

在TypeScript中编写实用程序函数有两种主要方法:

A) 普通函数(分组在文件中)

util.ts

用法

B) 类的静态方法

export class Math {
    static sum(a: number, b: number): number {
        return a + b;
    }
}
参考文献


为什么不创建具有类似utils功能的“utils”文件夹呢。没想到。但是助手的区别是什么呢?只是名字不同而已。其他想法-您也可以将功能共享到service.ohh中。我想我和帮手们相处得很好。我也在考虑这些服务,这样我就可以将它们注入消费者,但我不必这样做,因为有些消费者只会使用一个函数。谢谢。我通常会将这样的共享函数放入共享
服务中。
助手
实用程序
不一定是“错误的”,但我更喜欢它们作为服务。这是个人喜好的问题,也是什么对项目最有意义的问题。
export function fileExtensionChecker(file: string): boolean {
  const fileExtensions = {
    'png' : true,
    'jpg' : true,
    'jpeg': true,
    'stl' : true,
    'obj' : true,
    'zip' : true,
    'dcm' : true,
    '3oxz': true
  };
  // this is weird, instead of showing undefined if file argument is not present in the hash it will throw error.
  return fileExtensions[file] ? true : false;
}
export function fileTypeParser(fileType: string): string {
  return fileType.split('/')[1];
}
export function sum(a: number, b: number): number {
    return a + b;
}
export const sum = (a: number, b: number): number=> {
    return a + b;
};
import { sum } from './util';
...
let value = sum(4, 11);
export class Math {
    static sum(a: number, b: number): number {
        return a + b;
    }
}